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 10, 2023
2 parents 709b2dd + 52a2637 commit 419d5da
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 11 deletions.
25 changes: 24 additions & 1 deletion src/controllers/admin/platform.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type Profile from '../../entities/profile.entity'
import { ProfileTypes } from '../../enums'
import {
createPlatform,
getPlatformDetails
getPlatformDetails,
updatePlatformDetails
} from '../../services/admin/platform.service'

export const addPlatform = async (
Expand Down Expand Up @@ -45,3 +46,25 @@ export const getPlatform = async (
return res.status(500).json({ error: err })
}
}

export const updatePlatform = async (
req: Request,
res: Response
): Promise<ApiResponse<Platform>> => {
try {
const user = req.user as Profile

if (user.type !== ProfileTypes.ADMIN) {
return res.status(403).json({ message: 'Only Admins are allowed' })
}

const { platform, statusCode, message } = await updatePlatformDetails(
req.body
)

return res.status(statusCode).json({ platform, message })
} catch (err) {
console.error('Error executing query', err)
return res.status(500).json({ error: err })
}
}
5 changes: 0 additions & 5 deletions src/entities/platform.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class Platform extends BaseEntity {
@Column()
landing_page_url: string

@Column({ type: 'json' })
email_templates: JSON

@Column({ type: 'varchar', length: 255 })
title: string

Expand All @@ -26,15 +23,13 @@ class Platform extends BaseEntity {
mentor_questions: JSON,
image_url: string,
landing_page_url: string,
email_templates: JSON,
title: string
) {
super()
this.description = description
this.mentor_questions = mentor_questions
this.image_url = image_url
this.landing_page_url = landing_page_url
this.email_templates = email_templates
this.title = title
}
}
Expand Down
40 changes: 38 additions & 2 deletions src/routes/admin/platform/platform.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000
let server: Express
let agent: supertest.SuperAgentTest
let adminAgent: supertest.SuperAgentTest
let savedPlatformUUID: string

describe('Admin platform routes', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -46,7 +47,12 @@ describe('Admin platform routes', () => {
}, 5000)

it('should add a platform', async () => {
await adminAgent.post('/api/admin/platform').send(platformInfo).expect(201)
const platformDetails = await adminAgent
.post('/api/admin/platform')
.send(platformInfo)
.expect(201)

savedPlatformUUID = await platformDetails.body.platform.uuid
})

it('should only allow admins to add a platform', async () => {
Expand All @@ -63,12 +69,42 @@ describe('Admin platform routes', () => {
expect(platform).toHaveProperty('mentor_questions')
expect(platform).toHaveProperty('image_url')
expect(platform).toHaveProperty('landing_page_url')
expect(platform).toHaveProperty('email_templates')
expect(platform).toHaveProperty('title')
expect(platform).toHaveProperty('uuid')
})
})

it('should only allow admins to get a platform', async () => {
await agent.get('/api/admin/platform').expect(403)
})

it('should should be able to update the platform details and return a 200', async () => {
await adminAgent
.put('/api/admin/platform')
.send({
...platformInfo,
uuid: savedPlatformUUID
})
.expect(200)
})

it('should return a 404 when invalid platform uuid is sent', async () => {
await adminAgent
.put('/api/admin/platform')
.send({
...platformInfo,
uuid: '0058ab92-1c82-4af1-9f84-c60a3e922244'
})
.expect(404)
})

it('should only allow admins to update platform', async () => {
await agent
.put('/api/admin/platform')
.send({
...platformInfo,
uuid: savedPlatformUUID
})
.expect(403)
})
})
4 changes: 3 additions & 1 deletion src/routes/admin/platform/platform.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import express from 'express'
import { requireAuth } from '../../../controllers/auth.controller'
import {
addPlatform,
getPlatform
getPlatform,
updatePlatform
} from '../../../controllers/admin/platform.controller'

const platformRouter = express.Router()

platformRouter.post('/', requireAuth, addPlatform)
platformRouter.get('/', requireAuth, getPlatform)
platformRouter.put('/', requireAuth, updatePlatform)

export default platformRouter
49 changes: 47 additions & 2 deletions src/services/admin/platform.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const createPlatform = async ({
mentor_questions,
image_url,
landing_page_url,
email_templates,
title
}: Platform): Promise<{
statusCode: number
Expand All @@ -21,7 +20,6 @@ export const createPlatform = async ({
mentor_questions,
image_url,
landing_page_url,
email_templates,
title
)

Expand Down Expand Up @@ -58,3 +56,50 @@ export const getPlatformDetails = async (): Promise<{
throw new Error('Error creating Platform')
}
}

export const updatePlatformDetails = async ({
uuid,
description,
mentor_questions,
image_url,
landing_page_url,
title
}: Platform): Promise<{
statusCode: number
platform?: Platform | null
message: string
}> => {
try {
const platformRepository = dataSource.getRepository(Platform)

const updateData = {
description,
mentor_questions,
image_url,
landing_page_url,
title
}

const result = await platformRepository.update({ uuid }, updateData)

if (result.affected === 0) {
return {
statusCode: 404,
message: 'Platform not found'
}
}

const updatedPlatform = await platformRepository.findOne({
where: { uuid }
})

return {
statusCode: 200,
platform: updatedPlatform,
message: 'Platform updated successfully'
}
} catch (err) {
console.error('Error updateing platform', err)
throw new Error('Error updating platform')
}
}

0 comments on commit 419d5da

Please sign in to comment.