diff --git a/internal/modules/master/warehouses/services/warehouse.service.go b/internal/modules/master/warehouses/services/warehouse.service.go index 7eeaad3d..0b9dfc18 100644 --- a/internal/modules/master/warehouses/services/warehouse.service.go +++ b/internal/modules/master/warehouses/services/warehouse.service.go @@ -110,7 +110,11 @@ func (s *warehouseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent } typ := strings.ToUpper(req.Type) - if err := validateWarehouseTypeRequirements(typ, &req.AreaId, req.LocationId, req.KandangId); err != nil { + createValidationOpts := WarehouseTypeValidationOptions{ + LocationProvided: req.LocationId != nil, + KandangProvided: req.KandangId != nil, + } + if err := validateWarehouseTypeRequirements(typ, &req.AreaId, &req.LocationId, &req.KandangId, createValidationOpts); err != nil { return nil, err } @@ -208,9 +212,22 @@ func (s warehouseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin finalKandangId = req.KandangId } - if err := validateWarehouseTypeRequirements(finalType, &finalAreaId, finalLocationId, finalKandangId); err != nil { + originalLocationId := finalLocationId + originalKandangId := finalKandangId + updateValidationOpts := WarehouseTypeValidationOptions{ + AutoClear: true, + LocationProvided: req.LocationId != nil, + KandangProvided: req.KandangId != nil, + } + if err := validateWarehouseTypeRequirements(finalType, &finalAreaId, &finalLocationId, &finalKandangId, updateValidationOpts); err != nil { return nil, err } + if originalLocationId != finalLocationId { + updateBody["location_id"] = nil + } + if originalKandangId != finalKandangId { + updateBody["kandang_id"] = nil + } if len(updateBody) == 0 { return s.GetOne(c, id) @@ -238,47 +255,65 @@ func (s warehouseService) DeleteOne(c *fiber.Ctx, id uint) error { return nil } -func validateWarehouseTypeRequirements(typ string, areaID *uint, locationID *uint, kandangID *uint) error { +type WarehouseTypeValidationOptions struct { + AutoClear bool + LocationProvided bool + KandangProvided bool +} + +func validateWarehouseTypeRequirements(typ string, areaID *uint, locationID **uint, kandangID **uint, opts WarehouseTypeValidationOptions) error { switch utils.WarehouseType(typ) { case utils.WarehouseTypeArea: if areaID == nil || *areaID == 0 { return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is AREA") } - if locationID != nil { - return fiber.NewError(fiber.StatusBadRequest, "location_id must not be provided when type is AREA") + if locationID != nil && *locationID != nil { + if opts.AutoClear && !opts.LocationProvided { + *locationID = nil + } else { + return fiber.NewError(fiber.StatusBadRequest, "location_id must not be provided when type is AREA") + } } - if kandangID != nil { - return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is AREA") + if kandangID != nil && *kandangID != nil { + if opts.AutoClear && !opts.KandangProvided { + *kandangID = nil + } else { + return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is AREA") + } } return nil case utils.WarehouseTypeLokasi: if areaID == nil || *areaID == 0 { return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is LOCATION") } - if locationID == nil { + if locationID == nil || *locationID == nil { return fiber.NewError(fiber.StatusBadRequest, "location_id is required when type is LOCATION") } - if *locationID == 0 { + if **locationID == 0 { return fiber.NewError(fiber.StatusBadRequest, "location_id must be greater than 0 when type is LOCATION") } - if kandangID != nil { - return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is LOCATION") + if kandangID != nil && *kandangID != nil { + if opts.AutoClear && !opts.KandangProvided { + *kandangID = nil + } else { + return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is LOCATION") + } } return nil case utils.WarehouseTypeKandang: if areaID == nil || *areaID == 0 { return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is KANDANG") } - if locationID == nil { + if locationID == nil || *locationID == nil { return fiber.NewError(fiber.StatusBadRequest, "location_id is required when type is KANDANG") } - if *locationID == 0 { + if **locationID == 0 { return fiber.NewError(fiber.StatusBadRequest, "location_id must be greater than 0 when type is KANDANG") } - if kandangID == nil { + if kandangID == nil || *kandangID == nil { return fiber.NewError(fiber.StatusBadRequest, "kandang_id is required when type is KANDANG") } - if *kandangID == 0 { + if **kandangID == 0 { return fiber.NewError(fiber.StatusBadRequest, "kandang_id must be greater than 0 when type is KANDANG") } return nil