* INFRA-2866 | added APIs to get product for user and to get asigner and responder teams * INFRA-2866 | added create-incident-v3 API * INFRA-2866 | migration script to fill team_severity, team_user and team_user_severity tables * INFRA-2866 | adding team severity users upon team and severity update * INFRA-2866 | using update team v2 in slack action * INFRA-2866 | update product flow * INFRA-2866 | fixed user not invited issue * INFRA-2866 | updated API paths * INFRA-2866 | using constant for header fetching * INFRA-2866 | PR review changes
163 lines
3.8 KiB
Go
163 lines
3.8 KiB
Go
package team
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
"houston/model/log"
|
|
utils "houston/service/utils"
|
|
"time"
|
|
)
|
|
|
|
type Repository struct {
|
|
gormClient *gorm.DB
|
|
logRepository *log.Repository
|
|
}
|
|
|
|
var valueBeforeUpdate TeamEntity
|
|
var valueAfterUpdate TeamEntity
|
|
var differences []utils.Difference
|
|
|
|
func NewTeamRepository(gormClient *gorm.DB, logRepository *log.Repository) *Repository {
|
|
return &Repository{
|
|
gormClient: gormClient,
|
|
logRepository: logRepository,
|
|
}
|
|
}
|
|
|
|
func (e *Repository) CreateTeam(team *TeamEntity) (*TeamEntity, error) {
|
|
result := e.gormClient.Create(team)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return team, nil
|
|
}
|
|
|
|
func (r *Repository) GetAllActiveTeams() (*[]TeamEntity, error) {
|
|
var teamEntity []TeamEntity
|
|
|
|
result := r.gormClient.Order("name asc").Find(&teamEntity, "active = ?", true)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
} else if result.RowsAffected == 0 {
|
|
return nil, nil
|
|
}
|
|
return &teamEntity, nil
|
|
}
|
|
|
|
func (r *Repository) FindTeamByName(teamName string) (*TeamEntity, error) {
|
|
var teamEntity TeamEntity
|
|
|
|
result := r.gormClient.Find(&teamEntity, "name = ?", teamName)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
} else if result.RowsAffected == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
return &teamEntity, nil
|
|
}
|
|
|
|
func (r *Repository) FindTeamById(teamId uint) (*TeamEntity, error) {
|
|
var teamEntity TeamEntity
|
|
|
|
result := r.gormClient.Find(&teamEntity, "id = ?", teamId)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
} else if result.RowsAffected == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
return &teamEntity, nil
|
|
}
|
|
|
|
func (r *Repository) FindTeamByTeamName(teamName string) (*TeamEntity, error) {
|
|
var teamEntity TeamEntity
|
|
result := r.gormClient.Find(&teamEntity, "LOWER(name) = LOWER(?)", teamName)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
} else if result.RowsAffected == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
return &teamEntity, nil
|
|
}
|
|
|
|
func (t *TeamEntity) BeforeUpdate(tx *gorm.DB) (err error) {
|
|
println(fmt.Sprintf("BeforeUpdate executed at: %v", time.Now()))
|
|
valueBeforeUpdate = TeamEntity{}
|
|
if err := tx.First(&valueBeforeUpdate, t.ID).Error; err != nil {
|
|
return err
|
|
}
|
|
println(fmt.Sprintf("team entity before update is: %s", valueBeforeUpdate))
|
|
return nil
|
|
}
|
|
|
|
func (t *TeamEntity) AfterUpdate(tx *gorm.DB) (err error) {
|
|
println(fmt.Sprintf("AfterUpdate executed at: %v", time.Now()))
|
|
valueAfterUpdate = TeamEntity{}
|
|
if err := tx.First(&valueAfterUpdate, t.ID).Error; err != nil {
|
|
return err
|
|
}
|
|
println(fmt.Sprintf("team entity after updated is: %s", valueAfterUpdate))
|
|
differences = utils.DeepCompare(valueBeforeUpdate, valueAfterUpdate)
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) CaptureLogs() {
|
|
if differences != nil && len(differences) > 0 {
|
|
jsonDiff, _ := json.Marshal(differences)
|
|
|
|
jsonUser, _ := json.Marshal(log.UserInfo{
|
|
Id: "",
|
|
Email: valueAfterUpdate.UpdatedBy,
|
|
Name: "",
|
|
})
|
|
|
|
r.logRepository.CreateLog(
|
|
&log.LogEntity{
|
|
CreatedAt: time.Now(),
|
|
RelationName: "team",
|
|
RecordId: valueAfterUpdate.ID,
|
|
Changes: jsonDiff,
|
|
UserInfo: jsonUser,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r *Repository) UpdateTeam(teamEntity *TeamEntity) error {
|
|
result := r.gormClient.Updates(teamEntity)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
|
|
r.CaptureLogs()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) UpdateTeamStatus(teamEntity *TeamEntity) error {
|
|
result := r.gormClient.Model(&teamEntity).Updates(map[string]interface{}{"updated_by": teamEntity.UpdatedBy, "active": false})
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
|
|
r.CaptureLogs()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) FindActiveTeamById(id uint) (*TeamEntity, error) {
|
|
var teamEntity TeamEntity
|
|
|
|
result := r.gormClient.Find(&teamEntity, "id = ? and active = ?", id, true)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
} else if result.RowsAffected == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
return &teamEntity, nil
|
|
}
|