Files
houston-be/pkg/postgres/query/incident_role.go
Shubham Kirve b974cb6bf3 TP-0000 | Initialize houston repo (#1)
* TP-0000 | intialize houston repo

* TP-0000 | intialize houston repo
2023-03-29 00:01:17 +05:30

43 lines
1.2 KiB
Go

package query
import (
"houston/entity"
"houston/model/request"
"time"
"gorm.io/gorm"
)
func UpsertIncidentRole(db *gorm.DB, addIncidnentRoleRequest *request.AddIncidentRoleRequest) error {
incidentRolesEntity := &entity.IncidentRoles{
IncidentId: addIncidnentRoleRequest.IncidentId,
Role: addIncidnentRoleRequest.Role,
AssignedToUserSlackId: addIncidnentRoleRequest.UserId,
AssignedByUserSlackId: addIncidnentRoleRequest.CreatedById,
}
var incidentRoles entity.IncidentRoles
result := db.Find(&incidentRoles, "incident_id = ? AND role = ?", addIncidnentRoleRequest.IncidentId, addIncidnentRoleRequest.Role)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 1 {
incidentRolesEntity.ID = incidentRoles.ID
incidentRolesEntity.CreatedAt = incidentRoles.CreatedAt
incidentRolesEntity.UpdatedAt = time.Now()
addResult := db.Save(incidentRolesEntity)
if addResult != nil {
return addResult.Error
}
return nil
} else if result.RowsAffected == 0 {
addResult := db.Create(incidentRolesEntity)
if addResult != nil {
return addResult.Error
}
return nil
}
return gorm.ErrInvalidData
}