Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Partial Updates to User Profile in updateProfile Function #166

Merged
merged 11 commits into from
Sep 10, 2024
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
20 changes: 13 additions & 7 deletions src/services/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@ 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 { primary_email, first_name, last_name, image_url } = updateData

const updatedFields: Partial<Profile> = {
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({
Expand Down
Loading