diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index 6165486..001df3a 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 f4706d1..1885666 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/auth.service.ts b/src/services/auth.service.ts index b478e98..3c0eb50 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 c9912ff..d16c854 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 9178fc5..dd7f4fa 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/services/profile.service.ts b/src/services/profile.service.ts index b3c5156..95c8acd 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({ diff --git a/src/utils.ts b/src/utils.ts index d2d338b..7d1bff2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -313,3 +313,7 @@ export const getPasswordChangedEmailContent = ( ` } } + +export const capitalizeFirstLetter = (word: string): string => { + return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() +}