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/entities/country.entity.ts b/src/entities/country.entity.ts index 48628c8..810f7d9 100644 --- a/src/entities/country.entity.ts +++ b/src/entities/country.entity.ts @@ -1,5 +1,6 @@ -import { Column, Entity } from 'typeorm' +import { Column, Entity, OneToMany } from 'typeorm' import BaseEntity from './baseEntity' +import Mentor from './mentor.entity' @Entity() export class Country extends BaseEntity { @@ -9,6 +10,9 @@ export class Country extends BaseEntity { @Column() name: string + @OneToMany(() => Mentor, (mentor) => mentor.country) + mentors?: Mentor[] + constructor(code: string, name: string) { super() this.code = code diff --git a/src/entities/mentor.entity.ts b/src/entities/mentor.entity.ts index cd6ddfa..dd1310c 100644 --- a/src/entities/mentor.entity.ts +++ b/src/entities/mentor.entity.ts @@ -4,6 +4,7 @@ import Mentee from './mentee.entity' import Category from './category.entity' import { MentorApplicationStatus } from '../enums' import BaseEntity from './baseEntity' +import { Country } from './country.entity' @Entity('mentor') class Mentor extends BaseEntity { @@ -28,6 +29,10 @@ class Mentor extends BaseEntity { @JoinColumn() profile: Profile + @ManyToOne(() => Country, (country) => country.mentors) + @JoinColumn() + country: Country + @OneToMany(() => Mentee, (mentee) => mentee.mentor) mentees?: Mentee[] @@ -36,7 +41,8 @@ class Mentor extends BaseEntity { category: Category, application: Record, availability: boolean, - profile: Profile + profile: Profile, + country: Country ) { super() this.state = state @@ -44,6 +50,7 @@ class Mentor extends BaseEntity { this.application = application this.availability = availability this.profile = profile + this.country = country } } diff --git a/src/migrations/1726930041488-UpdateMentorTableWithCountry.ts b/src/migrations/1726930041488-UpdateMentorTableWithCountry.ts new file mode 100644 index 0000000..5000549 --- /dev/null +++ b/src/migrations/1726930041488-UpdateMentorTableWithCountry.ts @@ -0,0 +1,21 @@ +import { MigrationInterface, QueryRunner } from 'typeorm' + +export class UpdateMentorTableWithCountry1726930041488 + implements MigrationInterface +{ + name = 'UpdateMentorTableWithCountry1726930041488' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "mentor" ADD "countryUuid" uuid`) + await queryRunner.query( + `ALTER TABLE "mentor" ADD CONSTRAINT "FK_3302c22eb1636f239d605eb61c3" FOREIGN KEY ("countryUuid") REFERENCES "country"("uuid") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "mentor" DROP CONSTRAINT "FK_3302c22eb1636f239d605eb61c3"` + ) + await queryRunner.query(`ALTER TABLE "mentor" DROP COLUMN "countryUuid"`) + } +} 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)