# ngo-the-dat/e-voucher-system architecture

> Architecture diagram of the GitHub repository ngo-the-dat/e-voucher-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ngo-the-dat/e-voucher-system
- Repository: https://github.com/ngo-the-dat/e-voucher-system

## Overview

#### Purpose and Topology
An online discount-voucher marketplace with three role-specific surfaces: customers discover, buy, and review vouchers; partners manage businesses, employees, branches, voucher campaigns, and redemption; administrators approve and govern platform activity. It is a TypeScript full-stack application: Next.js frontend plus Node/Express-style backend and SQL database assets.

#### Entry Points and API Composition
Backend startup is centered on `backend/src/server.ts` and application assembly in `backend/src/app.ts`. `backend/src/routers/index.ts` composes role-scoped router trees. Request handling follows router → controller → service, separating HTTP concerns from business and integration logic; e.g. `backend/src/routers/customer/order.router.ts`, `backend/src/controllers/customer/order.controller.ts`, and `backend/src/services/customer/order.service.ts`.

#### Identity, Roles, and Access Boundaries
Separate auth flows exist for common, customer, and partner identities, with role enforcement at API boundaries. `backend/src/middlewares/auth.middleware.ts` authenticates callers and `backend/src/middlewares/role.middleware.ts` restricts protected operations. Auth, password-reset OTP, registration OTP, and email delivery are represented by `backend/src/services/common/auth.service.ts`, `backend/src/services/common/password-reset-otp.service.ts`, and `backend/src/services/email/otp-email.service.ts`.

#### Customer Commerce Lifecycle
Customer APIs cover catalog discovery, cart state, checkout/orders, owned vouchers, reviews, and payment-provider initiation/return processing. The central purchase flow spans `backend/src/services/customer/cart.service.ts`, `backend/src/services/customer/order.service.ts`, and payment-specific services such as `backend/src/services/customer/stripe-payment.service.ts`. The customer Next.js route space begins under `frontend/src/app/(customer)/`; primary commerce screens include `frontend/src/app/(customer)/cart/page.tsx` and `frontend/src/app/(customer)/orders/page.tsx`.

#### Partner Supply and Redemption Lifecycle
Partners register and maintain organization profiles, branches, employees, and voucher listings; staff validate/redeem purchased vouchers. This is modeled by `backend/src/services/partner/voucher.service.ts`, `backend/src/services/partner/redeem.service.ts`, and `backend/src/services/partner/partner-employee-mgmt.service.ts`. Partner UI divides onboarding and authenticated management under `frontend/src/app/partner/(auth)/` and `frontend/src/app/partner/(main)/`, with employee-facing redemption at `frontend/src/app/partner/employee/page.tsx`.

#### Administrative Governance
Administrators oversee users, partners and employee approvals, voucher approvals/status, orders, CMS content, audit logs, and dashboards. Core services are `backend/src/services/admin/partner.service.ts`, `backend/src/services/admin/voucher.service.ts`, and `backend/src/services/admin/dashboard.service.ts`; administrative UI is rooted in `frontend/src/app/admin/page.tsx` and organized under `frontend/src/app/admin/partners/` and `frontend/src/app/admin/vouchers/`.

#### Data, External Services, and Operations
Database connectivity is configured in `backend/src/config/db.ts`, with schema/bootstrap and seed material in `database/scripts/init.sql` and `database/seeds/data.sql`. Payment integrations are isolated in `backend/src/config/stripe.ts`, `backend/src/config/paypal.ts`, `backend/src/config/vnpay.ts`, and `backend/src/config/zalopay.ts`. Voucher-image uploads cross an HTTP middleware boundary (`backend/src/middlewares/voucher-image-upload.middleware.ts`) into Cloudflare R2 storage (`backend/src/services/storage/r2.service.ts`). A crawler pipeline extracts, normalizes, and seeds voucher data through `backend/src/crawler/index.ts`, `backend/src/crawler/normalizer.ts`, and `backend/src/crawler/database-seeder.ts`. Independent GitHub Actions deploy frontend and backend via `.github/workflows/deploy-frontend.yml` and `.github/workflows/deploy-backend.yml`.

Main entry points: [`backend/src/routers/index.ts`](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/routers/index.ts) (Role-scoped routers), [`backend/src/server.ts`](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/server.ts) (Backend server), [`backend/src/config/db.ts`](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/config/db.ts) (SQL database), [`backend/src/services/customer/order.service.ts`](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/customer/order.service.ts) (Customer commerce)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Next.js surfaces"]
  node_customer_ui["Customer commerce UI<br/>Next.js routes"]
  node_partner_ui["Partner management UI<br/>Next.js routes"]
  node_employee_redemption_ui["Employee redemption UI<br/>Next.js page<br/>[page.tsx]"]
  node_admin_ui["Admin governance UI<br/>Next.js routes<br/>[page.tsx]"]
