Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Get all mentee emails by status endpoint #81

Merged
merged 21 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/controllers/admin/email.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Request, Response } from 'express'
import type { ApiResponse } from '../../types'
import { getAllMenteeEmailsService } from '../../services/admin/email.service'
import { ApplicationStatus } from '../../enums'

export const getAllMenteeEmails = async (
req: Request,
res: Response
): Promise<ApiResponse<string[]>> => {
try {
const status = req.query.status
if (
status === ApplicationStatus.APPROVED ||
status === ApplicationStatus.REJECTED ||
status === ApplicationStatus.PENDING
) {
const { emails, statusCode, message } = await getAllMenteeEmailsService(
status
)
return res.status(statusCode).json({ emails, message })
} else {
return res.status(400).json({ message: 'Invalid Status' })
}
} catch (err) {
console.error(err)
return res.status(500).json({ error: err || 'Internal Server Error' })
}
}
2 changes: 2 additions & 0 deletions src/routes/admin/admin.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import mentorRouter from './mentor/mentor.route'
import categoryRouter from './category/category.route'
import platformRouter from './platform/platform.route'
import emailTemplateRouter from './email/emailTemplate.route'
import menteeRouter from './mentee/mentee.route'

const adminRouter = express()

adminRouter.use('/users', userRouter)
adminRouter.use('/mentors', mentorRouter)
adminRouter.use('/mentee', menteeRouter)
adminRouter.use('/categories', categoryRouter)
adminRouter.use('/platform', platformRouter)
adminRouter.use('/emailTemplate', emailTemplateRouter)
Expand Down
84 changes: 84 additions & 0 deletions src/routes/admin/mentee/mentee.route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { startServer } from '../../../app'
import { type Express } from 'express'
import supertest from 'supertest'
import bcrypt from 'bcrypt'
import { mockAdmin, mockUser } from '../../../../mocks'
import { dataSource } from '../../../configs/dbConfig'
import Profile from '../../../entities/profile.entity'
import { ProfileTypes } from '../../../enums'

const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

let server: Express
let agent: supertest.SuperAgentTest
let adminAgent: supertest.SuperAgentTest

describe('Admin mentee routes', () => {
beforeAll(async () => {
server = await startServer(port)
agent = supertest.agent(server)
adminAgent = supertest.agent(server)

await supertest(server)
.post('/api/auth/register')
.send(mockUser)
.expect(201)
await agent.post('/api/auth/login').send(mockUser).expect(200)

const profileRepository = dataSource.getRepository(Profile)

const hashedPassword = await bcrypt.hash(mockAdmin.password, 10)
const newProfile = profileRepository.create({
primary_email: mockAdmin.email,
password: hashedPassword,
contact_email: '',
first_name: '',
last_name: '',
image_url: '',
linkedin_url: '',
type: ProfileTypes.ADMIN
})

await profileRepository.save(newProfile)

await adminAgent.post('/api/auth/login').send(mockAdmin).expect(200)
}, 5000)

it('should get all mentee emails with status approved', async () => {
const response = await adminAgent
.get('/api/admin/mentee/emails?status=approved')
.expect(200)

const { emails, message } = response.body
expect(emails).toBeInstanceOf(Array)
expect(message).toContain('All mentee emails with status approved')
})

it('should get all mentee emails with status rejected', async () => {
const response = await adminAgent
.get('/api/admin/mentee/emails?status=rejected')
.expect(200)

const { emails, message } = response.body
expect(emails).toBeInstanceOf(Array)
expect(message).toContain('All mentee emails with status rejected')
})

it('should get all mentee emails with status pending', async () => {
const response = await adminAgent
.get('/api/admin/mentee/emails?status=pending')
.expect(200)

const { emails, message } = response.body
expect(emails).toBeInstanceOf(Array)
expect(message).toContain('All mentee emails with status pending')
})

it('should throw status code 400', async () => {
const response = await adminAgent
.get('/api/admin/mentee/emails?status=wrongstatus')
.expect(400)
const { message } = response.body
expect(message).toContain('Invalid Status')
})
})
9 changes: 9 additions & 0 deletions src/routes/admin/mentee/mentee.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from 'express'
import { requireAuth } from '../../../controllers/auth.controller'
import { getAllMenteeEmails } from '../../../controllers/admin/email.controller'

const menteeRouter = express.Router()

menteeRouter.get('/emails/', requireAuth, getAllMenteeEmails)

export default menteeRouter
33 changes: 33 additions & 0 deletions src/services/admin/email.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { dataSource } from '../../configs/dbConfig'
import Mentee from '../../entities/mentee.entity'
import type { ApplicationStatus } from '../../enums'

export const getAllMenteeEmailsService = async (
status: ApplicationStatus | undefined
): Promise<{
statusCode: number
emails?: string[]
message: string
}> => {
try {
const menteeRepositroy = dataSource.getRepository(Mentee)
const allMentees: Mentee[] = await menteeRepositroy.find({
where: status ? { state: status } : {},
relations: ['profile']
})
const emails = allMentees.map((mentee) => mentee?.profile?.primary_email)
if (!emails) {
return {
statusCode: 404,
message: 'Mentees Emails not found'
}
}
return {
statusCode: 200,
emails,
message: 'All mentee emails with status ' + (status ?? 'undefined')
}
} catch (err) {
throw new Error('Error getting mentee emails')
}
}
Loading