How mdrakibmia99/swiftcart-apis works
Purpose and Runtime
SwiftCart is a TypeScript Node.js/Express REST backend for an e-commerce platform. It exposes APIs for catalog, shops, customer accounts, orders, reviews, promotions, and payments. MongoDB/Mongoose is the persistent store; it is deployable with Docker and Vercel configuration. Process entry is src/server.ts, which starts the Express application assembled in src/app.ts.
Application Composition and HTTP Boundary
The Express app centralizes middleware, route registration, missing-route handling, and global error translation in src/app.ts. The route aggregator is src/app/routes/index.ts; it mounts module route files, keeping transport concerns separate from domain implementations. Requests commonly traverse authentication, request parsing/validation, controller, service, and Mongoose model layers.
• src/app.ts
• src/app/routes/index.ts
• src/app/middleware/globalErrorHandler.ts
Authentication and Authorization
The auth domain owns registration/login and JWT-oriented credential flows, while the auth middleware protects downstream routes and likely establishes request identity/roles. User records are the account authority, with customer/shop concepts modeled separately for commerce-specific profiles and operations.
• src/app/modules/auth/auth.routes.ts
• src/app/modules/auth/auth.service.ts
• src/app/middleware/auth.ts
• src/app/modules/user/user.model.ts
Catalog and Seller Domain
Product discovery and management are centered on products, categories, brands, and shops. Each domain follows a consistent route → controller → service → model structure. Product query behavior is abstracted through a reusable query builder, establishing the search/filter/pagination boundary rather than embedding query logic in controllers.
• src/app/modules/product/product.service.ts
• src/app/modules/shop/shop.service.ts
• src/app/modules/category/category.model.ts
• src/app/builder/QueryBuilder.ts
Commerce Lifecycle
Order processing is the core transactional domain, backed by order persistence and service logic. Coupons and flash sales contribute pricing/promotion rules; reviews connect customers back to purchased/discovered products. Invoice PDF generation is an outbound artifact associated with order completion.
• src/app/modules/order/order.service.ts
• src/app/modules/order/order.model.ts
• src/app/modules/coupon/coupon.service.ts
• src/app/modules/flashSell/flashSale.service.ts
• src/app/utils/generateOrderInvoicePDF.ts
Payments and External Integrations
Payment behavior is split between a general payment module and an SSLCommerz-specific gateway module, including callback-facing routes. The configured external dependencies are Stripe/SSLCommerz for payments, Cloudinary for image storage/upload handling, and Nodemailer for email. These form integration boundaries around otherwise MongoDB-backed domain services.
• src/app/modules/payment/payment.service.ts
• src/app/modules/sslcommerz/sslcommerz.service.ts
• src/app/config/cloudinary.config.ts
• src/app/config/multer.config.ts
• src/app/utils/emailHelper.ts
Validation, Errors, and API Contracts
Module-specific validation files enforce input contracts before services operate. Typed application errors and dedicated Mongo/Mongoose/Zod error mappers converge at the global error handler, providing a consistent client-facing failure boundary. Controllers use shared async and response utilities to normalize successful and failed request execution.
• src/app/middleware/validateRequest.ts
• src/app/errors/appError.ts
• src/app/errors/handleValidationError.ts
• src/app/utils/catchAsync.ts
• src/app/utils/sendResponse.ts
Main entry points: index.ts Route registry · order.service.ts Orders · product.service.ts Catalog and shops · app.ts Express app
Components 20
HTTP API
- Server entry · Starts the Express application.
src/server.ts - Express app · Configures middleware, routes, and error handling.
src/app.ts - Route registry · Mounts module HTTP routes.
src/app/routes/index.ts - Auth middleware · Protects routes and establishes request identity.
src/app/middleware/auth.ts - Request validation · Enforces module input contracts.
src/app/middleware/validateRequest.ts - Global error handler · Translates application and validation failures to API responses.
src/app/middleware/globalErrorHandler.ts
Commerce Domains
- Authentication · Registration, login, and credential flows.
src/app/modules/auth/auth.service.ts - User accounts · Account authority for identities and roles.
src/app/modules/user/user.model.ts - Catalog and shops · Product discovery and seller/shop management.
src/app/modules/product/product.service.ts - Categories
src/app/modules/category/category.model.ts - Orders · Core transactional commerce lifecycle.
src/app/modules/order/order.service.ts - Order records
src/app/modules/order/order.model.ts - Promotions · Coupon and flash-sale pricing rules.
src/app/modules/coupon/coupon.service.ts - Payments · General payment processing and SSLCommerz gateway handling.
src/app/modules/payment/payment.service.ts
Platform Services
- Query builder · Applies catalog search, filters, and pagination.
src/app/builder/QueryBuilder.ts - Invoice PDF · Produces order-completion invoice artifacts.
src/app/utils/generateOrderInvoicePDF.ts
External Integrations
- MongoDB · Mongoose-backed domain persistence.
- Stripe / SSLCommerz · External payment gateway boundary.
- Cloudinary uploads · Image upload and storage boundary.
src/app/config/cloudinary.config.ts - Email delivery
src/app/utils/emailHelper.ts
Connections 23
- Server entry → Express app: starts
- Express app → Route registry: registers
- Express app → Global error handler: uses
- Route registry → Request validation: validates requests
- Route registry → Auth middleware: protects routes
- Route registry → Authentication: auth endpoints
- Route registry → Catalog and shops: catalog endpoints
- Route registry → Orders: order endpoints
- Route registry → Payments: payment callbacks
- Auth middleware → User accounts: resolves identity
- Authentication → User accounts: manages accounts
- Catalog and shops → Categories: organizes products
- Catalog and shops → Query builder: builds discovery queries
- Catalog and shops → MongoDB: persists
- Orders → Order records: persists
- Orders → Promotions: applies pricing rules
- Orders → Payments: collects payment
- Orders → Invoice PDF: generates on completion
- Order records → MongoDB: stored in
- Payments → Stripe / SSLCommerz: charges via
- Catalog and shops → Cloudinary uploads: stores product images
- Orders → Email delivery: sends notifications
- Request validation → Global error handler: validation failures