mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
fix(BE): warehouse provided location
This commit is contained in:
@@ -110,7 +110,11 @@ func (s *warehouseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
|||||||
}
|
}
|
||||||
|
|
||||||
typ := strings.ToUpper(req.Type)
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,9 +212,22 @@ func (s warehouseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
|||||||
finalKandangId = req.KandangId
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if originalLocationId != finalLocationId {
|
||||||
|
updateBody["location_id"] = nil
|
||||||
|
}
|
||||||
|
if originalKandangId != finalKandangId {
|
||||||
|
updateBody["kandang_id"] = nil
|
||||||
|
}
|
||||||
|
|
||||||
if len(updateBody) == 0 {
|
if len(updateBody) == 0 {
|
||||||
return s.GetOne(c, id)
|
return s.GetOne(c, id)
|
||||||
@@ -238,47 +255,65 @@ func (s warehouseService) DeleteOne(c *fiber.Ctx, id uint) error {
|
|||||||
return nil
|
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) {
|
switch utils.WarehouseType(typ) {
|
||||||
case utils.WarehouseTypeArea:
|
case utils.WarehouseTypeArea:
|
||||||
if areaID == nil || *areaID == 0 {
|
if areaID == nil || *areaID == 0 {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is AREA")
|
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is AREA")
|
||||||
}
|
}
|
||||||
if locationID != nil {
|
if locationID != nil && *locationID != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "location_id must not be provided when type is AREA")
|
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 {
|
if kandangID != nil && *kandangID != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is AREA")
|
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
|
return nil
|
||||||
case utils.WarehouseTypeLokasi:
|
case utils.WarehouseTypeLokasi:
|
||||||
if areaID == nil || *areaID == 0 {
|
if areaID == nil || *areaID == 0 {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is LOCATION")
|
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")
|
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")
|
return fiber.NewError(fiber.StatusBadRequest, "location_id must be greater than 0 when type is LOCATION")
|
||||||
}
|
}
|
||||||
if kandangID != nil {
|
if kandangID != nil && *kandangID != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is LOCATION")
|
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
|
return nil
|
||||||
case utils.WarehouseTypeKandang:
|
case utils.WarehouseTypeKandang:
|
||||||
if areaID == nil || *areaID == 0 {
|
if areaID == nil || *areaID == 0 {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is KANDANG")
|
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")
|
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")
|
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")
|
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 fiber.NewError(fiber.StatusBadRequest, "kandang_id must be greater than 0 when type is KANDANG")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user