hngprojects / hng_boilerplate_nestjs

Description
Apache License 2.0
182 stars 105 forks source link

[FEAT] Implement User Profile Settings Update API Endpoint #156

Closed Ukeme-Edet closed 3 weeks ago

Ukeme-Edet commented 1 month ago

Description

Implement a RESTful API endpoint to handle partial updates of user profile settings. This endpoint will allow authenticated users to modify specific parts of their profile information, such as name, email, or preferences. User preferences will be stored as a JSON field in the user table of the database.

Acceptance Criteria

API Endpoint Implementation:

Data Validation and Authorization:

Updating User Profile:

Response:

Request Example:

PATCH /api/users/profile

{
  "name": "John Doe",
  "email": "john.doe@example.com",
  "preferences": {
    "newsletter": true,
    "darkMode": false
  }
}

Successful Response:

{
  "message": "Profile updated successfully",
  "user": {
    "name": "John Doe",
    "email": "existing.email@example.com",
    "preferences": {
      "newsletter": true,
      "darkMode": false
    }
  },
  "status": 200
}

Error response:

Purpose

Provide a secure and efficient backend service for users to update specific parts of their profile settings, ensuring data validation and maintaining user information integrity.

Requirements

Database Schema

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    name VARCHAR(100),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    preferences JSONB DEFAULT '{}'::jsonb
);

CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_username ON users(username);

image

Expected Outcome

A fully functional API endpoint that securely handles partial user profile updates, maintaining data integrity and providing clear feedback on the success or failure of update attempts.

Status Codes:

Testing

markessien commented 1 month ago

this is unclear how preferences are stored. Are they in a different table from the user model?

Ukeme-Edet commented 1 month ago

@markessien The preferences will be stored as a JSON field within the user table

jovialcore commented 1 month ago

I assume that you are validating the input for preferences too ?

Ukeme-Edet commented 1 month ago

Yes @jovialcore , The inputs will be validated... Thank you