|
1 | 1 | import { Response, Router } from 'express' |
2 | 2 | import { AuthenticatedRequest } from 'types' |
3 | 3 | import { ApplicationError } from '../../util/ApplicationError' |
| 4 | +import { Organisation } from '../../models' |
4 | 5 |
|
5 | 6 | import { createFeedbackTargetLog } from '../../services/auditLog' |
6 | 7 | import { mailer } from '../../mailer' |
@@ -32,6 +33,52 @@ import { PUBLIC_COURSE_BROWSER_ENABLED } from '../../util/config' |
32 | 33 | const adRouter = Router() |
33 | 34 | const noadRouter = Router() |
34 | 35 |
|
| 36 | +// TODO figure out if the two bellow functions could be united |
| 37 | +adRouter.get('/for-faculty/:code', async (req: AuthenticatedRequest, res: Response) => { |
| 38 | + const { user } = req |
| 39 | + const { code } = req.params |
| 40 | + const { startDate, endDate } = req.query |
| 41 | + if (!code) throw ApplicationError.BadRequest('Missing code') |
| 42 | + |
| 43 | + const organisationAccess = await user.organisationAccess |
| 44 | + if (!organisationAccess[code]?.read) throw ApplicationError.Forbidden() |
| 45 | + |
| 46 | + const facultyOrganisation = await Organisation.findOne({ |
| 47 | + where: { code }, |
| 48 | + include: [ |
| 49 | + { |
| 50 | + model: Organisation, |
| 51 | + as: 'childOrganisations', |
| 52 | + attributes: ['id', 'code'], |
| 53 | + }, |
| 54 | + ], |
| 55 | + }) |
| 56 | + |
| 57 | + if (!facultyOrganisation) throw ApplicationError.NotFound('Organisation not found') |
| 58 | + |
| 59 | + const childOrgCodes = |
| 60 | + facultyOrganisation.childOrganisations |
| 61 | + ?.filter(child => organisationAccess[child.code]?.read) |
| 62 | + .map(child => child.code) || [] |
| 63 | + |
| 64 | + const allOrganisationCodes = [code, ...childOrgCodes] |
| 65 | + |
| 66 | + const feedbackTargetsPromises = allOrganisationCodes.map(orgCode => |
| 67 | + getFeedbackTargetsForOrganisation({ |
| 68 | + organisationCode: orgCode, |
| 69 | + startDate: startDate as string, |
| 70 | + endDate: endDate as string, |
| 71 | + user, |
| 72 | + }) |
| 73 | + ) |
| 74 | + |
| 75 | + const feedbackTargetsArrays = await Promise.all(feedbackTargetsPromises) |
| 76 | + |
| 77 | + const feedbackTargets = feedbackTargetsArrays.flat() |
| 78 | + |
| 79 | + res.send(feedbackTargets) |
| 80 | +}) |
| 81 | + |
35 | 82 | adRouter.get('/for-organisation/:code', async (req: AuthenticatedRequest, res: Response) => { |
36 | 83 | const { user } = req |
37 | 84 | const { code } = req.params |
|
0 commit comments