Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/issue-153_token_revalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmabulage authored Sep 16, 2024
2 parents a98c398 + d1d6d7a commit 53cfbfd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 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
5 changes: 3 additions & 2 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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: ''
})

Expand Down
8 changes: 7 additions & 1 deletion src/services/mentee.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { MenteeApplicationStatus } from '../enums'
import {
getEmailContent,
getMentorNotifyEmailContent,
getMenteePublicData
getMenteePublicData,
capitalizeFirstLetter
} from '../utils'
import { sendEmail } from './admin/email.service'

Expand Down Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion src/services/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
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
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,7 @@ export const getPasswordChangedEmailContent = (
`
}
}

export const capitalizeFirstLetter = (word: string): string => {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
}

0 comments on commit 53cfbfd

Please sign in to comment.