-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only get roles for current initiative
- Loading branch information
1 parent
b34d2a4
commit 136deed
Showing
7 changed files
with
75 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// @ts-expect-error api-problem lacks a defined interface; code still works fine | ||
import Problem from 'api-problem'; | ||
|
||
import { Initiative } from '../utils/enums/application'; | ||
|
||
import type { NextFunction, Request, Response } from '../interfaces/IExpress'; | ||
|
||
/** | ||
* @function setInitiative | ||
* Injects the given initiative into the currentUser | ||
* @param {string} initiative An initiative code | ||
* @returns {function} Express middleware function | ||
* @throws The error encountered upon failure | ||
*/ | ||
export const setInitiative = (initiative: Initiative) => { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
if (req.currentUser) { | ||
req.currentUser.initiative = initiative; | ||
} else { | ||
throw new Problem(403, { | ||
detail: 'Unable to determine initiative', | ||
instance: req.originalUrl | ||
}); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (err: any) { | ||
return next(new Problem(403, { detail: err.message, instance: req.originalUrl })); | ||
} | ||
|
||
// Continue middleware | ||
next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import prisma from '../db/dataConnection'; | ||
|
||
import { Initiative } from '../utils/enums/application'; | ||
|
||
const service = { | ||
/** | ||
* @function getInitiative | ||
* Create an activity for the given initiative with a unique identifier | ||
* @param {string} initiative The initiative ID | ||
* @returns {Promise<Activity | null>} The result of running the findFirst operation | ||
*/ | ||
getInitiative: async (initiative: Initiative) => { | ||
const result = await prisma.initiative.findFirstOrThrow({ | ||
where: { | ||
code: initiative | ||
} | ||
}); | ||
return { initiativeId: result.initiative_id, code: result.code, label: result.label }; | ||
} | ||
}; | ||
|
||
export default service; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import jwt from 'jsonwebtoken'; | ||
|
||
import { ApiScope } from './ApiScope'; | ||
import { AuthType } from '../utils/enums/application'; | ||
import { AuthType, Initiative } from '../utils/enums/application'; | ||
|
||
export type CurrentUser = { | ||
authType: AuthType; | ||
apiScope: ApiScope; | ||
apiScope?: ApiScope; | ||
initiative?: Initiative; | ||
tokenPayload: string | jwt.JwtPayload | null; | ||
}; |