# Create Order Use Case
## Overview
The Create Order use case is responsible for creating a new order in the system, including
validating the order, storing it in the database, synchronizing it with Hubrise, creating a
Stripe checkout session, and notifying interested parties via events.
## Inputs
- `order`: An `Order` entity with the following properties:
- `locationId`: The ID of the location where the order is placed
- `customerId`: The ID of the customer placing the order
- `items`: An array of order items, each with:
- `menuItemId`: The ID of the menu item
- `quantity`: The quantity of the item
- `price`: The price of the item
- `totalAmount`: The total amount of the order
## Outputs
- The created `Order` entity, including:
- `id`: The ID of the created order
- `status`: The status of the order (initially 'pending')
- `createdAt`: The creation timestamp
- `updatedAt`: The last update timestamp
## Workflow
1. Validate location exists
2. Validate customer exists
3. Validate order (items, total amount, etc.)
4. Create order in database
5. Synchronize order with Hubrise
6. Create Stripe checkout session
7. Emit order.created event
8. Return created order
## Error Handling
- `LocationNotFoundError`: When the location doesn't exist
- `CustomerNotFoundError`: When the customer doesn't exist
- `EmptyOrderError`: When the order has no items
- `InvalidOrderAmountError`: When the order amount is invalid
- `HubriseIntegrationError`: When synchronization with Hubrise fails
- `StripeIntegrationError`: When creating a Stripe checkout session fails
## External Interactions
- **OrderRepository**: Creates the order in the database
- **LocationRepository**: Validates the location exists
- **CustomerRepository**: Validates the customer exists
- **HubriseClient**: Synchronizes the order with Hubrise
- **StripeClient**: Creates a Stripe checkout session
- **EventEmitter**: Emits the order.created event
## Example
```typescript
// Create order data
const order = new Order({
locationId: "location_123",
customerId: "customer_123",
items: [
{
menuItemId: "menuItem_123",
quantity: 2,
price: 15.0,
},
],
totalAmount: 30.0,
});
// Execute use case
const createdOrder = await createOrderUseCase.execute(order);
console.log(createdOrder.id); // The ID of the created order
console.log(createdOrder.status); // 'pending'
```