From ac0e2f088bd01d5a47d7dc6034e9fcca35f4f4d2 Mon Sep 17 00:00:00 2001 From: Disura Randunu Date: Sun, 29 Sep 2024 14:43:50 +0530 Subject: [PATCH] Mentor Country Handling --- src/controllers/admin/mentor.controller.ts | 6 ++- src/controllers/mentor.controller.ts | 5 ++- src/schemas/mentor-routes.schema.ts | 3 +- src/services/admin/mentor.service.ts | 44 +++++++++++++++------- src/services/mentor.service.ts | 19 +++++++++- 5 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/controllers/admin/mentor.controller.ts b/src/controllers/admin/mentor.controller.ts index b0a2b26..4d9e660 100644 --- a/src/controllers/admin/mentor.controller.ts +++ b/src/controllers/admin/mentor.controller.ts @@ -51,6 +51,8 @@ export const updateMentorHandler = async ( const mentorUpdateData: Partial = { ...data } const profileUpdateData: Partial = { ...data.profile } + const categoryId: string = data.categoryId + const countryId: string = data.countryId if (req.file) { profileUpdateData.image_url = `${IMG_HOST}/${req.file.filename}` @@ -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) { diff --git a/src/controllers/mentor.controller.ts b/src/controllers/mentor.controller.ts index 7c93a20..6263015 100644 --- a/src/controllers/mentor.controller.ts +++ b/src/controllers/mentor.controller.ts @@ -18,12 +18,13 @@ export const mentorApplicationHandler = async ( ): Promise> => { 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 }) diff --git a/src/schemas/mentor-routes.schema.ts b/src/schemas/mentor-routes.schema.ts index 751bb06..79247fc 100644 --- a/src/schemas/mentor-routes.schema.ts +++ b/src/schemas/mentor-routes.schema.ts @@ -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({ diff --git a/src/services/admin/mentor.service.ts b/src/services/admin/mentor.service.ts index bd2fc85..c078b2d 100644 --- a/src/services/admin/mentor.service.ts +++ b/src/services/admin/mentor.service.ts @@ -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' @@ -59,7 +60,9 @@ export const updateMentorStatus = async ( export const updateMentorDetails = async ( mentorId: string, mentorData: Partial, - profileData?: Partial + profileData?: Partial, + categoryId?: string, + countryId?: string ): Promise<{ statusCode: number mentor?: Mentor | null @@ -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 }, @@ -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 @@ -138,7 +154,7 @@ export const updateMentorDetails = async ( const updatedMentor = await mentorRepository.findOne({ where: { uuid: mentorId }, - relations: ['profile', 'category'] + relations: ['profile', 'category', 'country'] }) return { diff --git a/src/services/mentor.service.ts b/src/services/mentor.service.ts index dd7f4fa..120d6be 100644 --- a/src/services/mentor.service.ts +++ b/src/services/mentor.service.ts @@ -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' @@ -15,7 +16,8 @@ import { sendEmail } from './admin/email.service' export const createMentor = async ( user: Profile, application: Record, - categoryId: string + categoryId: string, + countryId: string ): Promise<{ statusCode: number mentor?: Mentor | null @@ -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: { @@ -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: @@ -86,7 +100,8 @@ export const createMentor = async ( category, application, true, - user + user, + country ) const savedMentor = await mentorRepository.save(newMentor)