|
5 | 5 | const _ = require('lodash')
|
6 | 6 | const Joi = require('joi')
|
7 | 7 | const config = require('config')
|
| 8 | +const HttpStatus = require('http-status-codes') |
8 | 9 | const { Op } = require('sequelize')
|
9 | 10 | const { v4: uuid } = require('uuid')
|
10 | 11 | const helper = require('../common/helper')
|
@@ -46,6 +47,34 @@ async function _getJobCandidates (jobId) {
|
46 | 47 | return candidates
|
47 | 48 | }
|
48 | 49 |
|
| 50 | +/** |
| 51 | + * Validate if all skills exist. |
| 52 | + * |
| 53 | + * @param {Array} skills the list of skills |
| 54 | + * @returns {undefined} |
| 55 | + */ |
| 56 | +async function _validateSkills (skills) { |
| 57 | + const m2mToken = await helper.getM2Mtoken() |
| 58 | + const responses = await Promise.all( |
| 59 | + skills.map( |
| 60 | + skill => helper.getSkillById(`Bearer ${m2mToken}`, skill) |
| 61 | + .then(() => { |
| 62 | + return { found: true } |
| 63 | + }) |
| 64 | + .catch(err => { |
| 65 | + if (err.status !== HttpStatus.NOT_FOUND) { |
| 66 | + throw err |
| 67 | + } |
| 68 | + return { found: false, skill } |
| 69 | + }) |
| 70 | + ) |
| 71 | + ) |
| 72 | + const errResponses = responses.filter(res => !res.found) |
| 73 | + if (errResponses.length) { |
| 74 | + throw new errors.BadRequestError(`Invalid skills: [${errResponses.map(res => res.skill)}]`) |
| 75 | + } |
| 76 | +} |
| 77 | + |
49 | 78 | /**
|
50 | 79 | * Get job by id
|
51 | 80 | * @param {String} id the job id
|
@@ -91,6 +120,7 @@ getJob.schema = Joi.object().keys({
|
91 | 120 | * @returns {Object} the created job
|
92 | 121 | */
|
93 | 122 | async function createJob (currentUser, job) {
|
| 123 | + await _validateSkills(job.skills) |
94 | 124 | if (!currentUser.isBookingManager) {
|
95 | 125 | const connect = await helper.isConnectMember(job.projectId, currentUser.jwtToken)
|
96 | 126 | if (!connect) {
|
@@ -130,6 +160,9 @@ createJob.schema = Joi.object().keys({
|
130 | 160 | * @returns {Object} the updated job
|
131 | 161 | */
|
132 | 162 | async function updateJob (currentUser, id, data) {
|
| 163 | + if (data.skills) { |
| 164 | + await _validateSkills(data.skills) |
| 165 | + } |
133 | 166 | let job = await Job.findById(id)
|
134 | 167 | if (!currentUser.isBookingManager) {
|
135 | 168 | const connect = await helper.isConnectMember(job.dataValues.projectId, currentUser.jwtToken)
|
|
0 commit comments