# quangiscoding/flashsale-engine architecture

> Architecture diagram of the GitHub repository quangiscoding/flashsale-engine, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/quangiscoding/flashsale-engine
- Repository: https://github.com/quangiscoding/flashsale-engine

## Overview

This is a flash-sale checkout service. A client submits user and product IDs to the HTTP API; the API assigns an order ID, queues the request, and immediately returns HTTP 202. A separate worker consumes queued orders and transactionally decrements stock and records successful orders in PostgreSQL, rejecting sold-out items. The database initializer creates and seeds the schema. The main excerpts show these runtime paths; details beyond them are not inferred.

Main entry points: [`app/src/worker.ts`](https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/worker.ts) (Order Worker), [`app/src/api.ts`](https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/api.ts) (Checkout API), [`app/src/db/index.ts`](https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/db/index.ts) (Postgres Pool), [`app/src/db/init.ts`](https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/db/init.ts) (Schema Initializer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_checkout["Checkout API"]
  node_api["Checkout API<br/>[api.ts]"]
end

subgraph group_messaging["Order Messaging"]
  node_rabbit["RabbitMQ Queue"]
  node_worker["Order Worker<br/>[worker.ts]"]
end

subgraph group_persistence["Order Persistence"]
  node_pool["Postgres Pool<br/>[index.ts]"]
  node_postgres[("Orders Database")]
  node_initializer["Schema Initializer<br/>[init.ts]"]
end

node_client(("Checkout Client"))

node_client -->|"submits checkout"| node_api
node_api -->|"publishes order"| node_rabbit
node_api -->|"returns 202"| node_client
node_rabbit -->|"delivers order"| node_worker
node_worker -->|"acknowledges message"| node_rabbit
node_worker -->|"connects through"| node_pool
node_pool -->|"opens connections"| node_postgres
node_worker -->|"updates stock and inserts order"| node_postgres
node_initializer -->|"connects through"| node_pool
node_initializer -->|"creates and seeds schema"| node_postgres

click node_api "https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/api.ts"
click node_worker "https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/worker.ts"
click node_pool "https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/db/index.ts"
click node_initializer "https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/db/init.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_client toneBlue
class node_rabbit,node_worker toneAmber
class node_pool,node_postgres,node_initializer toneMint
```

## Components

### Checkout API

- [Checkout API](https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/api.ts): `app/src/api.ts`

### Order Messaging

- **RabbitMQ Queue**
- [Order Worker](https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/worker.ts): `app/src/worker.ts`

### Order Persistence

- [Postgres Pool](https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/db/index.ts): `app/src/db/index.ts`
- **Orders Database**
- [Schema Initializer](https://github.com/quangiscoding/flashsale-engine/blob/main/app/src/db/init.ts): `app/src/db/init.ts`

### Other components

- **Checkout Client**

## Connections

- Checkout Client → Checkout API: submits checkout (evidence: `app/src/api.ts`)
- Checkout API → RabbitMQ Queue: publishes order (evidence: `app/src/api.ts`)
- Checkout API → Checkout Client: returns 202 (evidence: `app/src/api.ts`)
- RabbitMQ Queue → Order Worker: delivers order (evidence: `app/src/worker.ts`)
- Order Worker → RabbitMQ Queue: acknowledges message (evidence: `app/src/worker.ts`)
- Order Worker → Postgres Pool: connects through (evidence: `app/src/worker.ts`)
- Postgres Pool → Orders Database: opens connections (evidence: `app/src/db/index.ts`)
- Order Worker → Orders Database: updates stock and inserts order (evidence: `app/src/worker.ts`)
- Schema Initializer → Postgres Pool: connects through (evidence: `app/src/db/init.ts`)
- Schema Initializer → Orders Database: creates and seeds schema (evidence: `app/src/db/init.ts`)
