Skip to content

Commit b75a435

Browse files
authored
create job after project was created (#595)
1 parent c44dd43 commit b75a435

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

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: 57 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,27 @@ 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+
const token = await util.getM2MToken();
26+
const headers = {
27+
'Content-Type': 'application/json',
28+
Authorization: `Bearer ${token}`,
29+
};
30+
const res = await axios
31+
.post(config.taasJobApiUrl, data, { headers })
32+
.catch((err) => {
33+
const error = new Error();
34+
error.message = _.get(err, 'response.data.message', error.message);
35+
throw error;
36+
});
37+
return res.data;
38+
};
39+
1740
/**
1841
* Payload for deprecated BUS events like `connect.notification.project.updated`.
1942
*/
@@ -164,6 +187,40 @@ async function projectCreatedKafkaHandler(app, topic, payload) {
164187
await Promise.all(topicPromises);
165188
app.logger.debug('Topics for phases are successfully created.');
166189
}
190+
if (project.type === 'talent-as-a-service') {
191+
const specialists = _.get(project, 'details.taasDefinition.specialists');
192+
if (!specialists || !specialists.length) {
193+
app.logger.debug(`no specialists found in the project ${project.id}`);
194+
return;
195+
}
196+
const targetSpecialists = _.filter(specialists, specialist => Number(specialist.people) > 0); // must be at least one people
197+
await Promise.all(
198+
_.map(
199+
targetSpecialists,
200+
(specialist) => {
201+
const startDate = new Date();
202+
const endDate = moment(startDate).add(Number(specialist.duration), 'M'); // the unit of duration is month
203+
const skills = specialist.skills.filter(skill => skill.id).map(skill => skill.id);
204+
return createTaasJob({
205+
projectId: project.id,
206+
externalId: _.get(project, 'external.id') || String(project.id),
207+
description: specialist.roleTitle,
208+
startDate,
209+
endDate,
210+
skills,
211+
numPositions: Number(specialist.people),
212+
resourceType: specialist.role,
213+
rateType: 'hourly',
214+
workload: specialist.workLoad.title.toLowerCase(),
215+
}).then((job) => {
216+
app.logger.debug(`jobId: ${job.id} job created for roleTitle ${specialist.roleTitle}`);
217+
}).catch((err) => {
218+
app.logger.error(`Unable to create job for ${specialist.roleTitle}: ${err.message}`);
219+
});
220+
},
221+
),
222+
);
223+
}
167224
}
168225

169226
module.exports = {

0 commit comments

Comments
 (0)