Customer Management
Relevant source files
The following files were used as context for generating this wiki page:
- marketplace-backend/app/src/schemas/customer.schema.ts
- marketplace-backend/app/src/repositories/impl/customers.repository.ts
- marketplace-backend/app/src/modules/customers/controllers/customers.controller.ts
- marketplace-backend/app/src/modules/auth/controllers/auth.controller.ts
- marketplace-frontend/src/app/models/authorization/customer.model.ts
- marketplace-backend/app/src/modules/customers/customers.module.ts
- marketplace-backend/app/src/mappers/customer-to-dto.mapper.ts
Customer Management handles the lifecycle, authentication, and data persistence of users within the marketplace. The system provides internal administrative endpoints for managing customer records and public authentication endpoints for registration and login.
Data Model
The system defines customers using a Mongoose schema on the backend and TypeScript interfaces on the frontend. The backend schema enforces uniqueness for identifiers and credentials while linking customers to specific roles.
Backend Schema
The Customer class defines the structure for MongoDB persistence.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | String | Unique, Required | Primary unique identifier (UUID) |
userName | String | Unique, Required | Unique handle for the user |
email | String | Unique, Required | User's electronic mail address |
password | String | Required | Hashed user password |
firstName | String | Required | User's first name |
age | Number | Optional | User's age (defaults to 18) |
role | Role | ObjectId Ref | Reference to the Role schema |
Sources: marketplace-backend/app/src/schemas/customer.schema.ts:13-37, marketplace-backend/app/src/repositories/impl/customers.repository.ts:28.
Frontend Model
The frontend uses the Customer interface for type safety during API interactions and state management.
export interface Customer extends Base {
firstName: string;
userName: string;
email: string;
age?: number;
password?: string;
}
Sources: marketplace-frontend/src/app/models/authorization/customer.model.ts:3-9
Backend Architecture
The backend implements a modular architecture using NestJS, separating concerns into controllers, services, and repositories.
The diagram shows the dependency flow from the API layer down to the data persistence layer.
Sources: marketplace-backend/app/src/modules/customers/customers.module.ts:11-38, marketplace-backend/app/src/repositories/impl/customers.repository.ts:15-18
Repositories and Services
The CustomersRepository handles direct database operations. It generates unique IDs using uuidv4 and hashes passwords using bcrypt before storage.
- Creation: Assigns a default role and a default age of 18 if not provided.
- Retrieval: Provides methods to find customers by ID, username, or email, often populating the
rolefield.
Sources: marketplace-backend/app/src/repositories/impl/customers.repository.ts:20-56
API Endpoints
The system exposes two primary controllers for customer-related operations: CustomersController for management and AuthController for session handling.
Management Endpoints (/api/v1/customerManagement)
| Method | Path | Description |
|---|---|---|
| GET | /all | Retrieves a list of all registered customers. |
| GET | /:id | Retrieves a specific customer by their ID. |
| POST | /create | Manually creates a new customer record. |
Sources: marketplace-backend/app/src/modules/customers/controllers/customers.controller.ts:9-30
Authentication Endpoints (/api/v1/auth)
Authentication logic utilizes JWT tokens stored in HTTP-only cookies and caches customer data to improve performance.
This diagram illustrates the login flow and data caching mechanism.
Sources: marketplace-backend/app/src/modules/auth/controllers/auth.controller.ts:63-75
- Caching: The
AuthControllerstores customer DTOs in a cache (viaCACHE_MANAGER) using the prefixid_followed by the customer ID. - Mappers: The
customerToDtoMapperutility strips sensitive information like passwords before sending data to the client.
Sources: marketplace-backend/app/src/modules/auth/controllers/auth.controller.ts:21, marketplace-backend/app/src/mappers/customer-to-dto.mapper.ts:4-15
Summary
Customer Management provides a secure foundation for user interactions. It ensures data integrity through Mongoose schemas and unique constraints, protects credentials via bcrypt hashing, and optimizes performance through NestJS caching and JWT-based session management.