|
1 | 1 | const joi = require("joi");
|
2 |
| -const { TASK_REQUEST_TYPE } = require("../../constants/taskRequests"); |
| 2 | +const { RQLQueryParser } = require("../../utils/RQLParser"); |
3 | 3 | const githubOrg = config.get("githubApi.org");
|
4 | 4 | const githubBaseUrl = config.get("githubApi.baseUrl");
|
5 | 5 | const githubIssuerUrlPattern = new RegExp(`^${githubBaseUrl}/repos/${githubOrg}/.+/issues/\\d+$`);
|
| 6 | +const { TASK_REQUEST_STATUS, TASK_REQUEST_TYPE } = require("../../constants/taskRequests"); |
6 | 7 |
|
7 | 8 | const postTaskRequests = async (req, res, next) => {
|
8 | 9 | const taskAssignmentSchema = joi
|
@@ -40,6 +41,61 @@ const postTaskRequests = async (req, res, next) => {
|
40 | 41 | }
|
41 | 42 | };
|
42 | 43 |
|
| 44 | +const getTaskRequests = async (req, res, next) => { |
| 45 | + const queryParamsSchema = joi |
| 46 | + .object() |
| 47 | + .keys({ |
| 48 | + dev: joi.bool().optional().sensitive(), |
| 49 | + prev: joi.string().optional(), |
| 50 | + next: joi.string().optional(), |
| 51 | + size: joi.number().integer().positive().min(1).max(100).optional(), |
| 52 | + q: joi.string().optional(), |
| 53 | + }) |
| 54 | + .without("prev", "next") |
| 55 | + .with("prev", "size") |
| 56 | + .with("next", "size"); |
| 57 | + |
| 58 | + const filtersSchema = joi.object().keys({ |
| 59 | + status: joi |
| 60 | + .array() |
| 61 | + .items( |
| 62 | + joi.object().keys({ |
| 63 | + value: joi.string().valid(...Object.values(TASK_REQUEST_STATUS).map((value) => value.toLowerCase())), |
| 64 | + operator: joi.string().optional(), |
| 65 | + }) |
| 66 | + ) |
| 67 | + .optional(), |
| 68 | + "request-type": joi |
| 69 | + .array() |
| 70 | + .items( |
| 71 | + joi.object().keys({ |
| 72 | + value: joi.string().valid(...Object.values(TASK_REQUEST_TYPE).map((value) => value.toLowerCase())), |
| 73 | + operator: joi.string().optional(), |
| 74 | + }) |
| 75 | + ) |
| 76 | + .optional(), |
| 77 | + }); |
| 78 | + |
| 79 | + const sortSchema = joi.object().keys({ |
| 80 | + created: joi.string().valid("asc", "desc").optional(), |
| 81 | + requestors: joi.string().valid("asc", "desc").optional(), |
| 82 | + }); |
| 83 | + try { |
| 84 | + const { q: queryString } = req.query; |
| 85 | + const rqlQueryParser = new RQLQueryParser(queryString); |
| 86 | + |
| 87 | + await Promise.all([ |
| 88 | + filtersSchema.validateAsync(rqlQueryParser.getFilterQueries()), |
| 89 | + sortSchema.validateAsync(rqlQueryParser.getSortQueries()), |
| 90 | + queryParamsSchema.validateAsync(req.query), |
| 91 | + ]); |
| 92 | + next(); |
| 93 | + } catch (error) { |
| 94 | + logger.error(`Error validating get task requests payload : ${error}`); |
| 95 | + res.boom.badRequest(error?.details?.[0]?.message || error?.message); |
| 96 | + } |
| 97 | +}; |
43 | 98 | module.exports = {
|
| 99 | + getTaskRequests, |
44 | 100 | postTaskRequests,
|
45 | 101 | };
|
0 commit comments