diff --git a/internal/processor/order_processor.go b/internal/processor/order_processor.go index bea796c..60ad0f4 100644 --- a/internal/processor/order_processor.go +++ b/internal/processor/order_processor.go @@ -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), diff --git a/internal/transformer/order_transformer.go b/internal/transformer/order_transformer.go index 5bd19ba..2ed1194 100644 --- a/internal/transformer/order_transformer.go +++ b/internal/transformer/order_transformer.go @@ -27,6 +27,7 @@ func CreateOrderContractToModel(req *contract.CreateOrderRequest) *models.Create return &models.CreateOrderRequest{ OutletID: req.OutletID, UserID: req.UserID, + CustomerID: req.CustomerID, TableID: req.TableID, TableNumber: req.TableNumber, OrderType: constants.OrderType(req.OrderType), diff --git a/internal/transformer/order_transformer_test.go b/internal/transformer/order_transformer_test.go index 5c55afd..4c6fcde 100644 --- a/internal/transformer/order_transformer_test.go +++ b/internal/transformer/order_transformer_test.go @@ -32,6 +32,20 @@ func TestCreateOrderContractToModelCarriesWeight(t *testing.T) { require.Equal(t, weight, *result.OrderItems[0].Weight) } +func TestCreateOrderContractToModelCarriesCustomer(t *testing.T) { + customerID := uuid.New() + + result := CreateOrderContractToModel(&contract.CreateOrderRequest{ + OutletID: uuid.New(), + UserID: uuid.New(), + CustomerID: &customerID, + OrderItems: []contract.CreateOrderItemRequest{{ProductID: uuid.New(), Quantity: 1}}, + }) + + require.NotNil(t, result.CustomerID, "the order's customer is who earns EnakPoint and EnakCoin") + require.Equal(t, customerID, *result.CustomerID) +} + func TestAddToOrderContractToModelCarriesWeight(t *testing.T) { weight := 0.8