@@ -5,6 +5,8 @@ import _ from 'lodash';
55import Joi from 'joi' ;
66import Promise from 'bluebird' ;
77import config from 'config' ;
8+ import axios from 'axios' ;
9+ import moment from 'moment' ;
810import util from '../../util' ;
911import models from '../../models' ;
1012import { createPhaseTopic } from '../projectPhases' ;
@@ -14,6 +16,30 @@ const ES_PROJECT_INDEX = config.get('elasticsearchConfig.indexName');
1416const ES_PROJECT_TYPE = config . get ( 'elasticsearchConfig.docType' ) ;
1517const eClient = util . getElasticSearchClient ( ) ;
1618
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+ // TODO uncomment when TaaS API supports M2M tokens
26+ // see https://github.com/topcoder-platform/taas-apis/issues/40
27+ // const token = await util.getM2MToken();
28+ const token = process . env . TAAS_API_TOKEN ;
29+ const headers = {
30+ 'Content-Type' : 'application/json' ,
31+ Authorization : `Bearer ${ token } ` ,
32+ } ;
33+ const res = await axios
34+ . post ( config . taasJobApiUrl , data , { headers } )
35+ . catch ( ( err ) => {
36+ const error = new Error ( ) ;
37+ error . message = _ . get ( err , 'response.data.message' , error . message ) ;
38+ throw error ;
39+ } ) ;
40+ return res . data ;
41+ } ;
42+
1743/**
1844 * Payload for deprecated BUS events like `connect.notification.project.updated`.
1945 */
@@ -164,6 +190,46 @@ async function projectCreatedKafkaHandler(app, topic, payload) {
164190 await Promise . all ( topicPromises ) ;
165191 app . logger . debug ( 'Topics for phases are successfully created.' ) ;
166192 }
193+ if ( project . type === 'talent-as-a-service' ) {
194+ const specialists = _ . get ( project , 'details.taasDefinition.specialists' ) ;
195+ if ( ! specialists || ! specialists . length ) {
196+ app . logger . debug ( `no specialists found in the project ${ project . id } ` ) ;
197+ return ;
198+ }
199+ const targetSpecialists = _ . filter ( specialists , specialist => Number ( specialist . people ) > 0 ) ; // must be at least one people
200+ await Promise . all (
201+ _ . map (
202+ targetSpecialists ,
203+ ( specialist ) => {
204+ const startDate = new Date ( ) ;
205+ const endDate = moment ( startDate ) . add ( Number ( specialist . duration ) , 'M' ) ; // the unit of duration is month
206+ // make sure that skills would be unique in the list
207+ const skills = _ . uniq (
208+ // use both, required and additional skills for jobs
209+ specialist . skills . concat ( specialist . additionalSkills )
210+ // only include skills with `skillId` and ignore custom skills in jobs
211+ . filter ( skill => skill . skillId ) . map ( skill => skill . skillId ) ,
212+ ) ;
213+ return createTaasJob ( {
214+ projectId : project . id ,
215+ externalId : '0' , // hardcode for now
216+ description : specialist . roleTitle ,
217+ startDate,
218+ endDate,
219+ skills,
220+ numPositions : Number ( specialist . people ) ,
221+ resourceType : specialist . role ,
222+ rateType : 'hourly' , // hardcode for now
223+ workload : _ . get ( specialist , 'workLoad.title' , '' ) . toLowerCase ( ) ,
224+ } ) . then ( ( job ) => {
225+ app . logger . debug ( `jobId: ${ job . id } job created for roleTitle ${ specialist . roleTitle } ` ) ;
226+ } ) . catch ( ( err ) => {
227+ app . logger . error ( `Unable to create job for ${ specialist . roleTitle } : ${ err . message } ` ) ;
228+ } ) ;
229+ } ,
230+ ) ,
231+ ) ;
232+ }
167233}
168234
169235module . exports = {
0 commit comments