---
# System prepended metadata

title: 東毅中 Order Service - SDD
tags: [software-docs]

---

# 東毅中 Order Service - SDD

Order Service 用於管理訂單，包含創建訂單、更新訂單狀態 (已完成、運送中等) 等。並在創建訂單成功後發送 Email 給客戶。

## A. Version

| 版本  | 內容              | 日期   | 負責人 |
| ---- | ----------------- | ----- | ----- |
| 1.0  | Tech Stack        | 12/05 | Ateto |
| 1.1  | Model Design      | 12/08 | Ateto |
| 1.2  | Arch Design       | 12/10 | Ateto |
| 1.3  | Test Cases        | 12/12 | Ateto |
| 1.4  | API Design        | 12/14 | Ateto |
| 2.0  | Upd product model | 12/18 | Ateto |
| 2.1  | Upd `order-01`    | 12/19 | Ateto |
| 2.2  | Upd `order-03`    | 12/21 | Ateto |
| 2.3  | Upd `order-04`    | 12/25 | Ateto |

## B. Tech Stack
- Spring Boot (Java)
- MySQL
- Redis (Good to Have)
- Kafka (Good to Have)

## C. Architecture

- Clean Architecture

### Product Structure
```
- .github/workflows/main.yml
- src/main
    - java/serv.dongyi.order
        - domain/
            - Order.java
            - Product.java
        - repository/
            - OrderRepository.java
        - useCases/
            - OrderUseCase.java
        - controller/
            - OrderController.java
        - OrderServiceApplication.java
    - resources
        - application.properties
```

## D. Model

### 1. Product Model
- ID: string
    - Product ID
- Spec: map
    - key: size (string)
    - value: quantity (int)

### 2. Order Model
- ID: string
    - UUID
- Owner: string
    - User ID (email)
- Content: []Product
    - List of product model
- Status: string
    - Enumeration: Created, Pending, Delivery, Completed, Rejected, Failed
- Progess: []string
    - 用空格隔開進度狀態和時間戳 e.g.`pending 2024-11-24T11:09:06.544936+08:00`
- CreateAt: string



## E. API
- URI Prefix: `order/api/`

### 1. CreateOrder

- ID: `order-01`
- Description: 使用 Owner ID 和 Product ID List 創建 Order
- Workflow: 
    - check if stock is sufficient from product service
    - create order
    - (not complete) remove cart item from cart service
    - sent an email with order info to customer
- Method: `POST`
- URI: `/order-create`
- Status Code: 
    - `200`: OK
    - `404`: Not Found - Owner 不存在
- Body
    :::spoiler Request
    ```json
    {
        "owner": "demo@gmail.com",
        "content": [
            {
                "id": "p1",
                "price": 299,
                "spec": {
                    "M": 2
                }
            },
            {
                "id": "p2",
                "price": 249,
                "spec": {
                    "L": 1
                }
            },
            {
                "id": "p3",
                "price": 399,
                "spec": {
                    "XL": 1,
                    "XXL": 2
                }
            }
        ]
    }
    ```
    :::
    :::spoiler Reponse
    ```json
    {
        "id":"demo-order-id",
        "owner": "demo@gmail.com",
        "content": [
            {
                "id": "p1",
                "price": 299,
                "spec": {
                    "M": 2
                }
            },
            {
                "id": "p2",
                "price": 249,
                "spec": {
                    "L": 1
                }
            },
            {
                "id": "p3",
                "price": 399,
                "spec": {
                    "XL": 1,
                    "XXL": 2
                }
            }
        ],
        "status": "delivery",
        progess": [
            "created 2024-11-24T11:09:06.544936+08:00",
        ],
        "create_at": "2024-11-24T11:09:06.544936+08:00"
    }
    ```
    :::

### 2. GetOrder
- ID: `order-02`
- Description: 使用 Order ID 來取得 Order Info，並使用 Owner ID 驗證身份，Owner ID 可以是客戶或管理員 ID。
- Method: `POST`
- URI: `/order-get`
- State Code: 
    - `200`: OK
    - `401`: Unauthorized - Owner 未授權
    - `404`: Not Found - Order 或 Owner 不存在
- Body
    :::spoiler Request
    ```json
    {
        "id":"demo-order-id",
        "owner":"demo@gmail.com"
    }
    ```
    :::
    :::spoiler Response
    ```json
    {
        "id":"demo-order-id",
        "owner": "demo@gmail.com",
        "content": [
            {
                "id": "p1",
                "price": 299,
                "spec": {
                    "M": 2
                }
            },
            {
                "id": "p2",
                "price": 249,
                "spec": {
                    "L": 1
                }
            },
            {
                "id": "p3",
                "price": 399,
                "spec": {
                    "XL": 1,
                    "XXL": 2
                }
            }
        ],
        "status": "delivery",
        "progess": [
            "created 2024-11-24T11:09:06.544936+08:00",
            "pending 2024-11-24T11:09:06.544936+08:00",
            "delivery 2024-11-24T11:09:06.544936+08:00"
        ],
        "create_at": "2024-11-24T11:09:06.544936+08:00"
    }
    ```
    :::

### 3. UpdOrderStatus (not completed)
- ID: `order-03`
- Description: 更新 Order Status。
- Method: `PATCH`
- URI: `/status-upd`
- Status Code
    - `200`: OK
    - `400`: Bad Request
    - `404`: Not Found
    - `500`: Internal Error
- Body
    :::spoiler Request
    ```json
    {
        "id": "demo-order-id",
        "admin": "demo-user-id",
        "status": "expected-upd-status"
    }
    ```
    :::
    :::spoiler Response
    ```json
    {
        "status": "expected-upd-status",
        "progess": [
            "created 2024-11-24T11:09:06.544936+08:00",
            "pending 2024-11-24T11:09:06.544936+08:00",
            "delivery 2024-11-24T11:09:06.544936+08:00",
            "expected-upd-status demo-local-date-time" // 用空白格開
        ]
    }
    ```
    :::

### 4. GetAllOrders
- ID: `order-04`
- Description: 取得所有訂單 ID，只有管理員可以存取。
- Method: `POST`
- URI: `/orders-get` ( `order-02` 的 routes 是 `/order-get` 沒有 s )
- Status Code
    - `200`: OK
    - `400`: Bad Request
    - `404`: Not Found
    - `500`: Internal Error
- Body
    :::spoiler Request
    ```json
    {
        "id": "demo-order-id"
    }
    ```
    :::
    :::spoiler Response
    ```json
    {
        [
            "demo-order-01",
            "demo-order-02",
            "demo-order-03"
        ]
    }
    ```
    :::

## F. Test Cases

### 1. Create Order

### 2. Get Order

### 3. Order Not Found

### 4. Order Unauthorized
