mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 07:15:43 +00:00
[FEAT/BE] fix response closing and fix status rejected filter
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
@@ -117,18 +118,30 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
|||||||
Preload("Products.DeliveryProduct")
|
Preload("Products.DeliveryProduct")
|
||||||
|
|
||||||
if params.Status != "" {
|
if params.Status != "" {
|
||||||
|
status := strings.TrimSpace(params.Status)
|
||||||
latestApprovalSubQuery := s.MarketingRepo.DB().
|
latestApprovalSubQuery := s.MarketingRepo.DB().
|
||||||
WithContext(c.Context()).
|
WithContext(c.Context()).
|
||||||
Table("approvals").
|
Table("approvals").
|
||||||
Select("DISTINCT ON (approvable_id) approvable_id, step_name").
|
Select("DISTINCT ON (approvable_id) approvable_id, step_name, action").
|
||||||
Where("approvable_type = ?", utils.ApprovalWorkflowMarketing.String()).
|
Where("approvable_type = ?", utils.ApprovalWorkflowMarketing.String()).
|
||||||
Order("approvable_id, id DESC")
|
Order("approvable_id, id DESC")
|
||||||
db = db.Where(`EXISTS (
|
|
||||||
SELECT 1
|
if strings.EqualFold(status, "DITOLAK") {
|
||||||
FROM (?) AS latest_approval
|
db = db.Where(`EXISTS (
|
||||||
WHERE latest_approval.approvable_id = marketings.id
|
SELECT 1
|
||||||
AND LOWER(latest_approval.step_name) = LOWER(?)
|
FROM (?) AS latest_approval
|
||||||
)`, latestApprovalSubQuery, params.Status)
|
WHERE latest_approval.approvable_id = marketings.id
|
||||||
|
AND latest_approval.action = ?
|
||||||
|
)`, latestApprovalSubQuery, string(entity.ApprovalActionRejected))
|
||||||
|
} else {
|
||||||
|
db = db.Where(`EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM (?) AS latest_approval
|
||||||
|
WHERE latest_approval.approvable_id = marketings.id
|
||||||
|
AND LOWER(latest_approval.step_name) = LOWER(?)
|
||||||
|
AND (latest_approval.action IS NULL OR latest_approval.action <> ?)
|
||||||
|
)`, latestApprovalSubQuery, status, string(entity.ApprovalActionRejected))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Search != "" {
|
if params.Search != "" {
|
||||||
|
|||||||
@@ -152,6 +152,31 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
requestedByWarehouse := make(map[uint]float64)
|
||||||
|
for _, item := range req.MarketingProducts {
|
||||||
|
if item.ProductWarehouseId == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
requestedByWarehouse[item.ProductWarehouseId] += item.Qty
|
||||||
|
}
|
||||||
|
|
||||||
|
for pwID, requestedQty := range requestedByWarehouse {
|
||||||
|
productWarehouse, err := s.ProductWarehouseRepo.GetDetailByID(c.Context(), pwID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Product warehouse %d not found", pwID))
|
||||||
|
}
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check stock availability")
|
||||||
|
}
|
||||||
|
availableQty := productWarehouse.Quantity
|
||||||
|
if availableQty+1e-6 < requestedQty {
|
||||||
|
return nil, fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf("Stok tidak mencukupi untuk gudang %d: diminta %.3f, tersedia %.3f", pwID, requestedQty, availableQty),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
soDate, err := utils.ParseDateString(req.Date)
|
soDate, err := utils.ParseDateString(req.Date)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid date format")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid date format")
|
||||||
|
|||||||
Reference in New Issue
Block a user