User Profile Module
Authenticated profile read and update APIs for Mechanic Setu users.
The user profile module lets authenticated users read and update their own profile. It reuses the existing JWT authentication middleware, PostgreSQL database pool, error handling, and feature-based Express architecture.
Folder Structure
src/features/users/
controllers/userProfile.controller.js
dtos/userProfile.dto.js
interfaces/userProfile.interfaces.js
repositories/userProfile.repository.js
routes/userProfile.routes.js
services/userProfile.service.js
validators/userProfile.validator.jsControllers only translate HTTP requests and responses. Services hold business rules. Repositories own SQL. DTOs shape public responses and prevent sensitive fields from leaking.
Database Changes
Migration:
src/infrastructure/database/migrations/20260613194500_add_user_profile_columns.sqlAdds these nullable columns to users:
name varchar(100)
email varchar(255)
profile_image textAlso adds:
users_name_lengthcheck constraint.users_email_formatcheck constraint.users_profile_image_urlcheck constraint.users_email_unique_active_idxunique index onlower(email)for active users.
Apply with:
npm run migrateAPIs
Base URL:
/api/v1/usersAll endpoints require:
Authorization: Bearer <accessToken>GET /me
Returns the authenticated user's profile.
Response 200:
{
"success": true,
"message": "Profile loaded successfully",
"data": {
"id": "uuid",
"name": "John Doe",
"phone": "+94771234567",
"email": "john@example.com",
"profileImage": "https://example.com/avatar.png",
"role": "CUSTOMER",
"createdAt": "2026-06-13T00:00:00.000Z",
"updatedAt": "2026-06-13T00:00:00.000Z"
}
}PATCH /me
Updates the authenticated user's own profile. Partial updates are supported.
Request:
{
"name": "John Doe",
"email": "john@example.com",
"profileImage": "https://example.com/avatar.png"
}Response 200:
{
"success": true,
"message": "Profile updated successfully",
"data": {
"id": "uuid",
"name": "John Doe",
"phone": "+94771234567",
"email": "john@example.com",
"profileImage": "https://example.com/avatar.png",
"role": "CUSTOMER",
"createdAt": "2026-06-13T00:00:00.000Z",
"updatedAt": "2026-06-13T00:00:00.000Z"
}
}Validation Rules
| Field | Rules |
|---|---|
name | Optional string, minimum 2 characters, maximum 100 characters. |
email | Optional valid email address, unique across active users. |
profileImage | Optional valid http:// or https:// URL. |
Protected fields are ignored and never written:
id
role
password
phone
createdAt
updatedAtUnexpected fields are rejected with a validation error.
Security Considerations
- JWT authentication is required for both endpoints.
- The authenticated user ID comes from the verified access token.
- Users cannot choose a target user ID in the URL or body.
- Repository updates whitelist only
name,email, andprofileImage. - The response DTO excludes
password_hash, token data, account lock fields, and soft-delete metadata. - Email uniqueness is enforced in both service logic and the database unique index.