Skip to content

Commit 10a7c41

Browse files
committed
validate skills before create/update jobs
1 parent 456fc15 commit 10a7c41

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/common/helper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ module.exports = {
406406
isConnectMember,
407407
getESClient,
408408
getUserId,
409+
getM2Mtoken,
409410
postEvent,
410411
getBusApiClient,
411412
isDocumentMissingException,

src/services/JobService.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const _ = require('lodash')
66
const Joi = require('joi')
77
const config = require('config')
8+
const HttpStatus = require('http-status-codes')
89
const { Op } = require('sequelize')
910
const { v4: uuid } = require('uuid')
1011
const helper = require('../common/helper')
@@ -46,6 +47,34 @@ async function _getJobCandidates (jobId) {
4647
return candidates
4748
}
4849

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+
4978
/**
5079
* Get job by id
5180
* @param {String} id the job id
@@ -91,6 +120,7 @@ getJob.schema = Joi.object().keys({
91120
* @returns {Object} the created job
92121
*/
93122
async function createJob (currentUser, job) {
123+
await _validateSkills(job.skills)
94124
if (!currentUser.isBookingManager) {
95125
const connect = await helper.isConnectMember(job.projectId, currentUser.jwtToken)
96126
if (!connect) {
@@ -130,6 +160,9 @@ createJob.schema = Joi.object().keys({
130160
* @returns {Object} the updated job
131161
*/
132162
async function updateJob (currentUser, id, data) {
163+
if (data.skills) {
164+
await _validateSkills(data.skills)
165+
}
133166
let job = await Job.findById(id)
134167
if (!currentUser.isBookingManager) {
135168
const connect = await helper.isConnectMember(job.dataValues.projectId, currentUser.jwtToken)

0 commit comments

Comments
 (0)