end

subgraph group_backend["API and domain services"]
  node_server["Backend server<br/>Node entry point<br/>[server.ts]"]
  node_app["Application assembly<br/>Express app<br/>[app.ts]"]
  node_routers["Role-scoped routers<br/>router composition<br/>[index.ts]"]
  node_access_control["Authentication and roles<br/>middleware<br/>[auth.middleware.ts]"]
  node_commerce["Customer commerce<br/>domain services<br/>[order.service.ts]"]
  node_partner_operations["Partner supply and redemption<br/>domain services<br/>[voucher.service.ts]"]
  node_admin_governance["Administrative governance<br/>domain services<br/>[voucher.service.ts]"]
  node_identity_otp["Identity and OTP<br/>domain services<br/>[auth.service.ts]"]
  node_image_upload["Voucher image upload<br/>HTTP middleware"]
end

subgraph group_integrations["External integrations"]
  node_payment_providers{{"Payment providers<br/>payment integrations<br/>[stripe.ts]"}}
  node_r2_storage[("Cloudflare R2<br/>object storage<br/>[r2.service.ts]")]
end

subgraph group_dataops["Data and delivery"]
  node_sql_database[("SQL database<br/>database connection<br/>[db.ts]")]
  node_database_bootstrap["Schema and seed data<br/>SQL assets<br/>[init.sql]"]
  node_crawler["Voucher crawler pipeline<br/>ingestion pipeline<br/>[index.ts]"]
  node_frontend_deploy["Frontend deployment<br/>GitHub Actions"]
  node_backend_deploy["Backend deployment<br/>GitHub Actions<br/>[deploy-backend.yml]"]
end

node_customer_ui -->|"customer API requests"| node_server
node_partner_ui -->|"partner API requests"| node_server
node_employee_redemption_ui -->|"redemption requests"| node_server
node_admin_ui -->|"admin API requests"| node_server
node_server -->|"starts"| node_app
node_app -->|"mounts"| node_routers
node_routers -->|"protects routes with"| node_access_control
node_routers -->|"customer operations"| node_commerce
node_routers -->|"partner operations"| node_partner_operations
node_routers -->|"admin operations"| node_admin_governance
node_routers -->|"auth flows"| node_identity_otp
node_commerce -->|"initiates and handles payments"| node_payment_providers
node_commerce -->|"persists carts and orders"| node_sql_database
node_partner_operations -->|"accepts voucher images"| node_image_upload
node_image_upload -->|"uploads"| node_r2_storage
node_partner_operations -->|"persists supply and redemptions"| node_sql_database
node_admin_governance -->|"governs marketplace records"| node_sql_database
node_identity_otp -->|"manages identities and OTPs"| node_sql_database
node_database_bootstrap -->|"initializes"| node_sql_database
node_crawler -->|"seeds normalized vouchers"| node_sql_database
node_frontend_deploy -.->|"deploys frontend"| node_customer_ui
node_backend_deploy -.->|"deploys backend"| node_server

