diff options
Diffstat (limited to 'server/middlewares/validators/runners/runners.ts')
-rw-r--r-- | server/middlewares/validators/runners/runners.ts | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/server/middlewares/validators/runners/runners.ts b/server/middlewares/validators/runners/runners.ts new file mode 100644 index 000000000..71a1275d2 --- /dev/null +++ b/server/middlewares/validators/runners/runners.ts | |||
@@ -0,0 +1,95 @@ | |||
1 | import express from 'express' | ||
2 | import { body, param } from 'express-validator' | ||
3 | import { isIdValid } from '@server/helpers/custom-validators/misc' | ||
4 | import { | ||
5 | isRunnerDescriptionValid, | ||
6 | isRunnerNameValid, | ||
7 | isRunnerRegistrationTokenValid, | ||
8 | isRunnerTokenValid | ||
9 | } from '@server/helpers/custom-validators/runners/runners' | ||
10 | import { RunnerModel } from '@server/models/runner/runner' | ||
11 | import { RunnerRegistrationTokenModel } from '@server/models/runner/runner-registration-token' | ||
12 | import { forceNumber } from '@shared/core-utils' | ||
13 | import { HttpStatusCode, RegisterRunnerBody, ServerErrorCode } from '@shared/models' | ||
14 | import { areValidationErrors } from '../shared/utils' | ||
15 | |||
16 | const tags = [ 'runner' ] | ||
17 | |||
18 | const registerRunnerValidator = [ | ||
19 | body('registrationToken').custom(isRunnerRegistrationTokenValid), | ||
20 | body('name').custom(isRunnerNameValid), | ||
21 | body('description').optional().custom(isRunnerDescriptionValid), | ||
22 | |||
23 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
24 | if (areValidationErrors(req, res, { tags })) return | ||
25 | |||
26 | const body: RegisterRunnerBody = req.body | ||
27 | |||
28 | const runnerRegistrationToken = await RunnerRegistrationTokenModel.loadByRegistrationToken(body.registrationToken) | ||
29 | |||
30 | if (!runnerRegistrationToken) { | ||
31 | return res.fail({ | ||
32 | status: HttpStatusCode.NOT_FOUND_404, | ||
33 | message: 'Registration token is invalid', | ||
34 | tags | ||
35 | }) | ||
36 | } | ||
37 | |||
38 | res.locals.runnerRegistrationToken = runnerRegistrationToken | ||
39 | |||
40 | return next() | ||
41 | } | ||
42 | ] | ||
43 | |||
44 | const deleteRunnerValidator = [ | ||
45 | param('runnerId').custom(isIdValid), | ||
46 | |||
47 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
48 | if (areValidationErrors(req, res, { tags })) return | ||
49 | |||
50 | const runner = await RunnerModel.load(forceNumber(req.params.runnerId)) | ||
51 | |||
52 | if (!runner) { | ||
53 | return res.fail({ | ||
54 | status: HttpStatusCode.NOT_FOUND_404, | ||
55 | message: 'Runner not found', | ||
56 | tags | ||
57 | }) | ||
58 | } | ||
59 | |||
60 | res.locals.runner = runner | ||
61 | |||
62 | return next() | ||
63 | } | ||
64 | ] | ||
65 | |||
66 | const getRunnerFromTokenValidator = [ | ||
67 | body('runnerToken').custom(isRunnerTokenValid), | ||
68 | |||
69 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
70 | if (areValidationErrors(req, res, { tags })) return | ||
71 | |||
72 | const runner = await RunnerModel.loadByToken(req.body.runnerToken) | ||
73 | |||
74 | if (!runner) { | ||
75 | return res.fail({ | ||
76 | status: HttpStatusCode.NOT_FOUND_404, | ||
77 | message: 'Unknown runner token', | ||
78 | type: ServerErrorCode.UNKNOWN_RUNNER_TOKEN, | ||
79 | tags | ||
80 | }) | ||
81 | } | ||
82 | |||
83 | res.locals.runner = runner | ||
84 | |||
85 | return next() | ||
86 | } | ||
87 | ] | ||
88 | |||
89 | // --------------------------------------------------------------------------- | ||
90 | |||
91 | export { | ||
92 | registerRunnerValidator, | ||
93 | deleteRunnerValidator, | ||
94 | getRunnerFromTokenValidator | ||
95 | } | ||