Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Implement User Profile Management API Endpoints #5

Merged
merged 24 commits into from
Jan 9, 2025

Conversation

TKanX
Copy link
Owner

@TKanX TKanX commented Jan 9, 2025

Summary:

Implemented a comprehensive user profile management system with the following features:

  • Profile Viewing and Editing: Endpoints to view and update user profiles, including attributes like name, avatar, school, and country.
  • Settings Management: Endpoints to view and update user settings (e.g., time format, theme preferences).
  • User Safety Records: Endpoint to view safety records with pagination support.
  • Account Updates: Endpoints for updating username, email (with verification), and password.

Changes

  1. Added Backend API Endpoints:

    • User Data Retrieval:
      • GET /users/:id: Retrieves user data, excluding sensitive information.
    • Settings Management:
      • GET /users/:id/settings: Fetches user settings.
      • PATCH /users/:id/settings: Updates user settings, ensuring existing settings are preserved.
    • Safety Records Retrieval:
      • GET /users/:id/safety-records: Retrieves user safety records with pagination (query params: limit, offset).
    • Profile Updates:
      • PATCH /users/:id/profile: Updates user profile data (e.g., avatar, school, name).
    • Username Updates:
      • PUT /users/:id/username: Updates username after validating input.
    • Email Updates:
      • POST /users/:id/email: Sends a verification email for email updates.
      • POST /users/:id/email/complete: Completes the email update after token validation.
    • Password Updates:
      • PUT /users/:id/password: Updates user password after verifying current password.
  2. Model and Schema Changes:

    • Added a profile subdocument to the User model for attributes like avatar, school, and country.
    • Refactored the settings subdocument to exclude unnecessary _id fields.
    • Added default values for roles and locked fields, and ensured proper indexing for query performance.
  3. Utility Enhancements:

    • Added validation functions for avatar size and username/email format.
    • Refactored JWT operations to use a new jwtUtils module.
    • Moved password hashing and validation functions to the utils module for better reusability.
  4. Documentation Updates:

    • Updated API.md to include detailed descriptions and example requests/responses for all new user endpoints.

TKanX added 23 commits January 5, 2025 11:51
…and update validation functions

- Refactored `userSchema` to include a `profile` object containing `name`, `avatar`, `birthday`, `school`, and `country`.
- Moved `name`, `school`, and `country` fields into the `profile` object.
- Added `avatar` and `birthday` fields to the `profile` object.
- Updated validation functions in `validationUtils.js` to include `validateAvatar` and `validateBirthday`.
- Ensured `profile` fields are properly validated and default values are set.
… schema

- Added indexes for `roles` and `locked` fields in `userSchema` to improve query performance.
…entation

- Added `getUser` endpoint in `userRoutes.js` to handle requests for retrieving user details by ID.
- Implemented `getUser` controller in `userControllers.js` to fetch user details from the database.
- Updated `API.md` to include documentation for the `getUser` endpoint.
- Included request parameters and response examples for the new endpoint.
…ery parameters

- Added `validateLimit` function in `validationUtils.js` to validate limit query parameters.
- Added `validateOffset` function in `validationUtils.js` to validate offset query parameters.
- Ensured both functions check for valid numeric values within acceptable ranges.
- Exported `validateLimit` and `validateOffset` functions for use in other modules.
…documentation

- Added `getSafetyRecords` endpoint in `userRoutes.js` to handle requests for retrieving user safety records.
- Implemented `getSafetyRecords` controller in `userControllers.js` to fetch safety records from the database with pagination support.
- Updated `API.md` to include documentation for the `getSafetyRecords` endpoint.
- Included request parameters, query parameters, and response examples for the new endpoint.
…tation

- Added `getSettings` endpoint in `userRoutes.js` to handle requests for retrieving user settings.
- Implemented `getSettings` controller in `userControllers.js` to fetch user settings from the database.
- Updated `API.md` to include documentation for the `getSettings` endpoint.
- Included request parameters and response examples for the new endpoint.
- Updated `settingsSchema` in `userSchema.js` to explicitly set `_id: false` to prevent storing an `_id` field in the settings subdocument.
- Ensured the `settings` subdocument is properly configured and validated.
…mentation

- Added `updateUsername` endpoint in `userRoutes.js` to handle requests for updating a user's username.
- Implemented `updateUsername` controller in `userControllers.js` to validate the new username, check for conflicts, and update the user's username in the database.
- Updated `API.md` to include documentation for the `updateUsername` endpoint.
- Included request parameters, request body, and response examples for the new endpoint.
- Updated the not found handler in `index.js` to use `router.use('*', ...)` instead of `router.get('*', ...)`.
- Ensured all unmatched routes are properly handled and return a 404 response.
…tation

