Skip to content

create job after project was created #595

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
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
3 changes: 2 additions & 1 deletion config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
57 changes: 57 additions & 0 deletions src/events/projects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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`.
*/
Expand Down Expand Up @@ -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 = {
Expand Down