How ngo-the-dat/e-voucher-system works
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: index.ts Role-scoped routers · server.ts Backend server · db.ts SQL database · order.service.ts Customer commerce
Components 20
Next.js surfaces
Role-specific application routes.
- Customer commerce UI · Discovery, cart, checkout, orders, and reviews.
- Partner management UI · Onboarding, management, and employee redemption.
- Employee redemption UI
frontend/src/app/partner/employee/page.tsx - Admin governance UI · Approvals, operations, CMS, and dashboards.
frontend/src/app/admin/page.tsx
API and domain services
Express-style API composition and business logic.
- Backend server
backend/src/server.ts - Application assembly
backend/src/app.ts - Role-scoped routers · Composes common, customer, partner, and admin API trees.
backend/src/routers/index.ts - Authentication and roles · Authenticates callers and enforces protected role boundaries.
backend/src/middlewares/auth.middleware.ts - Customer commerce · Cart, orders, owned vouchers, reviews, and payment processing.
backend/src/services/customer/order.service.ts - Partner supply and redemption · Voucher campaigns, employees, branches, and redemption.
backend/src/services/partner/voucher.service.ts - Administrative governance · Approval workflows, platform oversight, and dashboards.
backend/src/services/admin/voucher.service.ts - Identity and OTP · Role-specific auth, registration, password reset, and email OTP.
backend/src/services/common/auth.service.ts - Voucher image upload
backend/src/middlewares/voucher-image-upload.middleware.ts
External integrations
- Payment providers · Stripe, PayPal, VNPay, and ZaloPay initiation and return handling.
backend/src/config/stripe.ts - Cloudflare R2 · Stores uploaded voucher images.
backend/src/services/storage/r2.service.ts
Data and delivery
- SQL database · Persistent marketplace data.
backend/src/config/db.ts - Schema and seed data
database/scripts/init.sql - Voucher crawler pipeline · Extracts, normalizes, and seeds voucher data.
backend/src/crawler/index.ts - Frontend deployment
.github/workflows/deploy-frontend.yml - Backend deployment
.github/workflows/deploy-backend.yml
Connections 22
- 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