# Order Service

> Order orchestration with Strategy Pattern for multi-vertical commerce.

## Overview

Order Service is the "brain" of the platform, orchestrating orders across multiple business verticals using the **Strategy Pattern**. It routes each line item to the appropriate vertical engine (Inventory, Booking, F&B Engine) based on product type.

### Key Features

- **Strategy Pattern** - Routes orders by product type without if-else chains
- **Hybrid Orders** - Single order with mixed items (Physical + Service + Food)
- **Transaction Coordination** - Validate → Pay → Execute workflow
- **Event-Driven** - Publishes integration events for downstream services

## Architecture Context

```mermaid
graph TB
    POS["POS / Web"] --> ORDER["📝 Order Service"]
    ORDER --> CATALOG["📦 Catalog Service"]
    ORDER --> MERCHANT["🏪 Merchant Service"]
    ORDER --> WALLET["💰 Wallet Service"]
    ORDER --> INVENTORY["🏭 Inventory Service"]
    ORDER --> BOOKING["📅 Booking Service"]
    ORDER --> FNB["🍳 F&B Engine"]
    
    style ORDER fill:#e74c3c,stroke:#c0392b,color:#fff
```

## Prerequisites

| Requirement | Version |
|-------------|---------|
| .NET SDK | 10.0.101+ |
| Docker | 24.0+ |
| PostgreSQL | 15+ |
| RabbitMQ | 3.12+ |

## Quick Start

```bash
cd services/order-service-net
cp .env.example .env
dotnet restore
dotnet run --project src/OrderService.API
```

## Project Structure

```
order-service-net/
├── src/
│   ├── OrderService.API/
│   │   ├── Application/
│   │   │   ├── Strategies/          # Line item strategies
│   │   │   ├── Commands/            # CreateOrder, CancelOrder
│   │   │   └── Queries/             # GetOrder, ListOrders
│   │   └── Controllers/
│   ├── OrderService.Domain/
│   │   ├── AggregatesModel/
│   │   │   └── OrderAggregate/      # Order, OrderItem
│   │   └── Events/
│   └── OrderService.Infrastructure/
└── tests/
```

## API Endpoints

| Method | Endpoint | Description |
|--------|----------|-------------|
| `POST` | `/api/v1/orders` | Create order |
| `GET` | `/api/v1/orders/{id}` | Get order details |
| `GET` | `/api/v1/orders` | List orders by shop |
| `POST` | `/api/v1/orders/{id}/pay` | Process payment |
| `POST` | `/api/v1/orders/{id}/cancel` | Cancel order |
| `PATCH` | `/api/v1/orders/{id}/items/{itemId}/status` | Update item status |

## Strategy Pattern

### ILineItemStrategy Interface

```csharp
public interface ILineItemStrategy
{
    ProductType SupportedType { get; }
    Task ValidateAsync(OrderItem item, Guid shopId);
    Task ExecuteAsync(OrderItem item, Guid shopId);
}
```

### Available Strategies

| Strategy | Product Type | Target Service |
|----------|--------------|----------------|
| `RetailStrategy` | Physical | Inventory Service |
| `ServiceStrategy` | Service | Booking Service |
| `FnbStrategy` | PreparedFood | F&B Engine |

### Order Processing Flow

```mermaid
sequenceDiagram
    participant POS
    participant Order as Order Service
    participant Catalog as Catalog Service
    participant Strategy as Strategy Factory
    participant Target as Target Service

    POS->>Order: Create Order
    Order->>Catalog: Get Product Types
    
    loop For each LineItem
        Order->>Strategy: Get Strategy by Type
        Strategy->>Target: Validate
        Target-->>Strategy: OK
    end
    
    Order->>Order: Process Payment
    
    loop For each LineItem
        Order->>Strategy: Get Strategy by Type
        Strategy->>Target: Execute
    end
    
    Order-->>POS: Order Completed
```

## Domain Model

### Order Aggregate

```csharp
public class Order : Entity, IAggregateRoot
{
    public Guid ShopId { get; private set; }
    public Guid? CustomerId { get; private set; }
    public OrderStatus Status { get; private set; }
    public decimal TotalAmount { get; private set; }
    
    private readonly List<OrderItem> _items;
    public IReadOnlyCollection<OrderItem> Items => _items.AsReadOnly();
    
    public void AddItem(Product product, int quantity);
    public void MarkAsPaid();
    public void Cancel(string reason);
}
```

## Integration Events

| Event | Trigger | Subscribers |
|-------|---------|-------------|
| `OrderCreatedEvent` | Order placed | Analytics |
| `OrderPaidEvent` | Payment success | Wallet, Notification |
| `OrderCancelledEvent` | Order cancelled | Inventory (rollback) |
| `KitchenTicketCreatedEvent` | F&B item added | F&B Engine |

## Related Services

- [Catalog Service](../../catalog-service-net/docs/en/README.md) - Product data
- [Inventory Service](../../inventory-service-net/docs/en/README.md) - Stock management
- [Booking Service](../../booking-service-net/docs/en/README.md) - Appointments
- [F&B Engine](../../fnb-engine-net/docs/en/README.md) - Kitchen display

## Resources

- [Multi-vertical Architecture](../../../../docs/en/architecture/multi-vertical-architecture.md)
- [Strategy Pattern](https://refactoring.guru/design-patterns/strategy)

## License

Proprietary - GoodGo Platform
