/backend

Primary LanguageJavaScript

Garbage Collection Application Backend

Introduction

This document provides comprehensive documentation for the backend of the garbage collection application. It covers the technical aspects, functionalities, and implementation details of the backend system.

Technologies Used

  • Node.js: Backend runtime environment.
  • Express.js: Web application framework for routing and middleware.
  • MongoDB: NoSQL database for storing application data.
  • Mongoose: Object Data Modeling (ODM) library for MongoDB.
  • JWT (JSON Web Tokens): Used for user authentication and authorization.
  • bcrypt: Hashing algorithm for securely storing passwords.
  • Express Validator: Middleware for input validation.
  • Helmet.js: Middleware for enhancing API security.
  • Morgan: HTTP request logger middleware.

Architecture Overview

The backend follows a RESTful architecture with the following components:

  • Controllers: Handle incoming HTTP requests, process data, and send responses.
  • Models: Define data schemas and interact with the MongoDB database using Mongoose.
  • Routes: Define API endpoints and map them to controller functions.
  • Middleware: Authentication, authorization, input validation, error handling, etc.

API Endpoints

User Management

  • POST /api/users/register: Register a new user.
  • POST /api/users/login: User login authentication.
  • GET /api/users/profile: Get user profile details.
  • PUT /api/users/update: Update user profile information.

Order Management

  • POST /api/orders/place: Place a new order for garbage collection.
  • PUT /api/orders/schedule: Schedule an order for collection.
  • DELETE /api/orders/cancel/:orderId: Cancel a scheduled order.
  • GET /api/orders/user: Get all orders for the current user.
  • GET /api/orders/all: Get all orders (admin only).

Payment Management

  • POST /api/payments/make: Make a payment for an order.
  • POST /api/payments/refund/:orderId: Refund a payment for a canceled order.
  • GET /api/payments/all: Get all payment transactions (admin only).

Database Models

User Model

  • name: String
  • email: String (unique)
  • password: String (hashed)

Order Model

  • userId: ObjectId (ref: User)
  • orderDate: Date
  • scheduledDate: Date
  • status: String (e.g., 'Placed', 'Scheduled', 'Canceled')
  • paymentId: ObjectId (ref: Payment)

Payment Model

  • orderId: ObjectId (ref: Order)
  • amount: Number
  • paymentDate: Date
  • status: String (e.g., 'Success', 'Refunded')

Middleware and Utilities

  • Authentication Middleware: Verify user tokens.
  • Authorization Middleware: Restrict certain routes to admin users only.
  • Input Validation Middleware: Validate incoming request data.
  • Error Handling Middleware: Handle validation errors, database errors, etc.
  • Logger Middleware (Morgan): Log HTTP requests for debugging.

Error Handling

  • Custom error handling middleware to catch and format errors.
  • Different error messages for various scenarios (e.g., validation errors, server errors).

Security Measures

  • Passwords stored securely using bcrypt hashing.
  • JWT tokens for user authentication and authorization.
  • Input validation to prevent malicious data inputs.
  • Helmet.js middleware for setting secure HTTP headers.

Deployment

  • Backend deployed on a cloud platform for scalability and reliability.
  • Environment variables used for sensitive information (e.g., database credentials).