click node_employee_redemption_ui "https://github.com/ngo-the-dat/e-voucher-system/blob/main/frontend/src/app/partner/employee/page.tsx"
click node_admin_ui "https://github.com/ngo-the-dat/e-voucher-system/blob/main/frontend/src/app/admin/page.tsx"
click node_server "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/server.ts"
click node_app "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/app.ts"
click node_routers "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/routers/index.ts"
click node_access_control "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/middlewares/auth.middleware.ts"
click node_commerce "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/customer/order.service.ts"
click node_partner_operations "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/partner/voucher.service.ts"
click node_admin_governance "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/admin/voucher.service.ts"
click node_identity_otp "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/common/auth.service.ts"
click node_image_upload "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/middlewares/voucher-image-upload.middleware.ts"
click node_payment_providers "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/config/stripe.ts"
click node_r2_storage "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/storage/r2.service.ts"
click node_sql_database "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/config/db.ts"
click node_database_bootstrap "https://github.com/ngo-the-dat/e-voucher-system/blob/main/database/scripts/init.sql"
click node_crawler "https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/crawler/index.ts"
click node_frontend_deploy "https://github.com/ngo-the-dat/e-voucher-system/blob/main/.github/workflows/deploy-frontend.yml"
click node_backend_deploy "https://github.com/ngo-the-dat/e-voucher-system/blob/main/.github/workflows/deploy-backend.yml"

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_customer_ui,node_partner_ui,node_employee_redemption_ui,node_admin_ui toneBlue
class node_server,node_app,node_routers,node_access_control,node_commerce,node_partner_operations,node_admin_governance,node_identity_otp,node_image_upload toneAmber
class node_payment_providers,node_r2_storage toneMint
class node_sql_database,node_database_bootstrap,node_crawler,node_frontend_deploy,node_backend_deploy toneRose
```

## Components

### Next.js surfaces

Role-specific application routes.

- **Customer commerce UI**: Next.js routes · Discovery, cart, checkout, orders, and reviews.
- **Partner management UI**: Next.js routes · Onboarding, management, and employee redemption.
- [Employee redemption UI](https://github.com/ngo-the-dat/e-voucher-system/blob/main/frontend/src/app/partner/employee/page.tsx): Next.js page · `frontend/src/app/partner/employee/page.tsx`
- [Admin governance UI](https://github.com/ngo-the-dat/e-voucher-system/blob/main/frontend/src/app/admin/page.tsx): Next.js routes · Approvals, operations, CMS, and dashboards. · `frontend/src/app/admin/page.tsx`

### API and domain services

Express-style API composition and business logic.

- [Backend server](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/server.ts): Node entry point · `backend/src/server.ts`
- [Application assembly](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/app.ts): Express app · `backend/src/app.ts`
- [Role-scoped routers](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/routers/index.ts): router composition · Composes common, customer, partner, and admin API trees. · `backend/src/routers/index.ts`
- [Authentication and roles](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/middlewares/auth.middleware.ts): middleware · Authenticates callers and enforces protected role boundaries. · `backend/src/middlewares/auth.middleware.ts`
- [Customer commerce](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/customer/order.service.ts): domain services · Cart, orders, owned vouchers, reviews, and payment processing. · `backend/src/services/customer/order.service.ts`
- [Partner supply and redemption](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/partner/voucher.service.ts): domain services · Voucher campaigns, employees, branches, and redemption. · `backend/src/services/partner/voucher.service.ts`
- [Administrative governance](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/admin/voucher.service.ts): domain services · Approval workflows, platform oversight, and dashboards. · `backend/src/services/admin/voucher.service.ts`
- [Identity and OTP](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/common/auth.service.ts): domain services · Role-specific auth, registration, password reset, and email OTP. · `backend/src/services/common/auth.service.ts`
- [Voucher image upload](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/middlewares/voucher-image-upload.middleware.ts): HTTP middleware · `backend/src/middlewares/voucher-image-upload.middleware.ts`

### External integrations

- [Payment providers](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/config/stripe.ts): payment integrations · Stripe, PayPal, VNPay, and ZaloPay initiation and return handling. · `backend/src/config/stripe.ts`
- [Cloudflare R2](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/services/storage/r2.service.ts): object storage · Stores uploaded voucher images. · `backend/src/services/storage/r2.service.ts`

### Data and delivery

- [SQL database](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/config/db.ts): database connection · Persistent marketplace data. · `backend/src/config/db.ts`
- [Schema and seed data](https://github.com/ngo-the-dat/e-voucher-system/blob/main/database/scripts/init.sql): SQL assets · `database/scripts/init.sql`
- [Voucher crawler pipeline](https://github.com/ngo-the-dat/e-voucher-system/blob/main/backend/src/crawler/index.ts): ingestion pipeline · Extracts, normalizes, and seeds voucher data. · `backend/src/crawler/index.ts`
- [Frontend deployment](https://github.com/ngo-the-dat/e-voucher-system/blob/main/.github/workflows/deploy-frontend.yml): GitHub Actions · `.github/workflows/deploy-frontend.yml`
- [Backend deployment](https://github.com/ngo-the-dat/e-voucher-system/blob/main/.github/workflows/deploy-backend.yml): GitHub Actions · `.github/workflows/deploy-backend.yml`

## Connections

- Customer commerce UI → Backend server: customer API requests
- Partner management UI → Backend server: partner API requests
- Employee redemption UI → Backend server: redemption requests
- Admin governance UI → Backend server: admin API requests
- Backend server → Application assembly: starts
- Application assembly → Role-scoped routers: mounts
- Role-scoped routers → Authentication and roles: protects routes with
- Role-scoped routers → Customer commerce: customer operations
- Role-scoped routers → Partner supply and redemption: partner operations
- Role-scoped routers → Administrative governance: admin operations
- Role-scoped routers → Identity and OTP: auth flows
- Customer commerce → Payment providers: initiates and handles payments
- Customer commerce → SQL database: persists carts and orders
- Partner supply and redemption → Voucher image upload: accepts voucher images
- Voucher image upload → Cloudflare R2: uploads
- Partner supply and redemption → SQL database: persists supply and redemptions
- Administrative governance → SQL database: governs marketplace records
- Identity and OTP → SQL database: manages identities and OTPs
- Schema and seed data → SQL database: initializes
- Voucher crawler pipeline → SQL database: seeds normalized vouchers
- Frontend deployment → Customer commerce UI: deploys frontend
- Backend deployment → Backend server: deploys backend
