fix merging

This commit is contained in:
ragilap
2025-11-21 10:50:30 +07:00
parent 53b226f243
commit 3fc330d8f7
5 changed files with 151 additions and 100 deletions
@@ -168,9 +168,9 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
return nil, err
}
user, ok := authmiddleware.AuthenticatedUser(c)
if !ok || user == nil || user.Id == 0 {
return nil, fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
actorID, err := actorIDFromContext(c)
if err != nil {
return nil, err
}
ctx := c.Context()
@@ -263,7 +263,7 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
DueDate: dueDate,
GrandTotal: 0,
Notes: req.Notes,
CreatedBy: uint64(user.Id),
CreatedBy: uint64(actorID),
}
items := make([]*entity.PurchaseItem, 0, len(aggregated))
@@ -332,7 +332,10 @@ func (s *purchaseService) processStaffPurchaseApproval(c *fiber.Ctx, id uint64,
return nil, err
}
actorID := uint(1) // TODO: replace with authenticated user id once available
actorID, err := actorIDFromContext(c)
if err != nil {
return nil, err
}
ctx := c.Context()
purchase, err := s.PurchaseRepo.GetByIDWithRelations(ctx, id)
@@ -476,6 +479,11 @@ func (s *purchaseService) ApproveManagerPurchase(c *fiber.Ctx, id uint64, req *v
return nil, err
}
actorID, err := actorIDFromContext(c)
if err != nil {
return nil, err
}
ctx := c.Context()
purchase, err := s.PurchaseRepo.GetByIDWithRelations(ctx, id)
@@ -496,7 +504,6 @@ func (s *purchaseService) ApproveManagerPurchase(c *fiber.Ctx, id uint64, req *v
return nil, fiber.NewError(fiber.StatusBadRequest, "Purchase must reach staff purchase step before manager approval")
}
actorID := uint(1)
action := entity.ApprovalActionApproved
now := time.Now().UTC()
hasExistingPO := purchase.PoNumber != nil && strings.TrimSpace(*purchase.PoNumber) != ""
@@ -577,6 +584,11 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint64, req *validati
return nil, err
}
actorID, err := actorIDFromContext(c)
if err != nil {
return nil, err
}
ctx := c.Context()
purchase, err := s.PurchaseRepo.GetByIDWithRelations(ctx, id)
@@ -674,7 +686,6 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint64, req *validati
receivingAction := entity.ApprovalActionApproved
completedAction := entity.ApprovalActionApproved
actorID := uint(1)
approvalSvc := s.approvalServiceForDB(nil)
if approvalSvc != nil {
@@ -1059,6 +1070,14 @@ func (s *purchaseService) notifyExpenseItemsDeleted(ctx context.Context, purchas
}
}
func actorIDFromContext(c *fiber.Ctx) (uint, error) {
user, ok := authmiddleware.AuthenticatedUser(c)
if !ok || user == nil || user.Id == 0 {
return 0, fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
}
return user.Id, nil
}
func (s *purchaseService) buildStaffAdjustmentPayload(
ctx context.Context,
purchase *entity.Purchase,