Fix Split Bill
This commit is contained in:
@@ -21,6 +21,7 @@ type OrderService interface {
|
||||
CreatePayment(ctx context.Context, req *models.CreatePaymentRequest) (*models.PaymentResponse, error)
|
||||
RefundPayment(ctx context.Context, paymentID uuid.UUID, refundAmount float64, reason string, refundedBy uuid.UUID) error
|
||||
SetOrderCustomer(ctx context.Context, orderID uuid.UUID, req *models.SetOrderCustomerRequest, organizationID uuid.UUID) (*models.SetOrderCustomerResponse, error)
|
||||
SplitBill(ctx context.Context, req *models.SplitBillRequest) (*models.SplitBillResponse, error)
|
||||
}
|
||||
|
||||
type OrderServiceImpl struct {
|
||||
@@ -95,12 +96,10 @@ func (s *OrderServiceImpl) GetOrderByID(ctx context.Context, id uuid.UUID) (*mod
|
||||
}
|
||||
|
||||
func (s *OrderServiceImpl) ListOrders(ctx context.Context, req *models.ListOrdersRequest) (*models.ListOrdersResponse, error) {
|
||||
// Validate request
|
||||
if err := s.validateListOrdersRequest(req); err != nil {
|
||||
return nil, fmt.Errorf("validation error: %w", err)
|
||||
}
|
||||
|
||||
// Process order listing
|
||||
response, err := s.orderProcessor.ListOrders(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list orders: %w", err)
|
||||
@@ -384,3 +383,65 @@ func (s *OrderServiceImpl) validateSetOrderCustomerRequest(req *models.SetOrderC
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *OrderServiceImpl) SplitBill(ctx context.Context, req *models.SplitBillRequest) (*models.SplitBillResponse, error) {
|
||||
if err := s.validateSplitBillRequest(req); err != nil {
|
||||
return nil, fmt.Errorf("validation error: %w", err)
|
||||
}
|
||||
|
||||
response, err := s.orderProcessor.SplitBill(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to split bill: %w", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *OrderServiceImpl) validateSplitBillRequest(req *models.SplitBillRequest) error {
|
||||
if req == nil {
|
||||
return fmt.Errorf("request cannot be nil")
|
||||
}
|
||||
|
||||
if req.OrderID == uuid.Nil {
|
||||
return fmt.Errorf("order ID is required")
|
||||
}
|
||||
|
||||
if req.PaymentMethodID == uuid.Nil {
|
||||
return fmt.Errorf("payment ID is required")
|
||||
}
|
||||
|
||||
if req.Type != "ITEM" && req.Type != "AMOUNT" {
|
||||
return fmt.Errorf("split type must be either ITEM or AMOUNT")
|
||||
}
|
||||
|
||||
if req.Type == "ITEM" {
|
||||
if len(req.Items) == 0 {
|
||||
return fmt.Errorf("items are required when splitting by ITEM")
|
||||
}
|
||||
|
||||
totalItemAmount := float64(0)
|
||||
for i, item := range req.Items {
|
||||
if item.OrderItemID == uuid.Nil {
|
||||
return fmt.Errorf("order item ID is required for item %d", i+1)
|
||||
}
|
||||
|
||||
if item.Amount <= 0 {
|
||||
return fmt.Errorf("amount must be greater than zero for item %d", i+1)
|
||||
}
|
||||
|
||||
totalItemAmount += item.Amount
|
||||
}
|
||||
|
||||
if totalItemAmount <= 0 {
|
||||
return fmt.Errorf("total item amount must be greater than zero")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Type == "AMOUNT" {
|
||||
if req.Amount <= 0 {
|
||||
return fmt.Errorf("amount must be greater than zero when splitting by AMOUNT")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user