Skip to content

Commit

Permalink
Mentor country details (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Disura-Randunu authored Oct 15, 2024
1 parent d88d261 commit 91b32c5
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 22 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
6 changes: 5 additions & 1 deletion src/entities/country.entity.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/entities/mentor.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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[]

Expand All @@ -36,14 +41,16 @@ class Mentor extends BaseEntity {
category: Category,
application: Record<string, unknown>,
availability: boolean,
profile: Profile
profile: Profile,
country: Country
) {
super()
this.state = state
this.category = category
this.application = application
this.availability = availability
this.profile = profile
this.country = country
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/migrations/1726930041488-UpdateMentorTableWithCountry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class UpdateMentorTableWithCountry1726930041488
implements MigrationInterface
{
name = 'UpdateMentorTableWithCountry1726930041488'

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query(
`ALTER TABLE "mentor" DROP CONSTRAINT "FK_3302c22eb1636f239d605eb61c3"`
)
await queryRunner.query(`ALTER TABLE "mentor" DROP COLUMN "countryUuid"`)
}
}
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 91b32c5

Please sign in to comment.