Fix[BE]: fix wrong approval step when SO uppdated

This commit is contained in:
aguhh18
2025-11-18 11:32:41 +07:00
parent 28c81aac25
commit 320f5e65c6
@@ -291,32 +291,30 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
}
}
} else {
// Create new marketing product (use helper)
if err := s.createMarketingProductWithDelivery(c.Context(), id, rp, marketingProductRepoTx, invDeliveryRepoTx); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing product")
}
}
}
// 2) Delete missing old products (prevent deletion if deliveries exist)
for _, old := range oldProducts {
if _, ok := reqByPW[old.ProductWarehouseId]; !ok {
// Check delivery product for this marketing product
deliveryProduct, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check existing delivery product")
}
if err == nil {
// If delivery exists (delivery_date not nil or qty > 0), prevent deletion
if deliveryProduct.DeliveryDate != nil || deliveryProduct.Qty > 0 {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Cannot delete marketing product %d because it has delivery records", old.Id))
}
// safe to delete delivery product record
if err := invDeliveryRepoTx.DeleteOne(c.Context(), deliveryProduct.Id); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete marketing delivery product")
}
}
// Delete marketing product
if err := marketingProductRepoTx.DeleteOne(c.Context(), old.Id); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete marketing product")
}
@@ -327,7 +325,7 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
if latestApproval != nil && latestApproval.StepNumber == 2 {
actorID := uint(1) // todo: ambil dari auth context
resetNote := ""
action := entity.ApprovalActionApproved
action := entity.ApprovalActionUpdated
_, err := approvalSvcTx.CreateApproval(
c.Context(),
utils.ApprovalWorkflowMarketing,