Backend Customers Module
Relevant source files
The following files were used as context for generating this wiki page:
- marketplace-backend/app/src/modules/customers/customers.module.ts
- marketplace-backend/app/src/repositories/impl/customers.repository.ts
- marketplace-backend/app/src/schemas/customer.schema.ts
- marketplace-backend/app/src/modules/auth/controllers/auth.controller.ts
- marketplace-backend/app/src/app.module.ts
- marketplace-backend/app/src/modules/auth/auth.module.ts
The Backend Customers Module manages user-related data, identity persistence, and profile information. It integrates with the Auth Module to support registration and login workflows by providing data access layers for customer records stored in MongoDB.
This module encapsulates the business logic for creating new customers, retrieving profile details, and mapping internal database models to Data Transfer Objects (DTOs) used for API responses.
Sources: marketplace-backend/app/src/modules/customers/customers.module.ts, marketplace-backend/app/src/app.module.ts:10-14
Architecture and Components
The module follows a layered architecture utilizing the NestJS framework. It consists of a controller for handling HTTP requests, a service for business logic, a repository for database interactions, and a Mongoose schema for data definition.
Module Configuration
The CustomersModule imports the MongooseModule to register the Customer and Role schemas. It provides concrete implementations for abstract repository and service classes to ensure loose coupling.
Data Persistence and Schema
The Customer schema defines the structure of the user documents in the MongoDB database. It includes unique identifiers and credentials.
Customer Entity Fields
| Field | Type | Constraints | Description |
|---|---|---|---|
id | String | Unique, Required | UUID version 4 identifier |
userName | String | Unique, Required | User's unique handle |
email | String | Unique, Required | User's email address |
password | String | Required | Bcrypt hashed password |
firstName | String | Required | User's first name |
age | Number | Default: 18 | User's age |
role | ObjectId | Ref: 'Role' | Reference to the Role document |
Sources: marketplace-backend/app/src/schemas/customer.schema.ts:13-41, marketplace-backend/app/src/repositories/impl/customers.repository.ts:25-28
Customer Operations
Creation Workflow
The CustomersRepository handles the persistence logic for new users. When a registration request occurs, the repository performs the following actions:
- Generates a new UUID v4 for the user.
- Hashes the plain-text password using
bcryptwith a salt round of 10. - Assigns a default user role.
- Sets the age to 18 if no age is provided in the request.
Search and Retrieval
The repository provides several methods to query customer data based on different identifiers:
- findCustomerByUserName: Searches for an exact match of the
userNamefield. - findCustomerByEMail: Searches for an exact match of the
emailfield. - findCustomerById: Searches by the UUID
idand populates therolefield to include role type details. - findAllCustomers: Returns a list of all customer documents with populated roles.
Sources: marketplace-backend/app/src/repositories/impl/customers.repository.ts:34-54
Integration with Authentication
The Customers Module is a dependency for the AuthModule. The AuthController uses the AbstractCustomersService to validate credentials during login and to fetch profile data for the /api/v1/auth/account endpoint.
Login Validation Flow
- The controller receives
userNameandpassword. - It fetches the customer document by username via the service.
- It compares the provided password with the stored hash using
validateCredentials. - If valid, it generates a JWT and caches the customer DTO.
Summary
The Backend Customers Module provides the foundational data management for users within the marketplace. By utilizing Mongoose schemas and a dedicated repository pattern, it ensures that user data is validated, passwords are securely hashed, and profiles are efficiently retrieved and cached for performance during authentication sessions.