Skip to content

Create Jobs in TaaS using user token instead of M2M #626

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
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
9 changes: 9 additions & 0 deletions src/events/busApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { createEvent } from '../services/busApi';
import models from '../models';
import util from '../util';
import createTaasJobsFromProject from '../events/projects/postTaasJobs';

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

// create taas jobs from project of type `talent-as-a-service`
if (project.type === 'talent-as-a-service') {
createTaasJobsFromProject(req, project, logger)
.catch((error) => {
logger.error(`Error while creating TaaS jobs: ${error}`);
});
}

// send event to bus api
createEvent(BUS_API_EVENT.PROJECT_CREATED, _.assign(project, {
refCode: _.get(project, 'details.utm.code'),
Expand Down
58 changes: 0 additions & 58 deletions src/events/projects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import _ from 'lodash';
import Joi from 'joi';
import Promise from 'bluebird';
import config from 'config';
import axios from 'axios';
import util from '../../util';
import models from '../../models';
import { createPhaseTopic } from '../projectPhases';
Expand All @@ -15,27 +14,6 @@ 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 @@ -186,42 +164,6 @@ async function projectCreatedKafkaHandler(app, topic, payload) {
await Promise.all(topicPromises);
app.logger.debug('Topics for phases are successfully created.');
}
try {
if (project.type === 'talent-as-a-service') {
const jobs = _.get(project, 'details.taasDefinition.taasJobs');
if (!jobs || !jobs.length) {
app.logger.debug(`no jobs found in the project id: ${project.id}`);
return;
}
app.logger.debug(`${jobs.length} jobs found in the project id: ${project.id}`);
await Promise.all(
_.map(
jobs,
(job) => {
// make sure that skills would be unique in the list and only include ones with 'skillId' (actually they all suppose to be with skillId)
const skills = _.chain(job.skills).map('skillId').uniq().compact()
.value();
return createTaasJob({
projectId: project.id,
title: job.title,
description: job.description,
skills,
numPositions: Number(job.people),
resourceType: _.get(job, 'role.value', ''),
rateType: 'weekly', // hardcode for now
workload: _.get(job, 'workLoad.title', '').toLowerCase(),
}).then((createdJob) => {
app.logger.debug(`jobId: ${createdJob.id} job created with title "${createdJob.title}"`);
}).catch((err) => {
app.logger.error(`Unable to create job with title "${job.title}": ${err.message}`);
});
},
),
);
}
} catch (error) {
app.logger.error(`Error while creating TaaS jobs: ${error}`);
}
}

module.exports = {
Expand Down
73 changes: 73 additions & 0 deletions src/events/projects/postTaasJobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Event handler for the creation of `talent-as-a-service` projects.
*/

import _ from 'lodash';
import config from 'config';
import axios from 'axios';

/**
* Create taas job.
*
* @param {String} authToken the authorization token
* @param {Object} data the job data
* @return {Object} the job created
*/
async function createTaasJob(authToken, data) {
const headers = {
'Content-Type': 'application/json',
Authorization: authToken,
};
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;
}

/**
* Create taas jobs from project of type `talent-as-a-service` using the token from current user.
*
* @param {Object} req the request object
* @param {Object} project the project data
* @param {Object} logger the logger object
* @return {Object} the taas jobs created
*/
async function createTaasJobsFromProject(req, project, logger) {
const jobs = _.get(project, 'details.taasDefinition.taasJobs');
if (!jobs || !jobs.length) {
logger.debug(`no jobs found in the project id: ${project.id}`);
return;
}
logger.debug(`${jobs.length} jobs found in the project id: ${project.id}`);
await Promise.all(
_.map(
jobs,
(job) => {
// make sure that skills would be unique in the list and only include ones with 'skillId' (actually they all suppose to be with skillId)
const skills = _.chain(job.skills).map('skillId').uniq().compact()
.value();
return createTaasJob(req.headers.authorization, {
projectId: project.id,
title: job.title,
description: job.description,
duration: job.duration,
skills,
numPositions: Number(job.people),
resourceType: _.get(job, 'role.value', ''),
rateType: 'weekly', // hardcode for now
workload: _.get(job, 'workLoad.title', '').toLowerCase(),
}).then((createdJob) => {
logger.debug(`jobId: ${createdJob.id} job created with title "${createdJob.title}"`);
}).catch((err) => {
logger.error(`Unable to create job with title "${job.title}": ${err.message}`);
});
},
),
);
}

module.exports = createTaasJobsFromProject;