|
| 1 | +const joi = require('joi') |
| 2 | + |
| 3 | +const createTask = async (req, res, next) => { |
| 4 | + const schema = joi.object().keys({ |
| 5 | + type: joi.string().required(), |
| 6 | + links: joi.array().items(joi.string()), |
| 7 | + endsOn: joi.string(), |
| 8 | + startedOn: joi.string().required(), |
| 9 | + status: joi.string().required(), |
| 10 | + ownerId: joi.string().required(), |
| 11 | + percentCompleted: joi.number().optional(), |
| 12 | + dependsOn: joi.array().items(joi.string()), |
| 13 | + participants: joi.array().items(joi.string()), |
| 14 | + completionAward: joi.object().keys({ |
| 15 | + gold: joi.number(), |
| 16 | + silver: joi.number(), |
| 17 | + bronze: joi.number() |
| 18 | + }).optional(), |
| 19 | + lossRate: joi.object().keys({ |
| 20 | + gold: joi.number(), |
| 21 | + silver: joi.number(), |
| 22 | + bronze: joi.number() |
| 23 | + }).optional() |
| 24 | + }) |
| 25 | + |
| 26 | + try { |
| 27 | + await schema.validateAsync(req.body) |
| 28 | + next() |
| 29 | + } catch (error) { |
| 30 | + logger.error(`Error validating createTask payload : ${error}`) |
| 31 | + res.boom.badRequest(error.details[0].message) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const updateTask = async (req, res, next) => { |
| 36 | + const schema = joi.object().keys({ |
| 37 | + type: joi.string().optional(), |
| 38 | + links: joi.array().items(joi.string()), |
| 39 | + endsOn: joi.string().optional(), |
| 40 | + startedOn: joi.string().optional(), |
| 41 | + status: joi.string().optional(), |
| 42 | + ownerId: joi.string().optional(), |
| 43 | + percentCompleted: joi.number().optional(), |
| 44 | + dependsOn: joi.array().items(joi.string()), |
| 45 | + participants: joi.array().items(joi.string()), |
| 46 | + completionAward: joi.object().keys({ |
| 47 | + gold: joi.number(), |
| 48 | + silver: joi.number(), |
| 49 | + bronze: joi.number() |
| 50 | + }).optional(), |
| 51 | + lossRate: joi.object().keys({ |
| 52 | + gold: joi.number(), |
| 53 | + silver: joi.number(), |
| 54 | + bronze: joi.number() |
| 55 | + }).optional() |
| 56 | + }) |
| 57 | + |
| 58 | + try { |
| 59 | + await schema.validateAsync(req.body) |
| 60 | + next() |
| 61 | + } catch (error) { |
| 62 | + logger.error(`Error validating updateTask payload : ${error}`) |
| 63 | + res.boom.badRequest(error.details[0].message) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +module.exports = { |
| 68 | + createTask, |
| 69 | + updateTask |
| 70 | +} |
0 commit comments