Skip to content

Commit

Permalink
Mentor Country Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Disura-Randunu committed Sep 29, 2024
1 parent 32a63c9 commit ac0e2f0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export const updateMentorHandler = async (

const mentorUpdateData: Partial<Mentor> = { ...data }
const profileUpdateData: Partial<Profile> = { ...data.profile }
const categoryId: string = data.categoryId
const countryId: string = data.countryId

if (req.file) {
profileUpdateData.image_url = `${IMG_HOST}/${req.file.filename}`
Expand All @@ -61,7 +63,9 @@ export const updateMentorHandler = async (
const { mentor, statusCode, message } = await updateMentorDetails(
mentorId,
mentorUpdateData,
profileUpdateData
profileUpdateData,
categoryId,
countryId
)
return res.status(statusCode).json({ mentor, message })
} catch (err) {
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export const mentorApplicationHandler = async (
): Promise<ApiResponse<Mentor>> => {
try {
const user = req.user as Profile
const { application, categoryId } = req.body
const { application, categoryId, countryId } = req.body

const { mentor, statusCode, message } = await createMentor(
user,
application,
categoryId
categoryId,
countryId
)

return res.status(statusCode).json({ mentor, message })
Expand Down
3 changes: 2 additions & 1 deletion src/schemas/mentor-routes.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { MentorApplicationStatus } from '../enums'

export const mentorApplicationSchema = z.object({
application: z.record(z.unknown()),
categoryId: z.string()
categoryId: z.string(),
countryId: z.string().optional()
})

export const getMenteesByMentorSchema = z.object({
Expand Down
44 changes: 30 additions & 14 deletions src/services/admin/mentor.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dataSource } from '../../configs/dbConfig'
import Category from '../../entities/category.entity'
import { Country } from '../../entities/country.entity'
import Mentor from '../../entities/mentor.entity'
import Profile from '../../entities/profile.entity'
import type { MentorApplicationStatus } from '../../enums'
Expand Down Expand Up @@ -59,7 +60,9 @@ export const updateMentorStatus = async (
export const updateMentorDetails = async (
mentorId: string,
mentorData: Partial<Mentor>,
profileData?: Partial<Profile>
profileData?: Partial<Profile>,
categoryId?: string,
countryId?: string
): Promise<{
statusCode: number
mentor?: Mentor | null
Expand All @@ -69,6 +72,7 @@ export const updateMentorDetails = async (
const mentorRepository = dataSource.getRepository(Mentor)
const profileRepository = dataSource.getRepository(Profile)
const categoryRepository = dataSource.getRepository(Category)
const countryRepository = dataSource.getRepository(Country)

const mentor = await mentorRepository.findOne({
where: { uuid: mentorId },
Expand All @@ -86,20 +90,32 @@ export const updateMentorDetails = async (
mentor.availability = mentorData.availability
}

if (mentorData.category) {
if (typeof mentorData.category === 'string') {
const category = await categoryRepository.findOne({
where: { uuid: mentorData.category }
})

if (!category) {
return {
statusCode: 404,
message: 'Category not found'
}
if (categoryId) {
const category = await categoryRepository.findOne({
where: { uuid: categoryId }
})

if (!category) {
return {
statusCode: 404,
message: 'Category not found'
}
mentor.category = category
}
mentor.category = category
}

if (countryId) {
const country = await countryRepository.findOne({
where: { uuid: countryId }
})

if (!country) {
return {
statusCode: 404,
message: 'Country not found'
}
}
mentor.country = country
}

// will override values of keys if exisitng keys provided. add new key-value pairs if not exists
Expand Down Expand Up @@ -138,7 +154,7 @@ export const updateMentorDetails = async (

const updatedMentor = await mentorRepository.findOne({
where: { uuid: mentorId },
relations: ['profile', 'category']
relations: ['profile', 'category', 'country']
})

return {
Expand Down
19 changes: 17 additions & 2 deletions src/services/mentor.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dataSource } from '../configs/dbConfig'
import Category from '../entities/category.entity'
import { Country } from '../entities/country.entity'
import Mentee from '../entities/mentee.entity'
import Mentor from '../entities/mentor.entity'
import type Profile from '../entities/profile.entity'
Expand All @@ -15,7 +16,8 @@ import { sendEmail } from './admin/email.service'
export const createMentor = async (
user: Profile,
application: Record<string, unknown>,
categoryId: string
categoryId: string,
countryId: string
): Promise<{
statusCode: number
mentor?: Mentor | null
Expand All @@ -25,6 +27,7 @@ export const createMentor = async (
const mentorRepository = dataSource.getRepository(Mentor)
const categoryRepository = dataSource.getRepository(Category)
const menteeRepository = dataSource.getRepository(Mentee)
const countryRepository = dataSource.getRepository(Country)

const mentee = await menteeRepository.findOne({
where: {
Expand Down Expand Up @@ -57,6 +60,17 @@ export const createMentor = async (
}
}

const country = await countryRepository.findOne({
where: { uuid: countryId }
})

if (!country) {
return {
statusCode: 404,
message: 'Country not found'
}
}

for (const mentor of existingMentorApplications) {
switch (mentor.state) {
case MentorApplicationStatus.PENDING:
Expand Down Expand Up @@ -86,7 +100,8 @@ export const createMentor = async (
category,
application,
true,
user
user,
country
)

const savedMentor = await mentorRepository.save(newMentor)
Expand Down

0 comments on commit ac0e2f0

Please sign in to comment.