INFRA-3664 : Make get users in conversation api more performant, add api to sync incident users, module to track request statuses and remove old add incident-user api (#451)
* INFRA-3664 : Make get users in conversation api more performant * INFRA-3664 : Add apis for incident user sync and get users in incident performance improvements * INFRA-3664 : Self review * INFRA-3664 : Add migration script * INFRA-3664 : Review comments * INFRA-3664 : Constant chanes * INFRA-3664 : Add rate limit constants * INFRA-3664 : Add rate limit constants * INFRA-3664 : Fix failing tests * INFRA-3664 : Add UT's
This commit is contained in:
@@ -39,3 +39,59 @@ func (repo *incidentUserRepositoryImpl) RemoveIncidentUser(incidentId uint, user
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *incidentUserRepositoryImpl) GetIncidentUsersByIncidentId(incidentId uint) ([]incidentUser.IncidentUserEntity, error) {
|
||||
var incidentUsers []incidentUser.IncidentUserEntity
|
||||
result := repo.gormClient.Where("incident_id = ?", incidentId).Preload("Incident").Preload("User").Find(&incidentUsers)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
return incidentUsers, nil
|
||||
}
|
||||
|
||||
func (repo *incidentUserRepositoryImpl) GetIncidentUsersByIncidentIds(incidentIds []uint) ([]incidentUser.IncidentUserEntity, error) {
|
||||
var incidentUsers []incidentUser.IncidentUserEntity
|
||||
result := repo.gormClient.Where("incident_id IN ?", incidentIds).Preload("Incident").Preload("User").Find(&incidentUsers)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
return incidentUsers, nil
|
||||
}
|
||||
|
||||
func (repo *incidentUserRepositoryImpl) ReplaceIncidentUsers(incidentUserIdsToRemove []uint, incidentIdToUserIdMappings [][]uint) error {
|
||||
return repo.gormClient.Transaction(func(tx *gorm.DB) error {
|
||||
if len(incidentUserIdsToRemove) != 0 {
|
||||
removeResult := tx.Where("id IN ?", incidentUserIdsToRemove).Delete(&incidentUser.IncidentUserEntity{})
|
||||
if removeResult.Error != nil {
|
||||
logger.Error(fmt.Sprintf("failed to remove incident-user mappings with incidentIds %v: %v", incidentUserIdsToRemove, removeResult.Error))
|
||||
return removeResult.Error
|
||||
}
|
||||
}
|
||||
|
||||
if len(incidentIdToUserIdMappings) != 0 {
|
||||
incidentUsers := createIncidentUserEntitiesFromIncidentUserPairs(incidentIdToUserIdMappings)
|
||||
createResult := tx.Create(&incidentUsers)
|
||||
if createResult.Error != nil {
|
||||
logger.Error(fmt.Sprintf("failed to add incident-user mappings with incidentIdToUserIdMappings %v: %v", incidentIdToUserIdMappings, createResult.Error))
|
||||
return createResult.Error
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func createIncidentUserEntitiesFromIncidentUserPairs(incidentUserPairs [][]uint) []incidentUser.IncidentUserEntity {
|
||||
var incidentUsers []incidentUser.IncidentUserEntity
|
||||
for _, pair := range incidentUserPairs {
|
||||
incidentUsers = append(incidentUsers, incidentUser.IncidentUserEntity{
|
||||
IncidentID: pair[0],
|
||||
UserID: pair[1],
|
||||
})
|
||||
}
|
||||
return incidentUsers
|
||||
}
|
||||
|
||||
@@ -2,11 +2,15 @@ package incidentUser
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"houston/model/incidentUser"
|
||||
)
|
||||
|
||||
type IncidentUserRepository interface {
|
||||
AddIncidentUser(incidentId uint, userId uint) error
|
||||
RemoveIncidentUser(incidentId uint, userId uint) error
|
||||
GetIncidentUsersByIncidentId(incidentId uint) ([]incidentUser.IncidentUserEntity, error)
|
||||
GetIncidentUsersByIncidentIds(incidentIds []uint) ([]incidentUser.IncidentUserEntity, error)
|
||||
ReplaceIncidentUsers(incidentUserIdsToRemove []uint, incidentIdToUserIdMappings [][]uint) error
|
||||
}
|
||||
|
||||
func NewIncidentUserRepository(gormClient *gorm.DB) IncidentUserRepository {
|
||||
|
||||
50
repository/requestStatus/request_status_repository_impl.go
Normal file
50
repository/requestStatus/request_status_repository_impl.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package requestStatus
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
requestStatusUtil "houston/common/util/requestStatus"
|
||||
"houston/model/requestStatus"
|
||||
)
|
||||
|
||||
type requestStatusRepositoryImpl struct {
|
||||
gormClient *gorm.DB
|
||||
}
|
||||
|
||||
func (repo *requestStatusRepositoryImpl) AddRequestStatus(
|
||||
requestType requestStatusUtil.RequestType, status requestStatusUtil.RequestStatus, message datatypes.JSON,
|
||||
) (requestStatus.RequestStatusEntity, error) {
|
||||
entity := requestStatus.RequestStatusEntity{
|
||||
RequestType: string(requestType),
|
||||
Status: status.Value(),
|
||||
Message: message,
|
||||
}
|
||||
result := repo.gormClient.Create(&entity)
|
||||
return entity, result.Error
|
||||
}
|
||||
|
||||
func (repo *requestStatusRepositoryImpl) GetRequestStatusByRequestId(requestId uuid.UUID) (*requestStatus.RequestStatusEntity, error) {
|
||||
var entity requestStatus.RequestStatusEntity
|
||||
result := repo.gormClient.Where("request_id = ?", requestId).First(&entity)
|
||||
return &entity, result.Error
|
||||
}
|
||||
|
||||
func (repo *requestStatusRepositoryImpl) UpdateRequestStatus(
|
||||
requestId uuid.UUID, status requestStatusUtil.RequestStatus, message datatypes.JSON,
|
||||
) (requestStatus.RequestStatusEntity, error) {
|
||||
entity := requestStatus.RequestStatusEntity{
|
||||
Status: status.Value(),
|
||||
Message: message,
|
||||
}
|
||||
result := repo.gormClient.Model(&requestStatus.RequestStatusEntity{}).Where("request_id = ?", requestId).Updates(&entity)
|
||||
return entity, result.Error
|
||||
}
|
||||
|
||||
func (repo *requestStatusRepositoryImpl) GetAllRequestStatusesByTerminalState(terminalState bool) ([]requestStatus.RequestStatusEntity, error) {
|
||||
var entities []requestStatus.RequestStatusEntity
|
||||
|
||||
result := repo.gormClient.Where("status IN ?", requestStatusUtil.GetAllStatusValuesByTerminalState(terminalState)).Find(&entities)
|
||||
|
||||
return entities, result.Error
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package requestStatus
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
requestStatusUtil "houston/common/util/requestStatus"
|
||||
"houston/model/requestStatus"
|
||||
)
|
||||
|
||||
type RequestStatusRepository interface {
|
||||
AddRequestStatus(
|
||||
requestType requestStatusUtil.RequestType, status requestStatusUtil.RequestStatus, message datatypes.JSON,
|
||||
) (requestStatus.RequestStatusEntity, error)
|
||||
GetRequestStatusByRequestId(requestId uuid.UUID) (*requestStatus.RequestStatusEntity, error)
|
||||
UpdateRequestStatus(requestId uuid.UUID, status requestStatusUtil.RequestStatus, message datatypes.JSON) (requestStatus.RequestStatusEntity, error)
|
||||
GetAllRequestStatusesByTerminalState(terminalState bool) ([]requestStatus.RequestStatusEntity, error)
|
||||
}
|
||||
|
||||
func NewRequestStatusRepository(gormClient *gorm.DB) RequestStatusRepository {
|
||||
return &requestStatusRepositoryImpl{
|
||||
gormClient: gormClient,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user