feat(BE): enhance chickin and transfer laying services with product warehouse validation and stockable support

This commit is contained in:
aguhh18
2026-01-07 14:02:39 +07:00
parent 9336289573
commit 375e057e7c
3 changed files with 68 additions and 28 deletions
@@ -301,7 +301,9 @@ func (s *transferLayingService) CreateOne(c *fiber.Ctx, req *validation.Create)
}
for _, targetDetail := range req.TargetKandangs {
var firstTargetProductWarehouseID *uint
for i, targetDetail := range req.TargetKandangs {
targetPFK, err := s.ProjectFlockKandangRepo.GetByID(c.Context(), targetDetail.ProjectFlockKandangId)
if err != nil {
@@ -316,30 +318,40 @@ func (s *transferLayingService) CreateOne(c *fiber.Ctx, req *validation.Create)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get target warehouse")
}
var targetPW entity.ProductWarehouse
err = dbTransaction.Where("warehouse_id = ? AND project_flock_kandang_id = ?", targetWarehouse.Id, targetDetail.ProjectFlockKandangId).
First(&targetPW).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No product warehouse found for target kandang %d in warehouse %d", targetDetail.ProjectFlockKandangId, targetWarehouse.Id))
}
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get product warehouse for target kandang %d: %v", targetDetail.ProjectFlockKandangId, err))
}
target := entity.LayingTransferTarget{
LayingTransferId: createBody.Id,
TargetProjectFlockKandangId: targetDetail.ProjectFlockKandangId,
Qty: targetDetail.Quantity,
ProductWarehouseId: &targetWarehouse.Id,
ProductWarehouseId: &targetPW.Id,
}
if err := dbTransaction.Create(&target).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create transfer target")
}
if i == 0 {
firstTargetProductWarehouseID = &targetPW.Id
}
}
// Set DestProductWarehouseID untuk STOCKABLE role (ambil dari target pertama)
if len(req.TargetKandangs) > 0 {
firstTargetPWID := req.TargetKandangs[0].ProjectFlockKandangId
// Cari ProductWarehouse untuk target kandang
targetWarehouse, _ := s.WarehouseRepo.GetLatestByKandangID(c.Context(), firstTargetPWID)
if targetWarehouse != nil {
// Query ProductWarehouse by warehouse and kandang
var targetPW entity.ProductWarehouse
err := dbTransaction.Where("warehouse_id = ? AND project_flock_kandang_id = ?", targetWarehouse.Id, firstTargetPWID).
First(&targetPW).Error
if err == nil {
createBody.DestProductWarehouseID = &targetPW.Id
}
if firstTargetProductWarehouseID != nil {
createBody.DestProductWarehouseID = firstTargetProductWarehouseID
// Update DestProductWarehouseID ke database
if err := dbTransaction.Model(&entity.LayingTransfer{}).
Where("id = ?", createBody.Id).
Update("dest_product_warehouse_id", *firstTargetProductWarehouseID).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update DestProductWarehouseID")
}
}
@@ -504,11 +516,21 @@ func (s *transferLayingService) UpdateOne(c *fiber.Ctx, req *validation.Update,
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get target warehouse")
}
var targetPW entity.ProductWarehouse
err = dbTransaction.Where("warehouse_id = ? AND project_flock_kandang_id = ?", targetWarehouse.Id, targetDetail.ProjectFlockKandangId).
First(&targetPW).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No product warehouse found for target kandang %d in warehouse %d", targetDetail.ProjectFlockKandangId, targetWarehouse.Id))
}
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get product warehouse for target kandang %d: %v", targetDetail.ProjectFlockKandangId, err))
}
target := entity.LayingTransferTarget{
LayingTransferId: id,
TargetProjectFlockKandangId: targetDetail.ProjectFlockKandangId,
Qty: targetDetail.Quantity,
ProductWarehouseId: &targetWarehouse.Id,
ProductWarehouseId: &targetPW.Id,
}
if err := dbTransaction.Create(&target).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create transfer target")