Skip to content

Commit

Permalink
Refactor profile update logic to use a single updateData object
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mayura-andrew committed Sep 9, 2024
1 parent 1401096 commit e1ef71e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
19 changes: 7 additions & 12 deletions src/controllers/profile.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,16 @@ export const updateProfileHandler = async (
reject(err)
} else {
try {
const updateData: Partial<Profile> = { ...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)
Expand Down
8 changes: 4 additions & 4 deletions src/schemas/profile-routes.schema.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
19 changes: 12 additions & 7 deletions src/services/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@ 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<Profile>
updateData: Partial<Profile>
): Promise<{
statusCode: number
profile?: Profile | null
message: string
}> => {
try {
const profileRepository = dataSource.getRepository(Profile)

const updatedFields: Partial<Profile> = {}

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({
Expand Down

0 comments on commit e1ef71e

Please sign in to comment.