Skip to content

Commit 001b679

Browse files
authored
Merge pull request #767 from topcoder-platform/TSJR-282
When updating a job, send the new details to the TaaS API
2 parents fde9564 + 74cd279 commit 001b679

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed

src/events/busApi.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +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';
18+
import { createTaasJobsFromProject, updateTaasJobsFromProject } from '../events/projects/postTaasJobs';
1919

2020
/**
2121
* Map of project status and event name sent to bus api
@@ -158,6 +158,13 @@ module.exports = (app, logger) => {
158158
userId: req.authUser.userId,
159159
initiatorUserId: req.authUser.userId,
160160
}, logger);
161+
// update taas jobs from project of type `talent-as-a-service`
162+
if (updated.type === 'talent-as-a-service') {
163+
updateTaasJobsFromProject(req, updated, logger)
164+
.catch((error) => {
165+
logger.error(`Error while updating TaaS jobs: ${error}`);
166+
});
167+
}
161168
}
162169
});
163170

src/events/projects/postTaasJobs.js

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Event handler for the creation of `talent-as-a-service` projects.
33
*/
44

5-
import _ from 'lodash';
5+
import _, { forEach } from 'lodash';
66
import config from 'config';
77
import axios from 'axios';
88
import models from '../../models';
@@ -75,4 +75,115 @@ async function createTaasJobsFromProject(req, project, logger) {
7575
await projectWithJobs.save();
7676
}
7777

78-
module.exports = createTaasJobsFromProject;
78+
/**
79+
* Update taas job.
80+
*
81+
* @param {String} authHeader the authorization header
82+
* @param {Object} data the job data
83+
* @return {Object} the job created
84+
*/
85+
async function updateTaasJob(authHeader, data) {
86+
const headers = {
87+
'Content-Type': 'application/json',
88+
Authorization: authHeader,
89+
};
90+
// Remove the jobId because it can't be passed to the taas API PATCH call
91+
const jobId = data.jobId;
92+
delete data.jobId;
93+
94+
const res = await axios
95+
.patch(`${config.taasJobApiUrl}/${jobId}`, data, { headers })
96+
.catch((err) => {
97+
const error = new Error();
98+
error.message = _.get(err, 'response.data.message', error.message);
99+
throw error;
100+
});
101+
return res.data;
102+
}
103+
104+
/**
105+
* Update taas jobs from project of type `talent-as-a-service` using the token from current user.
106+
* This is called when a `talent-as-a-service` project is updated
107+
*
108+
* @param {Object} req the request object
109+
* @param {Object} project the project data
110+
* @param {Object} logger the logger object
111+
* @return {Object} the taas jobs created
112+
*/
113+
async function updateTaasJobsFromProject(req, project, logger) {
114+
const originalJobs = _.get(project, 'details.taasDefinition.taasJobs');
115+
if (!originalJobs || !originalJobs.length) {
116+
logger.debug(`No jobs found in the project id to update: ${project.id}`);
117+
return;
118+
}
119+
logger.debug(`${originalJobs.length} jobs found in the project id to update: ${project.id}`);
120+
121+
const updateJobs = [];
122+
const createJobs = [];
123+
124+
// Split new jobs out from ones that need to be updated
125+
// If a job already has an ID assigned, assume it needs to be updated
126+
forEach(originalJobs, (job) => {
127+
if (job.jobId) {
128+
updateJobs.push(job);
129+
} else {
130+
createJobs.push(job);
131+
}
132+
});
133+
134+
await Promise.all(
135+
_.map(
136+
updateJobs,
137+
job => updateTaasJob(req.headers.authorization, {
138+
jobId: job.jobId,
139+
title: job.title,
140+
description: job.description,
141+
duration: Number(job.duration),
142+
skills: job.skills,
143+
numPositions: Number(job.people),
144+
resourceType: _.get(job, 'role.value', ''),
145+
workload: _.get(job, 'workLoad.title', '').toLowerCase(),
146+
}).then((updatedJob) => {
147+
logger.debug(`jobId: ${updatedJob.id} job updated with title "${updatedJob.title}"`);
148+
}).catch((err) => {
149+
logger.error(`Unable to update job with title "${job.title}": ${err.message}`);
150+
}),
151+
),
152+
);
153+
154+
await Promise.all(
155+
_.map(
156+
createJobs,
157+
job => createTaasJob(req.headers.authorization, {
158+
projectId: project.id,
159+
title: job.title,
160+
description: job.description,
161+
duration: Number(job.duration),
162+
skills: job.skills,
163+
numPositions: Number(job.people),
164+
resourceType: _.get(job, 'role.value', ''),
165+
rateType: 'weekly', // hardcode for now
166+
workload: _.get(job, 'workLoad.title', '').toLowerCase(),
167+
}).then((createdJob) => {
168+
logger.debug(`jobId: ${createdJob.id} job created with title "${createdJob.title}"`);
169+
/* eslint no-param-reassign: "error" */
170+
job.jobId = createdJob.id;
171+
}).catch((err) => {
172+
logger.error(`Unable to create job with title "${job.title}": ${err.message}`);
173+
}),
174+
),
175+
);
176+
const jobs = _.concat(updateJobs, createJobs);
177+
const projectWithJobs = await models.Project.findByPk(project.id);
178+
if (!projectWithJobs) {
179+
logger.error(`Project not found for id ${project.id}, so couldn't save TaaS Job IDs`);
180+
}
181+
projectWithJobs.details.taasDefinition.taasJobs = jobs;
182+
projectWithJobs.changed('details', true);
183+
await projectWithJobs.save();
184+
}
185+
186+
module.exports = {
187+
createTaasJobsFromProject,
188+
updateTaasJobsFromProject,
189+
};

0 commit comments

Comments
 (0)