|
2 | 2 | * Event handler for the creation of `talent-as-a-service` projects.
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -import _ from 'lodash'; |
| 5 | +import _, { forEach } from 'lodash'; |
6 | 6 | import config from 'config';
|
7 | 7 | import axios from 'axios';
|
8 | 8 | import models from '../../models';
|
@@ -75,4 +75,115 @@ async function createTaasJobsFromProject(req, project, logger) {
|
75 | 75 | await projectWithJobs.save();
|
76 | 76 | }
|
77 | 77 |
|
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