Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
anjula-sack authored Nov 4, 2023
2 parents 71d01c6 + 106db45 commit 709b2dd
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 14 deletions.
39 changes: 37 additions & 2 deletions src/controllers/profile.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { Request, Response } from 'express'
import { updateProfile, deleteProfile } from '../services/profile.service'
import { type Request, type Response } from 'express'
import {
updateProfile,
deleteProfile,
getAllMentorApplications
} from '../services/profile.service'
import type Profile from '../entities/profile.entity'
import type { ApiResponse } from '../types'
import type Mentor from '../entities/mentor.entity'

export const getProfileHandler = async (
req: Request,
Expand Down Expand Up @@ -75,3 +80,33 @@ export const deleteProfileHandler = async (
throw err
}
}

export const getApplicationsHandler = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentor[]>> => {
try {
const user = req.user as Profile
const applicationType = req.query.type
if (applicationType === 'mentor') {
const { mentorApplications, statusCode, message } =
await getAllMentorApplications(user)

return res.status(statusCode).json({
'mentor applications': mentorApplications,
message
})
} else {
return res.status(404).json({ message: 'Invalid application type' })
}
} catch (error) {
if (error instanceof Error) {
console.error('Error executing query', error)
return res
.status(500)
.json({ error: 'Internal server errorrrr', message: error.message })
}

throw error
}
}
11 changes: 2 additions & 9 deletions src/entities/mentor.entity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
OneToOne
} from 'typeorm'
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm'
import profileEntity from './profile.entity'
import Mentee from './mentee.entity'
import Category from './category.entity'
Expand All @@ -30,7 +23,7 @@ class Mentor extends BaseEntity {
@Column({ type: 'boolean' })
availability: boolean

@OneToOne(() => profileEntity)
@ManyToOne(() => profileEntity)
@JoinColumn()
profile: profileEntity

Expand Down
52 changes: 49 additions & 3 deletions src/routes/profile/profile.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,52 @@ describe('profile', () => {
})
})

describe('Get my mentor applications route', () => {
it('should return a 200 with mentor applications for the user', async () => {
const response = await agent
.get('/api/me/applications?type=mentor')
.expect(200)

if (
response.body.message === 'No mentor applications found for the user'
) {
expect(response.body).not.toHaveProperty('mentor applications')
} else {
expect(response.body).toHaveProperty('mentor applications')
expect(response.body['mentor applications']).toBeInstanceOf(Array)
expect(response.body['mentor applications']).toHaveProperty('state')
expect(response.body['mentor applications']).toHaveProperty('category')
expect(response.body['mentor applications']).toHaveProperty(
'availability'
)
expect(response.body['mentor applications']).toHaveProperty('uuid')
expect(response.body['mentor applications']).toHaveProperty(
'created_at'
)
expect(response.body['mentor applications']).toHaveProperty(
'updated_at'
)
}
})

it('should return a 404 for an invalid application type', async () => {
const response = await agent
.get('/api/me/applications?type=invalidType')
.expect(404)

expect(response.body).toHaveProperty(
'message',
'Invalid application type'
)
})

it('should return a 401 when a valid access token is not provided', async () => {
await supertest(server)
.get('/api/me/applications?type=mentor')
.expect(401)
})
})

describe('Delete profile route', () => {
it('should delete the user profile and return a 200', async () => {
await agent.delete('/api/me/profile').expect(200)
Expand All @@ -66,9 +112,9 @@ describe('profile', () => {
it('should return a 401 when a valid access token is not provided', async () => {
await supertest(server).delete(`/api/me/profile`).send({}).expect(401)
})
})

afterAll(async () => {
await dataSource.destroy()
})
afterAll(async () => {
await dataSource.destroy()
})
})
2 changes: 2 additions & 0 deletions src/routes/profile/profile.route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express'
import {
deleteProfileHandler,
getApplicationsHandler,
getProfileHandler,
updateProfileHandler
} from '../../controllers/profile.controller'
Expand All @@ -11,5 +12,6 @@ const profileRouter = express.Router()
profileRouter.get('/profile', requireAuth, getProfileHandler)
profileRouter.put('/profile', requireAuth, updateProfileHandler)
profileRouter.delete('/profile', requireAuth, deleteProfileHandler)
profileRouter.get('/applications', requireAuth, getApplicationsHandler)

export default profileRouter
38 changes: 38 additions & 0 deletions src/services/profile.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { dataSource } from '../configs/dbConfig'
import Mentor from '../entities/mentor.entity'
import Profile from '../entities/profile.entity'

export const updateProfile = async (
Expand Down Expand Up @@ -56,3 +57,40 @@ export const deleteProfile = async (userId: string): Promise<void> => {
.where('uuid = :uuid', { uuid: userId })
.execute()
}

export const getAllMentorApplications = async (
user: Profile
): Promise<{
statusCode: number
mentorApplications?: Mentor[] | null | undefined
message: string
}> => {
try {
const mentorRepository = dataSource.getRepository(Mentor)

const existingMentorApplications = await mentorRepository
.createQueryBuilder('mentor')
.innerJoinAndSelect('mentor.profile', 'profile')
.innerJoinAndSelect('mentor.category', 'category')
.addSelect('mentor.application')
.where('mentor.profile.uuid = :uuid', { uuid: user.uuid })
.getMany()

console.log(existingMentorApplications)
if (existingMentorApplications.length === 0) {
return {
statusCode: 200,
message: 'No mentor applications found for the user'
}
}

return {
statusCode: 200,
mentorApplications: existingMentorApplications,
message: 'Mentor applications found'
}
} catch (error) {
console.error('Error executing query', error)
return { statusCode: 500, message: 'Internal server error' }
}
}

0 comments on commit 709b2dd

Please sign in to comment.