From e1ef71e0efd18373dfaafcfba19c2caf4f415efe Mon Sep 17 00:00:00 2001 From: mayura-andrew Date: Mon, 9 Sep 2024 15:50:20 +0530 Subject: [PATCH 1/2] Refactor profile update logic to use a single updateData object Instead of directly passing individual fields to the updateProfile function, refactor the logic to use a single updateData object. This object contains the updated fields for the profile, such as primary_email, first_name, last_name, and image_url. This change improves code readability and maintainability. Related to #165 --- src/controllers/profile.controller.ts | 19 +++++++------------ src/schemas/profile-routes.schema.ts | 8 ++++---- src/services/profile.service.ts | 19 ++++++++++++------- 3 files changed, 23 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..42c74e0e 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,18 @@ export const updateProfile = async ( }> => { try { const profileRepository = dataSource.getRepository(Profile) + + const updatedFields: Partial = {} + + if (updateData.primary_email) + updatedFields.primary_email = updateData.primary_email + if (updateData.first_name) updatedFields.first_name = updateData.first_name + if (updateData.last_name) updatedFields.last_name = updateData.last_name + if (updateData.image_url) updatedFields.image_url = updateData.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 20f0de51a68d12c1e8f087f8d9abe9bce26f061a Mon Sep 17 00:00:00 2001 From: mayura-andrew Date: Tue, 10 Sep 2024 00:24:03 +0530 Subject: [PATCH 2/2] Refactor profile update logic to use a single updateData object --- src/services/profile.service.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/services/profile.service.ts b/src/services/profile.service.ts index 42c74e0e..95c8acdc 100644 --- a/src/services/profile.service.ts +++ b/src/services/profile.service.ts @@ -16,13 +16,14 @@ export const updateProfile = async ( try { const profileRepository = dataSource.getRepository(Profile) - const updatedFields: Partial = {} + const { primary_email, first_name, last_name, image_url } = updateData - if (updateData.primary_email) - updatedFields.primary_email = updateData.primary_email - if (updateData.first_name) updatedFields.first_name = updateData.first_name - if (updateData.last_name) updatedFields.last_name = updateData.last_name - if (updateData.image_url) updatedFields.image_url = updateData.image_url + const updatedFields: Partial = { + primary_email, + first_name, + last_name, + image_url + } await profileRepository.update( { uuid: user.uuid },