diff --git a/config/default.json b/config/default.json index 87fcaa52..b302a52f 100644 --- a/config/default.json +++ b/config/default.json @@ -75,5 +75,6 @@ "EMBED_REPORTS_MAPPING": "{\"mock\": \"/embed/looks/2\"}", "ALLOWED_USERS": "[]" }, - "DEFAULT_M2M_USERID": -101 + "DEFAULT_M2M_USERID": -101, + "taasJobApiUrl": "https://api.topcoder.com/v5/jobs" } diff --git a/config/development.json b/config/development.json index 3f3e909b..d20cddd9 100644 --- a/config/development.json +++ b/config/development.json @@ -6,5 +6,6 @@ "fileServiceEndpoint": "https://api.topcoder-dev.com/v3/files/", "connectProjectsUrl": "https://connect.topcoder-dev.com/projects/", "memberServiceEndpoint": "https://api.topcoder-dev.com/v3/members", - "identityServiceEndpoint": "https://api.topcoder-dev.com/v3/" + "identityServiceEndpoint": "https://api.topcoder-dev.com/v3/", + "taasJobApiUrl": "https://api.topcoder-dev.com/v5/jobs" } diff --git a/src/events/projects/index.js b/src/events/projects/index.js index 0f8dce34..b211cd85 100644 --- a/src/events/projects/index.js +++ b/src/events/projects/index.js @@ -5,6 +5,8 @@ import _ from 'lodash'; import Joi from 'joi'; import Promise from 'bluebird'; import config from 'config'; +import axios from 'axios'; +import moment from 'moment'; import util from '../../util'; import models from '../../models'; import { createPhaseTopic } from '../projectPhases'; @@ -14,6 +16,27 @@ 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`. */ @@ -164,6 +187,40 @@ async function projectCreatedKafkaHandler(app, topic, payload) { await Promise.all(topicPromises); app.logger.debug('Topics for phases are successfully created.'); } + if (project.type === 'talent-as-a-service') { + const specialists = _.get(project, 'details.taasDefinition.specialists'); + if (!specialists || !specialists.length) { + app.logger.debug(`no specialists found in the project ${project.id}`); + return; + } + const targetSpecialists = _.filter(specialists, specialist => Number(specialist.people) > 0); // must be at least one people + await Promise.all( + _.map( + targetSpecialists, + (specialist) => { + const startDate = new Date(); + const endDate = moment(startDate).add(Number(specialist.duration), 'M'); // the unit of duration is month + const skills = specialist.skills.filter(skill => skill.id).map(skill => skill.id); + return createTaasJob({ + projectId: project.id, + externalId: _.get(project, 'external.id') || String(project.id), + description: specialist.roleTitle, + startDate, + endDate, + skills, + numPositions: Number(specialist.people), + resourceType: specialist.role, + rateType: 'hourly', + workload: specialist.workLoad.title.toLowerCase(), + }).then((job) => { + app.logger.debug(`jobId: ${job.id} job created for roleTitle ${specialist.roleTitle}`); + }).catch((err) => { + app.logger.error(`Unable to create job for ${specialist.roleTitle}: ${err.message}`); + }); + }, + ), + ); + } } module.exports = {