From 06afee8b3717748f3bedc17ee75808aa1a197bae Mon Sep 17 00:00:00 2001 From: imcaizheng Date: Fri, 12 Feb 2021 12:53:54 +0800 Subject: [PATCH] Create Jobs in TaaS using user token instead of M2M --- src/events/busApi.js | 9 ++++ src/events/projects/index.js | 58 ----------------------- src/events/projects/postTaasJobs.js | 73 +++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 58 deletions(-) create mode 100644 src/events/projects/postTaasJobs.js diff --git a/src/events/busApi.js b/src/events/busApi.js index 7b01e055..4e51c98f 100644 --- a/src/events/busApi.js +++ b/src/events/busApi.js @@ -15,6 +15,7 @@ import { import { createEvent } from '../services/busApi'; import models from '../models'; import util from '../util'; +import createTaasJobsFromProject from '../events/projects/postTaasJobs'; /** * Map of project status and event name sent to bus api @@ -57,6 +58,14 @@ module.exports = (app, logger) => { app.on(EVENT.ROUTING_KEY.PROJECT_DRAFT_CREATED, ({ req, project }) => { logger.debug('receive PROJECT_DRAFT_CREATED event'); + // create taas jobs from project of type `talent-as-a-service` + if (project.type === 'talent-as-a-service') { + createTaasJobsFromProject(req, project, logger) + .catch((error) => { + logger.error(`Error while creating TaaS jobs: ${error}`); + }); + } + // send event to bus api createEvent(BUS_API_EVENT.PROJECT_CREATED, _.assign(project, { refCode: _.get(project, 'details.utm.code'), diff --git a/src/events/projects/index.js b/src/events/projects/index.js index a3db47c5..b3401098 100644 --- a/src/events/projects/index.js +++ b/src/events/projects/index.js @@ -5,7 +5,6 @@ import _ from 'lodash'; import Joi from 'joi'; import Promise from 'bluebird'; import config from 'config'; -import axios from 'axios'; import util from '../../util'; import models from '../../models'; import { createPhaseTopic } from '../projectPhases'; @@ -15,27 +14,6 @@ const ES_PROJECT_INDEX = config.get('elasticsearchConfig.indexName'); const ES_PROJECT_TYPE = config.get('elasticsearchConfig.docType'); const eClient = util.getElasticSearchClient(); -/** - * creates taas job - * @param {Object} data the job data - * @return {Object} the job created - */ -const createTaasJob = async (data) => { - const token = await util.getM2MToken(); - const headers = { - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}`, - }; - const res = await axios - .post(config.taasJobApiUrl, data, { headers }) - .catch((err) => { - const error = new Error(); - error.message = _.get(err, 'response.data.message', error.message); - throw error; - }); - return res.data; -}; - /** * Payload for deprecated BUS events like `connect.notification.project.updated`. */ @@ -186,42 +164,6 @@ async function projectCreatedKafkaHandler(app, topic, payload) { await Promise.all(topicPromises); app.logger.debug('Topics for phases are successfully created.'); } - try { - if (project.type === 'talent-as-a-service') { - const jobs = _.get(project, 'details.taasDefinition.taasJobs'); - if (!jobs || !jobs.length) { - app.logger.debug(`no jobs found in the project id: ${project.id}`); - return; - } - app.logger.debug(`${jobs.length} jobs found in the project id: ${project.id}`); - await Promise.all( - _.map( - jobs, - (job) => { - // make sure that skills would be unique in the list and only include ones with 'skillId' (actually they all suppose to be with skillId) - const skills = _.chain(job.skills).map('skillId').uniq().compact() - .value(); - return createTaasJob({ - projectId: project.id, - title: job.title, - description: job.description, - skills, - numPositions: Number(job.people), - resourceType: _.get(job, 'role.value', ''), - rateType: 'weekly', // hardcode for now - workload: _.get(job, 'workLoad.title', '').toLowerCase(), - }).then((createdJob) => { - app.logger.debug(`jobId: ${createdJob.id} job created with title "${createdJob.title}"`); - }).catch((err) => { - app.logger.error(`Unable to create job with title "${job.title}": ${err.message}`); - }); - }, - ), - ); - } - } catch (error) { - app.logger.error(`Error while creating TaaS jobs: ${error}`); - } } module.exports = { diff --git a/src/events/projects/postTaasJobs.js b/src/events/projects/postTaasJobs.js new file mode 100644 index 00000000..757c810b --- /dev/null +++ b/src/events/projects/postTaasJobs.js @@ -0,0 +1,73 @@ +/** + * Event handler for the creation of `talent-as-a-service` projects. + */ + +import _ from 'lodash'; +import config from 'config'; +import axios from 'axios'; + +/** + * Create taas job. + * + * @param {String} authToken the authorization token + * @param {Object} data the job data + * @return {Object} the job created + */ +async function createTaasJob(authToken, data) { + const headers = { + 'Content-Type': 'application/json', + Authorization: authToken, + }; + const res = await axios + .post(config.taasJobApiUrl, data, { headers }) + .catch((err) => { + const error = new Error(); + error.message = _.get(err, 'response.data.message', error.message); + throw error; + }); + return res.data; +} + +/** + * Create taas jobs from project of type `talent-as-a-service` using the token from current user. + * + * @param {Object} req the request object + * @param {Object} project the project data + * @param {Object} logger the logger object + * @return {Object} the taas jobs created + */ +async function createTaasJobsFromProject(req, project, logger) { + const jobs = _.get(project, 'details.taasDefinition.taasJobs'); + if (!jobs || !jobs.length) { + logger.debug(`no jobs found in the project id: ${project.id}`); + return; + } + logger.debug(`${jobs.length} jobs found in the project id: ${project.id}`); + await Promise.all( + _.map( + jobs, + (job) => { + // make sure that skills would be unique in the list and only include ones with 'skillId' (actually they all suppose to be with skillId) + const skills = _.chain(job.skills).map('skillId').uniq().compact() + .value(); + return createTaasJob(req.headers.authorization, { + projectId: project.id, + title: job.title, + description: job.description, + duration: job.duration, + skills, + numPositions: Number(job.people), + resourceType: _.get(job, 'role.value', ''), + rateType: 'weekly', // hardcode for now + workload: _.get(job, 'workLoad.title', '').toLowerCase(), + }).then((createdJob) => { + logger.debug(`jobId: ${createdJob.id} job created with title "${createdJob.title}"`); + }).catch((err) => { + logger.error(`Unable to create job with title "${job.title}": ${err.message}`); + }); + }, + ), + ); +} + +module.exports = createTaasJobsFromProject;