Fix config chickin

This commit is contained in:
ragilap
2026-03-04 14:39:50 +07:00
parent d334f46829
commit 1724a5f846
3 changed files with 59 additions and 0 deletions
@@ -28,6 +28,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type ChickinService interface {
@@ -518,6 +519,21 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
touchedProductWarehouseIDs := make(map[uint]struct{})
for _, approvableID := range approvableIDs {
// Re-check latest approval inside transaction to prevent double-approve races.
var latest entity.Approval
if err := dbTransaction.WithContext(c.Context()).
Table("approvals").
Where("approvable_type = ? AND approvable_id = ?", utils.ApprovalWorkflowChickin.String(), approvableID).
Order("id DESC").
Limit(1).
Clauses(clause.Locking{Strength: "UPDATE"}).
Take(&latest).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to recheck approval status")
}
if latest.Id != 0 && latest.StepNumber != uint16(utils.ChickinStepPengajuan) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("ProjectFlockKandang %d sudah tidak berada di tahap PENGAJUAN", approvableID))
}
if _, err := approvalSvc.CreateApproval(
c.Context(),
utils.ApprovalWorkflowChickin,