fix(orders): keep the customer a new order is created for

CreateOrderContractToModel never copied customer_id, so every order from
POST /orders was saved without a customer. Paying it then earned no
EnakPoint or EnakCoin (skipped as NO_CUSTOMER, which is not logged).

The customer must now belong to the order's organization, as
SetOrderCustomer already requires: it is who earns once the order is paid,
and orders.customer_id has no foreign key. A nil UUID means no customer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
efrilm
2026-09-30 23:29:17 +07:00
co-authored by Claude Opus 5.5
parent c9654a387a
commit 892575202b
3 changed files with 28 additions and 1 deletions
+13 -1
View File
@@ -316,6 +316,18 @@ func resolveLineQuantity(product *entities.Product, quantity int, weight *float6
}
func (p *OrderProcessorImpl) CreateOrder(ctx context.Context, req *models.CreateOrderRequest, organizationID uuid.UUID) (*models.OrderResponse, error) {
// The order's customer earns EnakPoint and EnakCoin once it is paid, so it must be
// one of the organization's own; orders.customer_id has no foreign key to catch it.
customerID := req.CustomerID
if customerID != nil && *customerID == uuid.Nil {
customerID = nil
}
if customerID != nil {
if _, err := p.customerRepo.GetByIDAndOrganization(ctx, *customerID, organizationID); err != nil {
return nil, fmt.Errorf("customer not found or does not belong to the organization: %w", err)
}
}
orderNumber, err := p.orderRepo.GetNextOrderNumber(ctx, organizationID, req.OutletID)
if err != nil {
return nil, fmt.Errorf("failed to generate order number: %w", err)
@@ -401,7 +413,7 @@ func (p *OrderProcessorImpl) CreateOrder(ctx context.Context, req *models.Create
OrganizationID: organizationID,
OutletID: req.OutletID,
UserID: req.UserID,
CustomerID: req.CustomerID,
CustomerID: customerID,
OrderNumber: orderNumber,
TableNumber: req.TableNumber,
OrderType: entities.OrderType(req.OrderType),