mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
Fix[BE]: use new request body for frontend requrement on chickin create API
This commit is contained in:
@@ -123,39 +123,34 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
|
|||||||
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse for Kandang not found")
|
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse for Kandang not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
var productWarehouses []entity.ProductWarehouse
|
|
||||||
category := strings.ToUpper(strings.TrimSpace(projectFlockKandang.ProjectFlock.Category))
|
category := strings.ToUpper(strings.TrimSpace(projectFlockKandang.ProjectFlock.Category))
|
||||||
|
|
||||||
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.GetByFlagAndWarehouseID(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)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid ChickInDate format")
|
|
||||||
}
|
|
||||||
|
|
||||||
actorID := uint(1) // todo nanti ambil dari auth context
|
actorID := uint(1) // todo nanti ambil dari auth context
|
||||||
newChikins := make([]*entity.ProjectChickin, 0)
|
newChikins := make([]*entity.ProjectChickin, 0)
|
||||||
for _, productWarehouse := range productWarehouses {
|
|
||||||
availableQty, err := s.calculateAvailableQuantity(c, req.ProjectFlockKandangId, &productWarehouse, category)
|
for _, chickinReq := range req.ChickinRequests {
|
||||||
|
|
||||||
|
productWarehouse, err := s.ProductWarehouseRepo.GetByID(c.Context(), chickinReq.ProductWarehouseId, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to calculate available quantity for product warehouse %d", productWarehouse.Id))
|
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Product warehouse %d not found", chickinReq.ProductWarehouseId))
|
||||||
|
}
|
||||||
|
|
||||||
|
if productWarehouse.WarehouseId != warehouse.Id {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Product warehouse %d is not bound to kandang's warehouse", chickinReq.ProductWarehouseId))
|
||||||
|
}
|
||||||
|
|
||||||
|
chickinDate, err := utils.ParseDateString(chickinReq.ChickInDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid ChickInDate format for product warehouse %d", chickinReq.ProductWarehouseId))
|
||||||
|
}
|
||||||
|
|
||||||
|
availableQty, err := s.calculateAvailableQuantity(c, req.ProjectFlockKandangId, productWarehouse, category)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to calculate available quantity for product warehouse %d", chickinReq.ProductWarehouseId))
|
||||||
}
|
}
|
||||||
|
|
||||||
if availableQty <= 0 {
|
if availableQty <= 0 {
|
||||||
continue
|
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No available quantity for product warehouse %d", chickinReq.ProductWarehouseId))
|
||||||
}
|
}
|
||||||
|
|
||||||
newChickin := &entity.ProjectChickin{
|
newChickin := &entity.ProjectChickin{
|
||||||
@@ -163,8 +158,8 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
|
|||||||
ChickInDate: chickinDate,
|
ChickInDate: chickinDate,
|
||||||
UsageQty: 0,
|
UsageQty: 0,
|
||||||
PendingUsageQty: availableQty,
|
PendingUsageQty: availableQty,
|
||||||
ProductWarehouseId: productWarehouse.Id,
|
ProductWarehouseId: chickinReq.ProductWarehouseId,
|
||||||
Notes: req.Note,
|
Notes: chickinReq.Note,
|
||||||
CreatedBy: actorID,
|
CreatedBy: actorID,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
package validation
|
package validation
|
||||||
|
|
||||||
type Create struct {
|
type Create struct {
|
||||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,number,min=1"`
|
ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,number,min=1"`
|
||||||
ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"`
|
ChickinRequests []ChickinRequestItem `json:"chickin_requests" validate:"required,min=1,dive"`
|
||||||
Note string `json:"note" validate:"omitempty"`
|
}
|
||||||
|
|
||||||
|
type ChickinRequestItem struct {
|
||||||
|
ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"`
|
||||||
|
ProductWarehouseId uint `json:"product_warehouse_id" validate:"required,number,min=1"`
|
||||||
|
Note string `json:"note" validate:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Update struct {
|
type Update struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user