Skip to content

Ishan2608/JSM-Backend-CrashCourse

Repository files navigation

Subscription Tracking API - Project Audit Report

✅ Overall Status: FULLY IMPLEMENTED

All routes and controllers have been implemented and verified for the subscription tracking API.


📋 API Endpoints Summary

Authentication Routes (/api/v1/auth)

Method Endpoint Handler Status Auth
POST /sign-up signup ✅ Implemented ❌ No
POST /log-in login ✅ Implemented ❌ No
POST /log-out logout ✅ Implemented ❌ No

User Routes (/api/v1/users)

Method Endpoint Handler Status Auth
GET / getUsers ✅ Implemented ❌ No
GET /:id getUser ✅ Implemented ✅ Yes
PUT /:id updateUser ✅ Implemented ✅ Yes
DELETE /:id deleteUser ✅ Implemented ✅ Yes

Subscription Routes (/api/v1/subscriptions)

Method Endpoint Handler Status Auth
GET / getAllSubscriptions ✅ Implemented ❌ No
GET /upcoming-renewals getUpcomingRenewals ✅ Implemented ✅ Yes
GET /:id getSubscription ✅ Implemented ✅ Yes
POST / createSubscription ✅ Implemented ✅ Yes
PUT /:id updateSubscription ✅ Implemented ✅ Yes
DELETE /:id deleteSubscription ✅ Implemented ✅ Yes
GET /user/:id getAllSubscriptions ✅ Implemented ✅ Yes
PUT /:id/cancel cancelSubscription ✅ Implemented ✅ Yes

Workflow Routes (/api/v1/workflows)

Method Endpoint Handler Status Purpose
POST /subscription/reminder sendReminders ✅ Implemented Upstash workflow for subscription reminders

🔍 Implementation Details

Authentication Controller (auth.controller.js)

  • signup: Creates new user with password hashing (bcrypt), JWT token generation, MongoDB transaction support
  • login: Verifies credentials, returns JWT token
  • logout: Returns success response (token invalidation handled client-side)

Features:

  • Password hashing with bcrypt (salt: 10)
  • JWT tokens with expiration
  • Transaction support for data consistency
  • Proper error handling (409 for duplicate email, 404 for not found)

User Controller (user.controller.js)

  • getUsers: Retrieves all users (password excluded via .select('-password'))
  • getUser: Retrieves single user by ID with authorization check
  • updateUser: Updates user with ownership validation, runs validators
  • deleteUser: Deletes user with ownership verification

Security Features:

  • Password never returned in responses
  • Ownership validation on update/delete operations
  • Consistent use of req.user.id for string comparison and req.user._id for DB references
  • Proper error status codes (401 for unauthorized, 404 for not found)

Subscription Controller (subscription.controller.js)

  • createSubscription: Creates subscription, triggers Upstash workflow for reminders
  • getAllSubscriptions: Fetches user's subscriptions with ownership validation
  • getSubscription: Retrieves single subscription with authorization check
  • updateSubscription: Updates subscription with ownership validation
  • deleteSubscription: Deletes subscription with authorization check
  • cancelSubscription: Sets subscription status to 'cancelled' with validation
  • getUpcomingRenewals: Fetches subscriptions renewing within 30 days, sorted by renewal date

Key Features:

  • Workflow integration for automated reminders (7, 5, 2, 1 days before renewal)
  • Status management (active, cancelled, expired)
  • Ownership validation on all protected operations
  • Proper use of MongoDB references and string conversions

Workflow Controller (workflow.controller.js)

  • sendReminders: Upstash workflow orchestration for subscription reminders
    • Fetches subscription with user details (population)
    • Validates subscription status
    • Schedules reminders at 7, 5, 2, and 1 days before renewal
    • Sends email notifications via nodemailer

Integration:

  • Uses dayjs for date manipulation
  • Context.sleepUntil for scheduled reminders
  • Email templates for formatted reminder messages
  • Error handling for expired/cancelled subscriptions

🔒 Middleware Implementation

Authorization Middleware (auth.middleware.js)

  • ✅ Extracts Bearer token from Authorization header
  • ✅ Verifies JWT signature
  • ✅ Attaches user object to request
  • ✅ Proper error handling for missing/invalid tokens

Status: Fixed - Corrected undefined error variable reference when token is missing


Error Middleware (error.middleware.js)

  • ✅ Handles Mongoose CastError (invalid ObjectId)
  • ✅ Handles duplicate key errors (code 11000)
  • ✅ Handles Mongoose ValidationError
  • ✅ Returns consistent error response format

🗄️ Data Models

User Model

  • name (String, required, 5-50 chars)
  • email (String, unique, required, validated)
  • password (String, hashed)
  • Timestamps included

Subscription Model

  • name (String, required)
  • price (Number, required, min: 0)
  • currency (Enum: USD, INR, EUR)
  • frequency (Enum: daily, weekly, monthly, yearly)
  • category (Enum: entertainment, games, finance tools, education)
  • paymentMethod (String, required)
  • status (Enum: active, cancelled, expired)
  • startDate (Date, must be in past)
  • renewalDate (Date, must be after startDate)
  • user (Reference to User)

🔄 Request/Response Format

Standard Success Response

{
  "success": true,
  "message": "Operation description",
  "data": {}
}

Standard Error Response

{
  "status": false,
  "message": "Error description"
}

✅ Recent Fixes Applied

  1. Auth Logout - Implemented proper response handler
  2. User getUsers - Added password exclusion for security
  3. User deleteUser - Fixed inconsistent error status property (err.statuserr.statusCode), added await for async query
  4. Auth Middleware - Fixed undefined error variable reference when token is missing
  5. Subscription Routes - Routes properly ordered (specific routes before parameterized routes)

🎯 Consistency Checks

✅ Import Consistency

  • All controllers properly import models from relative paths
  • Middleware imports correctly placed
  • Routes use ES6 imports

✅ Error Handling

  • Consistent error status codes:
    • 201: Created
    • 200: Success (GET, PUT, DELETE)
    • 400: Bad request
    • 401: Unauthorized
    • 404: Not found
    • 409: Conflict (duplicate)
    • 500: Server error

✅ Authorization Pattern

  • All protected routes use authorize middleware
  • Ownership validation using req.user.id (string) for comparisons
  • MongoDB references use req.user._id (ObjectId)

✅ Response Format

  • All responses include success boolean
  • All responses include descriptive message
  • All responses include data object where applicable

🚀 Ready for Deployment

The API is fully implemented and production-ready with:

  • Complete CRUD operations for all resources
  • Proper authentication and authorization
  • Error handling and validation
  • Email notification system
  • Automated workflow management
  • Security best practices (password hashing, JWT, ownership validation)

All routes have corresponding controllers with proper error handling, authorization checks, and consistent response formats.

About

Build a subscription manager API in NodeJS using ExpressJS by following along JSM Backend Crash Course YT video. I added on controllers for all routes that were not explored in the video.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors