Case Study · Team Project

Yox Fashion

E-commerce platform engineered with Clean Architecture, atomic inventory reservation, idempotent Razorpay webhook reconciliation, and granular RBAC.

Next.jsExpressTypeScriptClean ArchitectureMongoDBRedisRazorpayPM2Hostinger VPS

Race-Condition-Safe Inventory Reservation & Compensating Rollback

Problem: Concurrent checkouts for limited-stock garment variants risk overselling, while partial failures in multi-item carts can leave inventory permanently decremented.
Solution:
  • Engineered AtomicStockService using atomic conditional database updates (available >= quantity).
  • Added an in-process withVariantMutex fallback for non-atomic test repositories.
  • Implemented a compensating rollback mechanism that automatically releases all previously reserved variants if any item in a multi-item checkout fails.
Impact: Eliminated overselling under concurrent load and guaranteed stock consistency across multi-item orders.

Idempotent Payment Webhook Processing & Signature Verification

Problem: Asynchronous payment webhooks (Razorpay payment.captured, payment.failed, refund.processed) can arrive out of order, duplicate, or race against client-side verification callbacks.
Solution:
  • Built RazorpayWebhookUseCase with raw request body capture (req.rawBody) for strict HMAC-SHA256 signature verification.
  • Paired with MongooseWebhookEventDeduplicator backed by a unique compound index on (provider, eventId).
  • Duplicate PROCESSING/PROCESSED events are blocked, while FAILED events are atomically reclaimed via findOneAndUpdate for safe retries.
Impact: Guaranteed exactly-once financial reconciliation and prevented duplicate order confirmations or double stock deductions.

Authoritative Server-Side Checkout Pricing Engine

Problem: Relying on client-supplied cart prices or stale cached totals exposes checkout to price tampering and promotion abuse.
Solution:
  • Architected CheckoutPricingService to ignore client-supplied prices.
  • Dynamically resolves live catalog pricing, variant availability, active offer/coupon rules, store-wide tax inclusivity (taxRatePercent), and COD limits (codMaxLimit) directly from MongoDB.
Impact: Eliminated client-side price manipulation vectors and ensured deterministic financial calculations across cart, direct "Buy It Now", and webhook flows.

Order Aggregate State Machine & Immutable Snapshots

Problem: Order lifecycles spanning placement, packing, Shiprocket dispatch, delivery, cancellation, and returns require strict transition guards and historical data integrity.
Solution:
  • Modeled Order as a Domain Aggregate Root enforcing OrderStatus and PaymentStatus invariants.
  • Persisted immutable OrderItemSnapshot and ShippingAddressSnapshot value objects at order creation.
Impact: Prevented invalid state transitions and decoupled historical invoices/receipts from future catalog or address mutations.

Passwordless OTP Authentication, Token Rotation & Granular RBAC

Problem: Supporting frictionless mobile-first customer sign-in alongside multi-tier admin/staff access requires strict privilege isolation and brute-force protection.
Solution:
  • Implemented phone-first OTP authentication (Msg91OtpService) with multi-account resolution.
  • Configured separate signed JWT secrets (JWT_ACCESS_SECRET, JWT_REFRESH_SECRET, JWT_RESET_SECRET) validated at boot via Zod.
  • Enforced route-level requireAuth / requirePermission middleware protected by helmet, CORS whitelisting, and express-rate-limit.
Impact: Isolated customer and staff/admin security boundaries and enforced fail-fast environment validation on startup.

Low-Latency Caching, Zero-Downtime Deployment & Automated Backups

Problem: Maintaining fast storefront reads and reliable disaster recovery on a single-VPS production setup.
Solution:
  • Integrated Redis (ioredis) for storefront caching alongside Next.js SSR/ISR and Cloudinary CDN delivery.
  • Automated zero-downtime PM2 reloads (yox-api, yox-web) via deploy.sh.
  • Scheduled compressed mongodump --gzip backups with 7-day rolling retention via backup-db.sh.
Impact: Reduced database read pressure on catalog endpoints and established repeatable deployments with automated point-in-time recovery.