mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
feat(BE-281): adjustment uniformity for make unique for week,projectflockandang, and date
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||||
rUniformity "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
rUniformity "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
||||||
sUniformity "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/services"
|
sUniformity "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/services"
|
||||||
|
|
||||||
@@ -24,6 +25,7 @@ func (UniformityModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validat
|
|||||||
uniformityRepo := rUniformity.NewUniformityRepository(db)
|
uniformityRepo := rUniformity.NewUniformityRepository(db)
|
||||||
documentRepo := commonRepo.NewDocumentRepository(db)
|
documentRepo := commonRepo.NewDocumentRepository(db)
|
||||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
|
projectFlockKandangRepo := rProjectFlock.NewProjectFlockKandangRepository(db)
|
||||||
userRepo := rUser.NewUserRepository(db)
|
userRepo := rUser.NewUserRepository(db)
|
||||||
|
|
||||||
documentSvc, err := commonSvc.NewDocumentServiceFromConfig(context.Background(), documentRepo)
|
documentSvc, err := commonSvc.NewDocumentServiceFromConfig(context.Background(), documentRepo)
|
||||||
@@ -36,7 +38,7 @@ func (UniformityModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validat
|
|||||||
panic(fmt.Sprintf("failed to register uniformity approval workflow: %v", err))
|
panic(fmt.Sprintf("failed to register uniformity approval workflow: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
uniformityService := sUniformity.NewUniformityService(uniformityRepo, documentSvc, approvalRepo, approvalSvc, validate)
|
uniformityService := sUniformity.NewUniformityService(uniformityRepo, documentSvc, approvalRepo, approvalSvc, projectFlockKandangRepo, validate)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
UniformityRoutes(router, userService, uniformityService)
|
UniformityRoutes(router, userService, uniformityService)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/validations"
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/validations"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
@@ -39,12 +40,13 @@ type UniformityService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type uniformityService struct {
|
type uniformityService struct {
|
||||||
Log *logrus.Logger
|
Log *logrus.Logger
|
||||||
Validate *validator.Validate
|
Validate *validator.Validate
|
||||||
Repository repository.UniformityRepository
|
Repository repository.UniformityRepository
|
||||||
DocumentSvc commonSvc.DocumentService
|
DocumentSvc commonSvc.DocumentService
|
||||||
ApprovalRepo commonRepo.ApprovalRepository
|
ApprovalRepo commonRepo.ApprovalRepository
|
||||||
ApprovalSvc commonSvc.ApprovalService
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
|
ProjectFlockKandangRepo rProjectFlock.ProjectFlockKandangRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUniformityService(
|
func NewUniformityService(
|
||||||
@@ -52,15 +54,17 @@ func NewUniformityService(
|
|||||||
documentSvc commonSvc.DocumentService,
|
documentSvc commonSvc.DocumentService,
|
||||||
approvalRepo commonRepo.ApprovalRepository,
|
approvalRepo commonRepo.ApprovalRepository,
|
||||||
approvalSvc commonSvc.ApprovalService,
|
approvalSvc commonSvc.ApprovalService,
|
||||||
|
projectFlockKandangRepo rProjectFlock.ProjectFlockKandangRepository,
|
||||||
validate *validator.Validate,
|
validate *validator.Validate,
|
||||||
) UniformityService {
|
) UniformityService {
|
||||||
return &uniformityService{
|
return &uniformityService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
Validate: validate,
|
Validate: validate,
|
||||||
Repository: repo,
|
Repository: repo,
|
||||||
DocumentSvc: documentSvc,
|
DocumentSvc: documentSvc,
|
||||||
ApprovalRepo: approvalRepo,
|
ApprovalRepo: approvalRepo,
|
||||||
ApprovalSvc: approvalSvc,
|
ApprovalSvc: approvalSvc,
|
||||||
|
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,6 +125,9 @@ func (s *uniformityService) CreateOne(c *fiber.Ctx, req *validation.Create, file
|
|||||||
if err := s.Validate.Struct(req); err != nil {
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if s.ProjectFlockKandangRepo == nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Project flock kandang repository not available")
|
||||||
|
}
|
||||||
|
|
||||||
if file == nil {
|
if file == nil {
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "document is required")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "document is required")
|
||||||
@@ -131,6 +138,16 @@ func (s *uniformityService) CreateOne(c *fiber.Ctx, req *validation.Create, file
|
|||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "date must be in YYYY-MM-DD format")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "date must be in YYYY-MM-DD format")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := commonSvc.EnsureRelations(
|
||||||
|
c.Context(),
|
||||||
|
commonSvc.RelationCheck{Name: "Project Flock Kandang", ID: &req.ProjectFlockKandangId, Exists: s.ProjectFlockKandangRepo.IdExists},
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := s.ensureUniqueUniformity(c.Context(), 0, req.ProjectFlockKandangId, req.Week, &uniformDate); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if len(rows) == 0 {
|
if len(rows) == 0 {
|
||||||
parsedRows, err := s.ParseBodyWeightExcel(c, file)
|
parsedRows, err := s.ParseBodyWeightExcel(c, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -212,6 +229,7 @@ func (s uniformityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id ui
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateBody := make(map[string]any)
|
updateBody := make(map[string]any)
|
||||||
|
var uniformDate *time.Time
|
||||||
|
|
||||||
if req.Date != nil {
|
if req.Date != nil {
|
||||||
parsed, err := time.Parse("2006-01-02", *req.Date)
|
parsed, err := time.Parse("2006-01-02", *req.Date)
|
||||||
@@ -219,14 +237,51 @@ func (s uniformityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id ui
|
|||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "date must be in YYYY-MM-DD format")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "date must be in YYYY-MM-DD format")
|
||||||
}
|
}
|
||||||
updateBody["uniform_date"] = parsed
|
updateBody["uniform_date"] = parsed
|
||||||
|
uniformDate = &parsed
|
||||||
}
|
}
|
||||||
if req.ProjectFlockKandangId != nil {
|
if req.ProjectFlockKandangId != nil {
|
||||||
|
if s.ProjectFlockKandangRepo == nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Project flock kandang repository not available")
|
||||||
|
}
|
||||||
|
if err := commonSvc.EnsureRelations(
|
||||||
|
c.Context(),
|
||||||
|
commonSvc.RelationCheck{Name: "Project Flock Kandang", ID: req.ProjectFlockKandangId, Exists: s.ProjectFlockKandangRepo.IdExists},
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
updateBody["project_flock_kandang_id"] = *req.ProjectFlockKandangId
|
updateBody["project_flock_kandang_id"] = *req.ProjectFlockKandangId
|
||||||
}
|
}
|
||||||
if req.Week != nil {
|
if req.Week != nil {
|
||||||
updateBody["week"] = *req.Week
|
updateBody["week"] = *req.Week
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.Date != nil || req.ProjectFlockKandangId != nil || req.Week != nil {
|
||||||
|
current, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Uniformity not found")
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
targetDate := uniformDate
|
||||||
|
if targetDate == nil {
|
||||||
|
targetDate = current.UniformDate
|
||||||
|
}
|
||||||
|
targetWeek := current.Week
|
||||||
|
if req.Week != nil {
|
||||||
|
targetWeek = *req.Week
|
||||||
|
}
|
||||||
|
targetPFKID := current.ProjectFlockKandangId
|
||||||
|
if req.ProjectFlockKandangId != nil {
|
||||||
|
targetPFKID = *req.ProjectFlockKandangId
|
||||||
|
}
|
||||||
|
if targetDate != nil {
|
||||||
|
if err := s.ensureUniqueUniformity(c.Context(), id, targetPFKID, targetWeek, targetDate); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if file != nil {
|
if file != nil {
|
||||||
if s.DocumentSvc == nil {
|
if s.DocumentSvc == nil {
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Document service not available")
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Document service not available")
|
||||||
@@ -331,6 +386,28 @@ func (s uniformityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id ui
|
|||||||
return s.GetOne(c, id)
|
return s.GetOne(c, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *uniformityService) ensureUniqueUniformity(ctx context.Context, id uint, projectFlockKandangID uint, week int, uniformDate *time.Time) error {
|
||||||
|
if projectFlockKandangID == 0 || week == 0 || uniformDate == nil || uniformDate.IsZero() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
query := s.Repository.DB().WithContext(ctx).
|
||||||
|
Model(&entity.ProjectFlockKandangUniformity{}).
|
||||||
|
Where("project_flock_kandang_id = ? AND week = ? AND uniform_date = ?", projectFlockKandangID, week, *uniformDate)
|
||||||
|
if id != 0 {
|
||||||
|
query = query.Where("id <> ?", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
if err := query.Count(&count).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to validate uniformity uniqueness")
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
return fiber.NewError(fiber.StatusConflict, "Uniformity already exists for the same project flock kandang, week, and date")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s uniformityService) DeleteOne(c *fiber.Ctx, id uint) error {
|
func (s uniformityService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
|||||||
Reference in New Issue
Block a user