Skip to content

Commit c44df10

Browse files
authored
Merge pull request #626 from imcaizheng/create-taas-jobs-update
Create Jobs in TaaS using user token instead of M2M
2 parents 9d8f317 + 06afee8 commit c44df10

File tree

3 files changed

+82
-58
lines changed

3 files changed

+82
-58
lines changed

src/events/busApi.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { createEvent } from '../services/busApi';
1616
import models from '../models';
1717
import util from '../util';
18+
import createTaasJobsFromProject from '../events/projects/postTaasJobs';
1819

1920
/**
2021
* Map of project status and event name sent to bus api
@@ -57,6 +58,14 @@ module.exports = (app, logger) => {
5758
app.on(EVENT.ROUTING_KEY.PROJECT_DRAFT_CREATED, ({ req, project }) => {
5859
logger.debug('receive PROJECT_DRAFT_CREATED event');
5960

61+
// create taas jobs from project of type `talent-as-a-service`
62+
if (project.type === 'talent-as-a-service') {
63+
createTaasJobsFromProject(req, project, logger)
64+
.catch((error) => {
65+
logger.error(`Error while creating TaaS jobs: ${error}`);
66+
});
67+
}
68+
6069
// send event to bus api
6170
createEvent(BUS_API_EVENT.PROJECT_CREATED, _.assign(project, {
6271
refCode: _.get(project, 'details.utm.code'),

src/events/projects/index.js

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import _ from 'lodash';
55
import Joi from 'joi';
66
import Promise from 'bluebird';
77
import config from 'config';
8-
import axios from 'axios';
98
import util from '../../util';
109
import models from '../../models';
1110
import { createPhaseTopic } from '../projectPhases';
@@ -15,27 +14,6 @@ const ES_PROJECT_INDEX = config.get('elasticsearchConfig.indexName');
1514
const ES_PROJECT_TYPE = config.get('elasticsearchConfig.docType');
1615
const eClient = util.getElasticSearchClient();
1716

18-
/**
19-
* creates taas job
20-
* @param {Object} data the job data
21-
* @return {Object} the job created
22-
*/
23-
const createTaasJob = async (data) => {
24-
const token = await util.getM2MToken();
25-
const headers = {
26-
'Content-Type': 'application/json',
27-
Authorization: `Bearer ${token}`,
28-
};
29-
const res = await axios
30-
.post(config.taasJobApiUrl, data, { headers })
31-
.catch((err) => {
32-
const error = new Error();
33-
error.message = _.get(err, 'response.data.message', error.message);
34-
throw error;
35-
});
36-
return res.data;
37-
};
38-
3917
/**
4018
* Payload for deprecated BUS events like `connect.notification.project.updated`.
4119
*/
@@ -186,42 +164,6 @@ async function projectCreatedKafkaHandler(app, topic, payload) {
186164
await Promise.all(topicPromises);
187165
app.logger.debug('Topics for phases are successfully created.');
188166
}
189-
try {
190-
if (project.type === 'talent-as-a-service') {
191-
const jobs = _.get(project, 'details.taasDefinition.taasJobs');
192-
if (!jobs || !jobs.length) {
193-
app.logger.debug(`no jobs found in the project id: ${project.id}`);
194-
return;
195-
}
196-
app.logger.debug(`${jobs.length} jobs found in the project id: ${project.id}`);
197-
await Promise.all(
198-
_.map(
199-
jobs,
200-
(job) => {
201-
// make sure that skills would be unique in the list and only include ones with 'skillId' (actually they all suppose to be with skillId)
202-
const skills = _.chain(job.skills).map('skillId').uniq().compact()
203-
.value();
204-
return createTaasJob({
205-
projectId: project.id,
206-
title: job.title,
207-
description: job.description,
208-
skills,
209-
numPositions: Number(job.people),
210-
resourceType: _.get(job, 'role.value', ''),
211-
rateType: 'weekly', // hardcode for now
212-
workload: _.get(job, 'workLoad.title', '').toLowerCase(),
213-
}).then((createdJob) => {
214-
app.logger.debug(`jobId: ${createdJob.id} job created with title "${createdJob.title}"`);
215-
}).catch((err) => {
216-
app.logger.error(`Unable to create job with title "${job.title}": ${err.message}`);
217-
});
218-
},
219-
),
220-
);
221-
}
222-
} catch (error) {
223-
app.logger.error(`Error while creating TaaS jobs: ${error}`);
224-
}
225167
}
226168

227169
module.exports = {

src/events/projects/postTaasJobs.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Event handler for the creation of `talent-as-a-service` projects.
3+
*/
4+
5+
import _ from 'lodash';
6+
import config from 'config';
7+
import axios from 'axios';
8+
9+
/**
10+
* Create taas job.
11+
*
12+
* @param {String} authToken the authorization token
13+
* @param {Object} data the job data
14+
* @return {Object} the job created
15+
*/
16+
async function createTaasJob(authToken, data) {
17+
const headers = {
18+
'Content-Type': 'application/json',
19+
Authorization: authToken,
20+
};
21+
const res = await axios
22+
.post(config.taasJobApiUrl, data, { headers })
23+
.catch((err) => {
24+
const error = new Error();
25+
error.message = _.get(err, 'response.data.message', error.message);
26+
throw error;
27+
});
28+
return res.data;
29+
}
30+
31+
/**
32+
* Create taas jobs from project of type `talent-as-a-service` using the token from current user.
33+
*
34+
* @param {Object} req the request object
35+
* @param {Object} project the project data
36+
* @param {Object} logger the logger object
37+
* @return {Object} the taas jobs created
38+
*/
39+
async function createTaasJobsFromProject(req, project, logger) {
40+
const jobs = _.get(project, 'details.taasDefinition.taasJobs');
41+
if (!jobs || !jobs.length) {
42+
logger.debug(`no jobs found in the project id: ${project.id}`);
43+
return;
44+
}
45+
logger.debug(`${jobs.length} jobs found in the project id: ${project.id}`);
46+
await Promise.all(
47+
_.map(
48+
jobs,
49+
(job) => {
50+
// make sure that skills would be unique in the list and only include ones with 'skillId' (actually they all suppose to be with skillId)
51+
const skills = _.chain(job.skills).map('skillId').uniq().compact()
52+
.value();
53+
return createTaasJob(req.headers.authorization, {
54+
projectId: project.id,
55+
title: job.title,
56+
description: job.description,
57+
duration: job.duration,
58+
skills,
59+
numPositions: Number(job.people),
60+
resourceType: _.get(job, 'role.value', ''),
61+
rateType: 'weekly', // hardcode for now
62+
workload: _.get(job, 'workLoad.title', '').toLowerCase(),
63+
}).then((createdJob) => {
64+
logger.debug(`jobId: ${createdJob.id} job created with title "${createdJob.title}"`);
65+
}).catch((err) => {
66+
logger.error(`Unable to create job with title "${job.title}": ${err.message}`);
67+
});
68+
},
69+
),
70+
);
71+
}
72+
73+
module.exports = createTaasJobsFromProject;

0 commit comments

Comments
 (0)