mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
feat[BE]: Refactor Chickin create and approvals support chickin growing and chickin laying, and create get one project flock kandang API
This commit is contained in:
@@ -15,8 +15,8 @@ import (
|
||||
// === DTO Structs (ordered) ===
|
||||
|
||||
type ChickinBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
ProjectFlockKandang *ProjectFlockKandangDTO `json:"project_flock_kandang"`
|
||||
Id uint `json:"id"`
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||
ChickInDate time.Time `json:"chick_in_date"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id"`
|
||||
UsageQty float64 `json:"usage_qty"`
|
||||
@@ -55,10 +55,9 @@ type ChickinSimpleDTO struct {
|
||||
|
||||
type ChickinListDTO struct {
|
||||
ChickinBaseDTO
|
||||
ProjectFlockKandang *ProjectFlockKandangDTO `json:"project_flock_kandang"`
|
||||
CreatedUser *userBaseDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CreatedUser *userBaseDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type ChickinDetailDTO struct {
|
||||
@@ -141,14 +140,17 @@ func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangD
|
||||
}
|
||||
|
||||
func ToChickinBaseDTO(e entity.ProjectChickin) ChickinBaseDTO {
|
||||
var pfk *ProjectFlockKandangDTO
|
||||
var projectFlockKandangId uint
|
||||
// Check if ProjectFlockKandang relation is loaded
|
||||
if e.ProjectFlockKandang != nil && e.ProjectFlockKandang.Id != 0 {
|
||||
mapped := ToProjectFlockKandangDTO(*e.ProjectFlockKandang)
|
||||
pfk = &mapped
|
||||
projectFlockKandangId = e.ProjectFlockKandang.Id
|
||||
} else if e.ProjectFlockKandangId != 0 {
|
||||
// If relation is not loaded but ID is available, use the ID
|
||||
projectFlockKandangId = e.ProjectFlockKandangId
|
||||
}
|
||||
return ChickinBaseDTO{
|
||||
Id: e.Id,
|
||||
ProjectFlockKandang: pfk,
|
||||
ProjectFlockKandangId: projectFlockKandangId,
|
||||
ChickInDate: e.ChickInDate,
|
||||
ProductWarehouseId: e.ProductWarehouseId,
|
||||
UsageQty: e.UsageQty,
|
||||
@@ -176,17 +178,11 @@ func ToChickinListDTO(e entity.ProjectChickin) ChickinListDTO {
|
||||
mapped := userBaseDTO.ToUserBaseDTO(*e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
var pfk *ProjectFlockKandangDTO
|
||||
if e.ProjectFlockKandang != nil && e.ProjectFlockKandang.Id != 0 {
|
||||
mapped := ToProjectFlockKandangDTO(*e.ProjectFlockKandang)
|
||||
pfk = &mapped
|
||||
}
|
||||
return ChickinListDTO{
|
||||
ChickinBaseDTO: ToChickinBaseDTO(e),
|
||||
ProjectFlockKandang: pfk,
|
||||
CreatedUser: createdUser,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
ChickinBaseDTO: ToChickinBaseDTO(e),
|
||||
CreatedUser: createdUser,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,21 +11,24 @@ import (
|
||||
type ProjectChickinRepository interface {
|
||||
repository.BaseRepository[entity.ProjectChickin]
|
||||
GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.ProjectChickin, error)
|
||||
GetByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) ([]entity.ProjectChickin, error)
|
||||
}
|
||||
|
||||
type ChickinRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.ProjectChickin]
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewChickinRepository(db *gorm.DB) ProjectChickinRepository {
|
||||
return &ChickinRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectChickin](db),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ChickinRepositoryImpl) GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.ProjectChickin, error) {
|
||||
var chickin entity.ProjectChickin
|
||||
err := r.DB().WithContext(ctx).
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("project_floc_id = ?", projectFlockID).
|
||||
Where("deleted_at IS NULL").
|
||||
First(&chickin).Error
|
||||
@@ -34,3 +37,15 @@ func (r *ChickinRepositoryImpl) GetFirstByProjectFlockID(ctx context.Context, pr
|
||||
}
|
||||
return &chickin, nil
|
||||
}
|
||||
|
||||
func (r *ChickinRepositoryImpl) GetByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) ([]entity.ProjectChickin, error) {
|
||||
var chickins []entity.ProjectChickin
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Order("created_at DESC").
|
||||
Find(&chickins).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return chickins, nil
|
||||
}
|
||||
|
||||
@@ -125,13 +125,21 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
}
|
||||
|
||||
var productWarehouses []entity.ProductWarehouse
|
||||
category := strings.ToUpper(strings.TrimSpace(projectFlockKandang.ProjectFlock.Category))
|
||||
|
||||
if strings.ToUpper(strings.TrimSpace(projectFlockKandang.ProjectFlock.Category)) == string(utils.ProjectFlockCategoryGrowing) {
|
||||
var productCategoryCode string
|
||||
switch category {
|
||||
case string(utils.ProjectFlockCategoryGrowing):
|
||||
productCategoryCode = "DOC"
|
||||
case string(utils.ProjectFlockCategoryLaying):
|
||||
productCategoryCode = "PULLET"
|
||||
default:
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Unknown category: %s", category))
|
||||
}
|
||||
|
||||
productWarehouses, err = s.ProductWarehouseRepo.GetByCategoryCodeAndWarehouseID(c.Context(), "DOC", warehouse.Id)
|
||||
if err != nil || len(productWarehouses) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Product for growing category in the Kandang's warehouse not found")
|
||||
}
|
||||
productWarehouses, err = s.ProductWarehouseRepo.GetByCategoryCodeAndWarehouseID(c.Context(), productCategoryCode, warehouse.Id)
|
||||
if err != nil || len(productWarehouses) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Product for %s category in the Kandang's warehouse not found", strings.ToLower(category)))
|
||||
}
|
||||
|
||||
chickinDate, err := utils.ParseDateString(req.ChickInDate)
|
||||
@@ -142,20 +150,17 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
actorID := uint(1) // todo nanti ambil dari auth context
|
||||
newChikins := make([]*entity.ProjectChickin, 0)
|
||||
for _, productWarehouse := range productWarehouses {
|
||||
|
||||
if productWarehouse.Quantity > 0 {
|
||||
newChickin := &entity.ProjectChickin{
|
||||
ProjectFlockKandangId: req.ProjectFlockKandangId,
|
||||
ChickInDate: chickinDate,
|
||||
UsageQty: 0,
|
||||
PendingUsageQty: productWarehouse.Quantity,
|
||||
ProductWarehouseId: productWarehouse.Id,
|
||||
Notes: req.Note,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
newChikins = append(newChikins, newChickin)
|
||||
newChickin := &entity.ProjectChickin{
|
||||
ProjectFlockKandangId: req.ProjectFlockKandangId,
|
||||
ChickInDate: chickinDate,
|
||||
UsageQty: 0,
|
||||
PendingUsageQty: productWarehouse.Quantity,
|
||||
ProductWarehouseId: productWarehouse.Id,
|
||||
Notes: req.Note,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
newChikins = append(newChikins, newChickin)
|
||||
}
|
||||
|
||||
if len(newChikins) == 0 {
|
||||
@@ -219,7 +224,6 @@ func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
updateBody["chick_in_date"] = req.ChickInDate
|
||||
}
|
||||
if req.Note != "" {
|
||||
// entity uses `Notes` => column `notes`
|
||||
updateBody["notes"] = req.Note
|
||||
}
|
||||
if len(updateBody) == 0 {
|
||||
@@ -239,7 +243,6 @@ func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
|
||||
func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
|
||||
// Simplified delete: directly call repository delete. Complex restore logic removed for now.
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
||||
@@ -254,7 +257,6 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
actorID := uint(1) // todo nanti ambil dari auth context
|
||||
|
||||
var action entity.ApprovalAction
|
||||
@@ -272,26 +274,40 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "approvable_ids must contain at least one id")
|
||||
}
|
||||
|
||||
// Validate all ProjectFlockKandang IDs exist and have valid approval status
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(s.Repository.DB()))
|
||||
for _, id := range approvableIDs {
|
||||
idCopy := id
|
||||
if err := commonSvc.EnsureRelations(c.Context(), commonSvc.RelationCheck{Name: "ProjectFlockKandang", ID: &idCopy, Exists: s.ProjectflockKandangRepo.IdExists}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check latest approval status - must be PENGAJUAN to be approvable
|
||||
latestApproval, err := approvalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, id, nil)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
|
||||
}
|
||||
if latestApproval == nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No approval found for ProjectFlockKandang %d - chickins must be created first", id))
|
||||
}
|
||||
if latestApproval.StepNumber != uint16(utils.ProjectFlockKandangStepPengajuan) {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("ProjectFlockKandang %d cannot be approved - current status is not in PENGAJUAN stage", id))
|
||||
}
|
||||
}
|
||||
|
||||
step := utils.ProjectFlockKandangStepPengajuan
|
||||
if action == entity.ApprovalActionApproved {
|
||||
step = utils.ProjectFlockKandangStepDisetujui
|
||||
}
|
||||
|
||||
err := s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
ProjectFlockPopulationRepotx := s.ProjectflockPopulationRepo.WithTx(dbTransaction)
|
||||
chickinRepoTx := s.Repository.WithTx(dbTransaction)
|
||||
chickinRepoTx := repository.NewChickinRepository(dbTransaction)
|
||||
productWarehouseTx := s.ProductWarehouseRepo.WithTx(dbTransaction)
|
||||
|
||||
for _, approvableID := range approvableIDs {
|
||||
|
||||
exists, err := s.ProjectflockKandangRepo.WithTx(dbTransaction).IdExists(c.Context(), approvableID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("ProjectFlockKandang %d not found", approvableID))
|
||||
}
|
||||
|
||||
if _, err := approvalSvc.CreateApproval(
|
||||
c.Context(),
|
||||
utils.ApprovalWorkflowProjectFlockKandang,
|
||||
@@ -311,31 +327,54 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
|
||||
|
||||
if action == entity.ApprovalActionApproved {
|
||||
|
||||
var chickins []entity.ProjectChickin
|
||||
if err := chickinRepoTx.DB().WithContext(c.Context()).Where("project_flock_kandang_id = ?", approvableID).Find(&chickins).Error; err != nil {
|
||||
chickins, err := chickinRepoTx.GetByProjectFlockKandangID(c.Context(), approvableID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, chickin := range chickins {
|
||||
population := &entity.ProjectFlockPopulation{
|
||||
ProjectChickinId: chickin.Id,
|
||||
ProductWarehouseId: chickin.ProductWarehouseId,
|
||||
TotalQty: chickin.PendingUsageQty,
|
||||
TotalUsedQty: 0,
|
||||
Notes: chickin.Notes,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
if err := ProjectFlockPopulationRepotx.CreateOne(c.Context(), population, nil); err != nil {
|
||||
lower := strings.ToLower(err.Error())
|
||||
if !(strings.Contains(lower, "duplicate") || strings.Contains(lower, "unique constraint") || strings.Contains(lower, "23505")) {
|
||||
return err
|
||||
}
|
||||
s.Log.Infof("ignored duplicate population for chickin %d: %v", chickin.Id, err)
|
||||
kandangForApproval, err := s.ProjectflockKandangRepo.GetByID(c.Context(), approvableID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("ProjectFlockKandang %d not found", approvableID))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), kandangForApproval.KandangId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Warehouse for kandang %d not found", kandangForApproval.KandangId))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
pulletPW, err := s.getOrCreatePulletProductWarehouse(c, warehouse.Id, dbTransaction, actorID)
|
||||
if err != nil {
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if err := s.convertChickinsToPullet(c, chickins, pulletPW, dbTransaction, actorID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
} else if action == entity.ApprovalActionRejected {
|
||||
|
||||
chickins, err := chickinRepoTx.GetByProjectFlockKandangID(c.Context(), approvableID)
|
||||
if err != nil {
|
||||
s.Log.Warnf("failed to get chickins for rejection %d: %v", approvableID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, chickin := range chickins {
|
||||
|
||||
updates := map[string]any{"quantity": chickin.PendingUsageQty}
|
||||
|
||||
if err := productWarehouseTx.PatchOne(c.Context(), chickin.ProductWarehouseId, updates, nil); err != nil {
|
||||
s.Log.Warnf("failed to restore product warehouse quantity for rejection: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -364,6 +403,93 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func (s *chickinService) getOrCreatePulletProductWarehouse(ctx *fiber.Ctx, warehouseId uint, dbTransaction *gorm.DB, actorID uint) (*entity.ProductWarehouse, error) {
|
||||
|
||||
pulletProducts, err := s.ProductWarehouseRepo.GetByCategoryCodeAndWarehouseID(ctx.Context(), "PULLET", warehouseId)
|
||||
if err == nil && len(pulletProducts) > 0 {
|
||||
return &pulletProducts[0], nil
|
||||
}
|
||||
|
||||
pulletProduct, err := s.ProductWarehouseRepo.GetFirstProductByCategoryCode(ctx.Context(), "PULLET")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get PULLET product: %w", err)
|
||||
}
|
||||
if pulletProduct == nil {
|
||||
return nil, fmt.Errorf("no PULLET product found in system")
|
||||
}
|
||||
|
||||
newPulletPW := &entity.ProductWarehouse{
|
||||
ProductId: pulletProduct.Id,
|
||||
WarehouseId: warehouseId,
|
||||
Quantity: 0,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
if err := s.ProductWarehouseRepo.WithTx(dbTransaction).CreateOne(ctx.Context(), newPulletPW, nil); err != nil {
|
||||
return nil, fmt.Errorf("failed to create PULLET product warehouse: %w", err)
|
||||
}
|
||||
|
||||
return newPulletPW, nil
|
||||
}
|
||||
|
||||
func (s *chickinService) convertChickinsToPullet(ctx *fiber.Ctx, chickins []entity.ProjectChickin, pulletPW *entity.ProductWarehouse, dbTransaction *gorm.DB, actorID uint) error {
|
||||
|
||||
if pulletPW == nil || pulletPW.Id == 0 {
|
||||
return fmt.Errorf("invalid PULLET product warehouse")
|
||||
}
|
||||
|
||||
chickinRepoTx := repository.NewChickinRepository(dbTransaction)
|
||||
productWarehouseTx := s.ProductWarehouseRepo.WithTx(dbTransaction)
|
||||
ProjectFlockPopulationRepotx := s.ProjectflockPopulationRepo.WithTx(dbTransaction)
|
||||
|
||||
for _, chickin := range chickins {
|
||||
|
||||
quantityToConvert := chickin.PendingUsageQty
|
||||
|
||||
if err := chickinRepoTx.PatchOne(ctx.Context(), chickin.Id, map[string]any{
|
||||
"usage_qty": quantityToConvert,
|
||||
"pending_usage_qty": 0,
|
||||
}, nil); err != nil {
|
||||
return fmt.Errorf("failed to update chickin %d qty: %w", chickin.Id, err)
|
||||
}
|
||||
|
||||
// Update quantity di PULLET product warehouse dengan quantity dari chickin
|
||||
if err := productWarehouseTx.PatchOne(ctx.Context(), pulletPW.Id, map[string]any{
|
||||
"quantity": gorm.Expr("quantity + ?", quantityToConvert),
|
||||
}, nil); err != nil {
|
||||
s.Log.Warnf("failed to update PULLET warehouse quantity: %v", err)
|
||||
}
|
||||
|
||||
populationExists, err := ProjectFlockPopulationRepotx.ExistsByProjectChickinID(ctx.Context(), chickin.Id)
|
||||
if err != nil {
|
||||
s.Log.Warnf("failed to check population existence for chickin %d: %v", chickin.Id, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if !populationExists {
|
||||
population := &entity.ProjectFlockPopulation{
|
||||
ProjectChickinId: chickin.Id,
|
||||
ProductWarehouseId: pulletPW.Id,
|
||||
TotalQty: quantityToConvert,
|
||||
TotalUsedQty: 0,
|
||||
Notes: chickin.Notes,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
if err := ProjectFlockPopulationRepotx.CreateOne(ctx.Context(), population, nil); err != nil {
|
||||
lower := strings.ToLower(err.Error())
|
||||
if !(strings.Contains(lower, "duplicate") || strings.Contains(lower, "unique constraint") || strings.Contains(lower, "23505")) {
|
||||
return err
|
||||
}
|
||||
s.Log.Infof("ignored duplicate population for chickin %d: %v", chickin.Id, err)
|
||||
}
|
||||
} else {
|
||||
s.Log.Infof("population already exists for chickin %d", chickin.Id)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func uniqueUintSlice(values []uint) []uint {
|
||||
seen := make(map[uint]struct{}, len(values))
|
||||
result := make([]uint, 0, len(values))
|
||||
|
||||
Reference in New Issue
Block a user