Skip to content

validate skills before create/update jobs #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ module.exports = {
isConnectMember,
getESClient,
getUserId,
getM2Mtoken,
postEvent,
getBusApiClient,
isDocumentMissingException,
Expand Down
33 changes: 33 additions & 0 deletions src/services/JobService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const _ = require('lodash')
const Joi = require('joi')
const config = require('config')
const HttpStatus = require('http-status-codes')
const { Op } = require('sequelize')
const { v4: uuid } = require('uuid')
const helper = require('../common/helper')
Expand Down Expand Up @@ -46,6 +47,34 @@ async function _getJobCandidates (jobId) {
return candidates
}

/**
* Validate if all skills exist.
*
* @param {Array} skills the list of skills
* @returns {undefined}
*/
async function _validateSkills (skills) {
const m2mToken = await helper.getM2Mtoken()
const responses = await Promise.all(
skills.map(
skill => helper.getSkillById(`Bearer ${m2mToken}`, skill)
.then(() => {
return { found: true }
})
.catch(err => {
if (err.status !== HttpStatus.NOT_FOUND) {
throw err
}
return { found: false, skill }
})
)
)
const errResponses = responses.filter(res => !res.found)
if (errResponses.length) {
throw new errors.BadRequestError(`Invalid skills: [${errResponses.map(res => res.skill)}]`)
}
}

/**
* Get job by id
* @param {String} id the job id
Expand Down Expand Up @@ -91,6 +120,7 @@ getJob.schema = Joi.object().keys({
* @returns {Object} the created job
*/
async function createJob (currentUser, job) {
await _validateSkills(job.skills)
if (!currentUser.isBookingManager) {
const connect = await helper.isConnectMember(job.projectId, currentUser.jwtToken)
if (!connect) {
Expand Down Expand Up @@ -130,6 +160,9 @@ createJob.schema = Joi.object().keys({
* @returns {Object} the updated job
*/
async function updateJob (currentUser, id, data) {
if (data.skills) {
await _validateSkills(data.skills)
}
let job = await Job.findById(id)
if (!currentUser.isBookingManager) {
const connect = await helper.isConnectMember(job.dataValues.projectId, currentUser.jwtToken)
Expand Down