diff --git a/src/common/helper.js b/src/common/helper.js index 4df5766b..cfb4e2b3 100644 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -295,15 +295,42 @@ function isDocumentMissingException (err) { * @returns the request result */ async function getProjects (token) { - const url = `${config.TC_API}/projects?type=talent-as-a-service` +/* const url = `${config.TC_API}/projects?type=talent-as-a-service` const res = await request .get(url) + .query({ + memberOnly: true + }) .set('Authorization', token) .set('Content-Type', 'application/json') .set('Accept', 'application/json') localLogger.debug({ context: 'getProjects', message: `response body: ${JSON.stringify(res.body)}` }) return _.map(res.body, item => { return _.pick(item, ['id', 'name']) + })*/ + + const url = `${config.TC_API}/projects?type=talent-as-a-service` + let data = [] + let page = 1 + while (true) { + const res = await request + .get(url) + .query({ + page, + memberOnly: true + }) + .set('Authorization', token) + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + localLogger.debug({ context: 'getProjects', message: `page ${page} - response body: ${JSON.stringify(res.body)}` }) + data = [...data, ...res.body] + page += 1 + if (!res.headers['x-next-page']) { + break + } + } + return _.map(data, item => { + return _.pick(item, ['id', 'name']) }) } @@ -330,7 +357,7 @@ async function getUserById (token, userId) { * @returns the request result */ async function getMembers (token, handles) { - const handlesStr = _.map(handles, handle => { + /* const handlesStr = _.map(handles, handle => { return '%22' + handle.toLowerCase() + '%22' }).join(',') const url = `${config.TC_API}/members?fields=userId,handleLower,photoURL&handlesLower=[${handlesStr}]` @@ -341,7 +368,32 @@ async function getMembers (token, handles) { .set('Content-Type', 'application/json') .set('Accept', 'application/json') localLogger.debug({ context: 'getMembers', message: `response body: ${JSON.stringify(res.body)}` }) - return res.body + return res.body */ + + const handlesStr = _.map(handles, handle => { + return '%22' + handle.toLowerCase() + '%22' + }).join(',') + const url = `${config.TC_API}/members?fields=userId,handleLower,photoURL&handlesLower=[${handlesStr}]` + + let data = [] + let page = 1 + while (true) { + const res = await request + .get(url) + .query({ + page + }) + .set('Authorization', token) + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + localLogger.debug({ context: 'getMembers', message: `page ${page} - response body: ${JSON.stringify(res.body)}` }) + data = [...data, ...res.body] + page += 1 + if (!res.headers['x-next-page']) { + break + } + } + return data } /** @@ -410,6 +462,7 @@ module.exports = { getBusApiClient, isDocumentMissingException, getProjects, + getM2Mtoken, getUserById, getMembers, getProjectById, diff --git a/src/services/JobService.js b/src/services/JobService.js index f4953782..4787d904 100644 --- a/src/services/JobService.js +++ b/src/services/JobService.js @@ -4,6 +4,7 @@ const _ = require('lodash') const Joi = require('joi') +const HttpStatus = require('http-status-codes') const config = require('config') const { Op } = require('sequelize') const { v4: uuid } = require('uuid') @@ -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 @@ -97,6 +126,7 @@ async function createJob (currentUser, job) { throw new errors.ForbiddenError('You are not allowed to perform this action!') } } + await _validateSkills(job.skills) job.id = uuid() job.createdAt = new Date() job.createdBy = await helper.getUserId(currentUser.userId) @@ -137,6 +167,9 @@ async function updateJob (currentUser, id, data) { throw new errors.ForbiddenError('You are not allowed to perform this action!') } } + if (data.skills) { + await _validateSkills(data.skills) + } data.updatedAt = new Date() data.updatedBy = await helper.getUserId(currentUser.userId)