@@ -5,6 +5,8 @@ import _ from 'lodash';
5
5
import Joi from 'joi' ;
6
6
import Promise from 'bluebird' ;
7
7
import config from 'config' ;
8
+ import axios from 'axios' ;
9
+ import moment from 'moment' ;
8
10
import util from '../../util' ;
9
11
import models from '../../models' ;
10
12
import { createPhaseTopic } from '../projectPhases' ;
@@ -14,6 +16,27 @@ const ES_PROJECT_INDEX = config.get('elasticsearchConfig.indexName');
14
16
const ES_PROJECT_TYPE = config . get ( 'elasticsearchConfig.docType' ) ;
15
17
const eClient = util . getElasticSearchClient ( ) ;
16
18
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
+
17
40
/**
18
41
* Payload for deprecated BUS events like `connect.notification.project.updated`.
19
42
*/
@@ -164,6 +187,40 @@ async function projectCreatedKafkaHandler(app, topic, payload) {
164
187
await Promise . all ( topicPromises ) ;
165
188
app . logger . debug ( 'Topics for phases are successfully created.' ) ;
166
189
}
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
+ }
167
224
}
168
225
169
226
module . exports = {
0 commit comments