From 7d3602d829b30400430ca9020aa4aaf42276bc5d Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Sat, 17 Jan 2026 13:22:01 +0700 Subject: [PATCH] feat[BE]: Enhance CreateOne method to validate project flock closing status and handle warehouse without kandang_id --- .../transfers/services/transfer.service.go | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/internal/modules/inventory/transfers/services/transfer.service.go b/internal/modules/inventory/transfers/services/transfer.service.go index 3f12b444..1c085976 100644 --- a/internal/modules/inventory/transfers/services/transfer.service.go +++ b/internal/modules/inventory/transfers/services/transfer.service.go @@ -159,12 +159,15 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques return nil, err } - projectFlockKandang, err := s.ProjectFlockKandangRepo.GetByID(c.Context(), destPfkID) - if err != nil { - return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data project flock") - } - if projectFlockKandang.ClosedAt != nil { - return nil, fiber.NewError(fiber.StatusBadRequest, "Project flock tujuan sudah closing") + // Hanya validasi closing jika ada project flock kandang (warehouse punya kandang_id) + if destPfkID > 0 { + projectFlockKandang, err := s.ProjectFlockKandangRepo.GetByID(c.Context(), destPfkID) + if err != nil { + return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data project flock") + } + if projectFlockKandang.ClosedAt != nil { + return nil, fiber.NewError(fiber.StatusBadRequest, "Project flock tujuan sudah closing") + } } actorID, err := m.ActorIDFromContext(c) @@ -256,11 +259,18 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques if err != nil { return err } + + // Set ProjectFlockKandangId hanya jika ada kandang + var pfkID *uint + if projectFlockKandangID > 0 { + pfkID = &projectFlockKandangID + } + destPW = &entity.ProductWarehouse{ ProductId: uint(product.ProductID), WarehouseId: uint(req.DestinationWarehouseID), Quantity: 0, - ProjectFlockKandangId: &projectFlockKandangID, + ProjectFlockKandangId: pfkID, } if err := productWarehouseRepoTX.CreateOne(c.Context(), destPW, nil); err != nil { return fiber.NewError(fiber.StatusInternalServerError, "Gagal membuat product warehouse destination") @@ -477,8 +487,9 @@ func (s *transferService) getActiveProjectFlockKandangID(ctx context.Context, wa return 0, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data gudang") } + // Jika warehouse tidak punya kandang_id, return 0 tanpa error if warehouse.KandangId == nil || *warehouse.KandangId == 0 { - return 0, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Gudang %d belum terhubung ke kandang", warehouseID)) + return 0, nil } projectFlockKandang, err := s.ProjectFlockKandangRepo.GetActiveByKandangID(ctx, uint(*warehouse.KandangId))