- Added `updateEmail` endpoint in `userRoutes.js` to handle requests for updating a user's email.
- Implemented `updateEmail` controller in `userControllers.js` to validate the new email, check for conflicts, generate a verification token, and send a verification email.
- Updated `API.md` to include documentation for the `updateEmail` endpoint.
- Included request parameters, request body, and response examples for the new endpoint.
…I documentation

- Added `completeEmailUpdate` endpoint in `userRoutes.js` to handle requests for completing an email update.
- Implemented `completeEmailUpdate` controller in `userControllers.js` to verify the email update token and update the user's email in the database.
- Updated `API.md` to include documentation for the `completeEmailUpdate` endpoint.
- Included request parameters, request body, and response examples for the new endpoint.
…ser routes

- Updated `userRoutes.js` to change the HTTP method for the email update endpoint from `PUT` to `POST`.
- Ensured the `updateEmail` endpoint correctly handles email update requests with the new method.
- Updated `API.md` documentation to reflect the change in the HTTP method for the email update endpoint.
…mentation

- Added `updatePassword` endpoint in `userRoutes.js` to handle requests for updating a user's password.
- Implemented `updatePassword` controller in `userControllers.js` to validate the current and new passwords, verify the current password, and update the user's password in the database.
- Updated `API.md` to include documentation for the `updatePassword` endpoint.
- Included request parameters, request body, and response examples for the new endpoint.
…entation

- Added `updateProfile` endpoint in `userRoutes.js` to handle requests for updating a user's profile.
- Implemented `updateProfile` controller in `userControllers.js` to validate and update profile fields such as name, avatar, birthday, school, and country.
- Updated `API.md` to include documentation for the `updateProfile` endpoint.
- Included request parameters, request body, and response examples for the new endpoint.
…ction

- Updated `validateAvatar` function in `validationUtils.js` to include size validation for base64 avatars.
- Ensured the avatar size is less than or equal to 5MB.
…mentation

- Added `updateSettings` endpoint in `userRoutes.js` to handle requests for updating a user's settings.
- Implemented `updateSettings` controller in `userControllers.js` to validate and update settings fields such as time format, date format, and theme.
- Updated `API.md` to include documentation for the `updateSettings` endpoint.
- Included request parameters, request body, and response examples for the new endpoint.
…settings

- Updated `updateSettingsById` function in `userService.js` to preserve existing settings when updating user settings.
- Ensured only provided settings fields are updated, leaving other fields unchanged.
- Validated and applied partial updates to the `settings` subdocument.
… profiles

- Added `updateProfileById` function in `userService.js` to update user profiles by ID.
- Preserved existing profile fields when updating to avoid resetting unspecified fields.
- Exported the `updateProfileById` function for use in other modules.
… function

- Updated `updateProfile` controller in `userControllers.js` to use `updateProfileById` function from `userService.js`.
- Ensured the `updateProfileById` function validates and updates profile fields such as name, avatar, birthday, school, and country.
- Preserved existing profile fields when updating to avoid resetting unspecified fields.
…rations

- Moved JWT operations from `jwtService` to `jwtUtils` for better organization.
- Updated all references to JWT operations in controllers and middleware to use `jwtUtils`.
- Ensured all JWT operations such as token generation, verification, and decoding are handled by `jwtUtils`.
… update imports

- Moved `hashPassword` and `verifyPassword` functions from `passwordHashService` to `passwordHashUtils`.
- Updated all imports in `userService.js` and `userControllers.js` to use `passwordHashUtils` for password hashing and verification.
- Ensured all password-related operations are handled by the utility functions.
@TKanX TKanX added documentation 📖 Improvements or additions to documentation enhancement ✨ New feature or request labels Jan 9, 2025
@TKanX TKanX self-assigned this Jan 9, 2025
@Copilot Copilot bot review requested due to automatic review settings January 9, 2025 20:20
@TKanX TKanX linked an issue Jan 9, 2025 that may be closed by this pull request
49 tasks
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 7 out of 12 changed files in this pull request and generated 1 comment.

Files not reviewed (5)
  • backend/src/services/emailService.js: Evaluated as low risk
  • backend/src/controllers/authControllers.js: Evaluated as low risk
  • backend/src/middlewares/authMiddleware.js: Evaluated as low risk
  • backend/src/utils/passwordHashUtils.js: Evaluated as low risk
  • backend/src/utils/jwtUtils.js: Evaluated as low risk

backend/src/models/userSchema.js Outdated Show resolved Hide resolved
- Updated `birthday` field in `userSchema` to use a function for the `max` value: `max: () => Date.now()`.
- Ensured the `max` value is dynamically calculated at the time of validation, reflecting the current date.
- Fixed the issue where the `max` value was set to the schema definition time, causing incorrect validation for new documents.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@TKanX TKanX merged commit dcae1cd into main Jan 9, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation 📖 Improvements or additions to documentation enhancement ✨ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement User Profile Management Functionality
1 participant