Role Management System
Relevant source files
The following files were used as context for generating this wiki page:
- marketplace-backend/app/src/modules/role/role.module.ts
- marketplace-backend/app/src/modules/role/controllers/role.controller.ts
- marketplace-backend/app/src/services/impl/roles.service.ts
- marketplace-backend/app/src/enums/user-role.enum.ts
- marketplace-backend/app/src/schemas/role.schema.ts
- marketplace-backend/app/src/dto/role.dto.ts
- marketplace-backend/app/src/services/abstract-roles.service.ts
The Role Management System defines and manages user permissions within the marketplace application. It provides the backend infrastructure to create roles, retrieve role details by type, and associate these roles with user entities such as customers.
System Architecture
The system uses a modular NestJS architecture. The RoleModule encapsulates the controllers, services, and database schemas required for role operations. It leverages Mongoose for object modeling and MongoDB for persistence.
The diagram above illustrates the dependency injection and layered structure of the role management components.
Sources: marketplace-backend/app/src/modules/role/role.module.ts, marketplace-backend/app/src/services/impl/roles.service.ts
Component Responsibilities
| Component | Responsibility |
|---|---|
RoleController | Handles incoming HTTP requests for role retrieval and creation. |
RolesService | Implements business logic, generates unique IDs, and interacts with the database. |
RoleSchema | Defines the data structure and validation rules for role documents in MongoDB. |
UserRole | Defines a static enumeration of supported role types. |
Sources: marketplace-backend/app/src/modules/role/controllers/role.controller.ts, marketplace-backend/app/src/services/impl/roles.service.ts, marketplace-backend/app/src/schemas/role.schema.ts
Data Models and Types
Role Schema
The Role class defines the structure of a role document. Every role requires a unique identifier and a type.
| Field | Type | Description | Constraints |
|---|---|---|---|
id | String | Unique identifier for the role. | Required, Unique |
type | String | The name or category of the role. | Required, Unique |
description | String | Optional text describing role permissions. | Max length 60 |
Sources: marketplace-backend/app/src/schemas/role.schema.ts:13-25, marketplace-backend/app/src/dto/role.dto.ts:5-15
UserRole Enumeration
The system restricts role types to a predefined set of values to ensure consistency.
DefaultAdminManager
Sources: marketplace-backend/app/src/enums/user-role.enum.ts:1-5
API Endpoints
The RoleController exposes endpoints under the /api/v1/role prefix.
Get Role by Type
Retrieves a specific role definition based on the provided role type.
- URL:
GET /api/v1/role/:type - Parameter:
type(UserRole enum) - Response:
Promise<IRole>
Create New Role
Adds a new role definition to the system.
- URL:
POST /api/v1/role/create - Body:
RoleDto - Response:
Promise<IRole>
The sequence diagram shows the flow of creating a new role, including the automatic generation of a UUID.
Sources: marketplace-backend/app/src/modules/role/controllers/role.controller.ts:16-25, marketplace-backend/app/src/services/impl/roles.service.ts:21-25
Service Implementation
The RolesService extends AbstractRolesService to provide concrete implementations for role management.
- Role Retrieval: The
getRoleByTypemethod uses the MongoosefindOnequery to find a role matching the providedUserRoleenum. - Role Creation: The
createRolemethod assigns a new version 4 UUID to theRoleDto.idfield before persisting the entity to the database.
Sources: marketplace-backend/app/src/services/impl/roles.service.ts:17-25, marketplace-backend/app/src/services/abstract-roles.service.ts:7-11
Summary
The Role Management System establishes the foundational authorization entities for the marketplace. It utilizes a clear separation of concerns through NestJS modules and provides a controlled interface for role lifecycle management via a RESTful API. This system ensures that all roles possess unique identifiers and conform to the application's predefined UserRole specifications.