From 2712f07ca0a87ddb1d1f0c9784221cee44402090 Mon Sep 17 00:00:00 2001 From: Mayura Andrew <48531182+mayura-andrew@users.noreply.github.com> Date: Tue, 10 Sep 2024 10:22:55 +0530 Subject: [PATCH 1/2] Handle Partial Updates to User Profile in updateProfile Function (#166) --- src/controllers/profile.controller.ts | 19 +++++++------------ src/schemas/profile-routes.schema.ts | 8 ++++---- src/services/profile.service.ts | 20 +++++++++++++------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index 61654869..001df3aa 100644 --- a/src/controllers/profile.controller.ts +++ b/src/controllers/profile.controller.ts @@ -49,21 +49,16 @@ export const updateProfileHandler = async ( reject(err) } else { try { + const updateData: Partial = { ...req.body } + if (req.file) { - const image_url = IMG_HOST + '/' + req.file?.filename - const { statusCode, profile, message } = await updateProfile( - user, - { - ...req.body, - image_url - } - ) - return res.status(statusCode).json({ profile, message }) + updateData.image_url = IMG_HOST + '/' + req.file.filename } - const { statusCode, profile, message } = await updateProfile(user, { - ...req.body - }) + const { statusCode, profile, message } = await updateProfile( + user, + updateData + ) return res.status(statusCode).json({ profile, message }) } catch (error) { reject(error) diff --git a/src/schemas/profile-routes.schema.ts b/src/schemas/profile-routes.schema.ts index f4706d1d..18856669 100644 --- a/src/schemas/profile-routes.schema.ts +++ b/src/schemas/profile-routes.schema.ts @@ -1,10 +1,10 @@ import { z } from 'zod' export const updateProfileSchema = z.object({ - primary_email: z.string().email(), - first_name: z.string(), - last_name: z.string(), - image_url: z.string().url() + primary_email: z.string().email().optional(), + first_name: z.string().optional(), + last_name: z.string().optional(), + image_url: z.string().url().optional() }) export const getApplicationsSchema = z.object({ diff --git a/src/services/profile.service.ts b/src/services/profile.service.ts index b3c51564..95c8acdc 100644 --- a/src/services/profile.service.ts +++ b/src/services/profile.service.ts @@ -2,11 +2,12 @@ import { dataSource } from '../configs/dbConfig' import Mentee from '../entities/mentee.entity' import Mentor from '../entities/mentor.entity' import Profile from '../entities/profile.entity' +import { type CreateProfile } from '../types' import { getMentorPublicData } from '../utils' export const updateProfile = async ( user: Profile, - { primary_email, first_name, last_name, image_url }: Partial + updateData: Partial ): Promise<{ statusCode: number profile?: Profile | null @@ -14,14 +15,19 @@ export const updateProfile = async ( }> => { try { const profileRepository = dataSource.getRepository(Profile) + + const { primary_email, first_name, last_name, image_url } = updateData + + const updatedFields: Partial = { + primary_email, + first_name, + last_name, + image_url + } + await profileRepository.update( { uuid: user.uuid }, - { - primary_email, - first_name, - last_name, - image_url - } + updatedFields as CreateProfile ) const savedProfile = await profileRepository.findOneBy({ From d1d6d7a0ee37f9d83fcad55be2c0c2b66450034b Mon Sep 17 00:00:00 2001 From: Disura Randunu <37591051+Disura-Randunu@users.noreply.github.com> Date: Sun, 15 Sep 2024 19:23:37 +0530 Subject: [PATCH 2/2] Capitalize first letter on Signup, Mentor & Mentee Applications (#167) --- src/services/auth.service.ts | 5 +++-- src/services/mentee.service.ts | 8 +++++++- src/services/mentor.service.ts | 11 ++++++++++- src/utils.ts | 4 ++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index b478e984..3c0eb501 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -5,6 +5,7 @@ import { JWT_SECRET } from '../configs/envConfig' import Profile from '../entities/profile.entity' import { type CreateProfile, type ApiResponse } from '../types' import { + capitalizeFirstLetter, getPasswordChangedEmailContent, getPasswordResetEmailContent } from '../utils' @@ -35,8 +36,8 @@ export const registerUser = async ( const newProfile = profileRepository.create({ primary_email: email, password: hashedPassword, - first_name, - last_name, + first_name: capitalizeFirstLetter(first_name), + last_name: capitalizeFirstLetter(last_name), image_url: '' }) diff --git a/src/services/mentee.service.ts b/src/services/mentee.service.ts index c9912ff2..d16c8541 100644 --- a/src/services/mentee.service.ts +++ b/src/services/mentee.service.ts @@ -6,7 +6,8 @@ import { MenteeApplicationStatus } from '../enums' import { getEmailContent, getMentorNotifyEmailContent, - getMenteePublicData + getMenteePublicData, + capitalizeFirstLetter } from '../utils' import { sendEmail } from './admin/email.service' @@ -78,6 +79,11 @@ export const addMentee = async ( } } + application.firstName = capitalizeFirstLetter( + application.firstName as string + ) + application.lastName = capitalizeFirstLetter(application.lastName as string) + const newMentee = new Mentee( MenteeApplicationStatus.PENDING, application, diff --git a/src/services/mentor.service.ts b/src/services/mentor.service.ts index 9178fc54..dd7f4fa0 100644 --- a/src/services/mentor.service.ts +++ b/src/services/mentor.service.ts @@ -5,7 +5,11 @@ import Mentor from '../entities/mentor.entity' import type Profile from '../entities/profile.entity' import { MentorApplicationStatus } from '../enums' import { type PaginatedApiResponse } from '../types' -import { getEmailContent, getMentorPublicData } from '../utils' +import { + capitalizeFirstLetter, + getEmailContent, + getMentorPublicData +} from '../utils' import { sendEmail } from './admin/email.service' export const createMentor = async ( @@ -72,6 +76,11 @@ export const createMentor = async ( } } + application.firstName = capitalizeFirstLetter( + application.firstName as string + ) + application.lastName = capitalizeFirstLetter(application.lastName as string) + const newMentor = new Mentor( MentorApplicationStatus.PENDING, category, diff --git a/src/utils.ts b/src/utils.ts index a7f30f66..839e4232 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -286,3 +286,7 @@ export const getPasswordChangedEmailContent = ( ` } } + +export const capitalizeFirstLetter = (word: string): string => { + return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() +}