Skip to content

Commit d4cf43f

Browse files
authored
Merge pull request #596 from topcoder-platform/feature/create-taas-jobs
[Kafka Consumer] Create TaaS Jobs during project creation
2 parents ac22ec3 + 4baf155 commit d4cf43f

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ workflows:
114114
- test
115115
filters:
116116
branches:
117-
only: ['develop']
117+
only: ['develop', 'feature/create-taas-jobs']
118118
- deployProd:
119119
context : org-global
120120
requires:

config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@
7575
"EMBED_REPORTS_MAPPING": "{\"mock\": \"/embed/looks/2\"}",
7676
"ALLOWED_USERS": "[]"
7777
},
78-
"DEFAULT_M2M_USERID": -101
78+
"DEFAULT_M2M_USERID": -101,
79+
"taasJobApiUrl": "https://api.topcoder.com/v5/jobs"
7980
}

config/development.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"fileServiceEndpoint": "https://api.topcoder-dev.com/v3/files/",
77
"connectProjectsUrl": "https://connect.topcoder-dev.com/projects/",
88
"memberServiceEndpoint": "https://api.topcoder-dev.com/v3/members",
9-
"identityServiceEndpoint": "https://api.topcoder-dev.com/v3/"
9+
"identityServiceEndpoint": "https://api.topcoder-dev.com/v3/",
10+
"taasJobApiUrl": "https://api.topcoder-dev.com/v5/jobs"
1011
}

src/events/projects/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import _ from 'lodash';
55
import Joi from 'joi';
66
import Promise from 'bluebird';
77
import config from 'config';
8+
import axios from 'axios';
9+
import moment from 'moment';
810
import util from '../../util';
911
import models from '../../models';
1012
import { createPhaseTopic } from '../projectPhases';
@@ -14,6 +16,30 @@ const ES_PROJECT_INDEX = config.get('elasticsearchConfig.indexName');
1416
const ES_PROJECT_TYPE = config.get('elasticsearchConfig.docType');
1517
const eClient = util.getElasticSearchClient();
1618

19+
/**
20+
* creates taas job
21+
* @param {Object} data the job data
22+
* @return {Object} the job created
23+
*/
24+
const createTaasJob = async (data) => {
25+
// TODO uncomment when TaaS API supports M2M tokens
26+
// see https://github.com/topcoder-platform/taas-apis/issues/40
27+
// const token = await util.getM2MToken();
28+
const token = process.env.TAAS_API_TOKEN;
29+
const headers = {
30+
'Content-Type': 'application/json',
31+
Authorization: `Bearer ${token}`,
32+
};
33+
const res = await axios
34+
.post(config.taasJobApiUrl, data, { headers })
35+
.catch((err) => {
36+
const error = new Error();
37+
error.message = _.get(err, 'response.data.message', error.message);
38+
throw error;
39+
});
40+
return res.data;
41+
};
42+
1743
/**
1844
* Payload for deprecated BUS events like `connect.notification.project.updated`.
1945
*/
@@ -164,6 +190,46 @@ async function projectCreatedKafkaHandler(app, topic, payload) {
164190
await Promise.all(topicPromises);
165191
app.logger.debug('Topics for phases are successfully created.');
166192
}
193+
if (project.type === 'talent-as-a-service') {
194+
const specialists = _.get(project, 'details.taasDefinition.specialists');
195+
if (!specialists || !specialists.length) {
196+
app.logger.debug(`no specialists found in the project ${project.id}`);
197+
return;
198+
}
199+
const targetSpecialists = _.filter(specialists, specialist => Number(specialist.people) > 0); // must be at least one people
200+
await Promise.all(
201+
_.map(
202+
targetSpecialists,
203+
(specialist) => {
204+
const startDate = new Date();
205+
const endDate = moment(startDate).add(Number(specialist.duration), 'M'); // the unit of duration is month
206+
// make sure that skills would be unique in the list
207+
const skills = _.uniq(
208+
// use both, required and additional skills for jobs
209+
specialist.skills.concat(specialist.additionalSkills)
210+
// only include skills with `skillId` and ignore custom skills in jobs
211+
.filter(skill => skill.skillId).map(skill => skill.skillId),
212+
);
213+
return createTaasJob({
214+
projectId: project.id,
215+
externalId: '0', // hardcode for now
216+
description: specialist.roleTitle,
217+
startDate,
218+
endDate,
219+
skills,
220+
numPositions: Number(specialist.people),
221+
resourceType: specialist.role,
222+
rateType: 'hourly', // hardcode for now
223+
workload: _.get(specialist, 'workLoad.title', '').toLowerCase(),
224+
}).then((job) => {
225+
app.logger.debug(`jobId: ${job.id} job created for roleTitle ${specialist.roleTitle}`);
226+
}).catch((err) => {
227+
app.logger.error(`Unable to create job for ${specialist.roleTitle}: ${err.message}`);
228+
});
229+
},
230+
),
231+
);
232+
}
167233
}
168234

169235
module.exports = {

0 commit comments

Comments
 (0)