@@ -11,10 +11,8 @@ const logger = require('../common/logger')
11
11
const helper = require ( '../common/helper' )
12
12
const constants = require ( '../constants' )
13
13
const groupService = require ( './groupsService' )
14
+ const termsService = require ( './termsService' )
14
15
const copilotPaymentService = require ( './copilotPaymentService' )
15
- // TODO: Remove this
16
- // const showdown = require('showdown')
17
- // const converter = new showdown.Converter()
18
16
19
17
/**
20
18
* Get group information by V5 UUID
@@ -26,30 +24,85 @@ async function getGroup (v5GroupId, m2mToken) {
26
24
return response . body
27
25
}
28
26
27
+ /**
28
+ * Get terms information by V5 UUID
29
+ * @param {String } v5TermsId the v5 terms UUID
30
+ * @param {String } m2mToken token for accessing the API
31
+ */
32
+ async function getV5Terms ( v5TermsId , m2mToken ) {
33
+ const response = await helper . getRequest ( `${ config . V5_TERMS_API_URL } /${ v5TermsId } ` , m2mToken )
34
+ return response . body
35
+ }
36
+
37
+ // /**
38
+ // * Get resource role information by V5 UUID
39
+ // * @param {String } v5RoleId the v5 role UUID
40
+ // * @param {String } m2mToken token for accessing the API
41
+ // */
42
+ // async function getV5Role (v5RoleId, m2mToken) {
43
+ // const response = await helper.getRequest(`${config.V5_RESOURCE_ROLES_API_URL}?id=${v5RoleId}`, m2mToken)
44
+ // return response.body[0]
45
+ // }
46
+
29
47
/**
30
48
* Associate challenge groups
31
49
* @param {Array<String> } toBeAdded the array of groups to be added
32
50
* @param {Array<String> } toBeDeleted the array of groups to be deleted
33
- * @param {String|Number } challengeId the legacy challenge ID
51
+ * @param {String|Number } legacyChallengeId the legacy challenge ID
34
52
*/
35
- async function associateChallengeGroups ( toBeAdded = [ ] , toBeDeleted = [ ] , challengeId ) {
53
+ async function associateChallengeGroups ( toBeAdded = [ ] , toBeDeleted = [ ] , legacyChallengeId ) {
36
54
for ( const group of toBeAdded ) {
37
- await groupService . addGroupToChallenge ( challengeId , group )
55
+ await groupService . addGroupToChallenge ( legacyChallengeId , group )
38
56
}
39
57
for ( const group of toBeDeleted ) {
40
- await groupService . removeGroupFromChallenge ( challengeId , group )
58
+ await groupService . removeGroupFromChallenge ( legacyChallengeId , group )
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Associate challenge terms
64
+ * @param {Array<Object{termsId, roleId}> } toBeAdded the array of terms to be added
65
+ * @param {Array<Object{termsId, roleId}> } toBeDeleted the array of terms to be deleted
66
+ * @param {String|Number } legacyChallengeId the legacy challenge ID
67
+ */
68
+ async function associateChallengeTerms ( v5Terms , legacyChallengeId ) {
69
+ const nda = _ . find ( v5Terms , e => e . id === config . V5_TERMS_NDA_ID )
70
+ const legacyTermsArray = await termsService . getTermsForChallenge ( legacyChallengeId )
71
+ const legacyNDA = _ . find ( legacyTermsArray , e => _ . toNumber ( e . id ) === _ . toNumber ( config . LEGACY_TERMS_NDA_ID ) )
72
+
73
+ // logger.debug(`V5 Terms ${JSON.stringify(v5Terms)}`)
74
+ // logger.debug(`V5 NDA Found ${nda} ${JSON.stringify(nda)}`)
75
+
76
+ // logger.debug(`Legacy Terms ${JSON.stringify(legacyTermsArray)}`)
77
+ // logger.debug(`Legacy NDA Found ${JSON.stringify(legacyNDA)}`)
78
+
79
+ if ( nda && nda . id && ! legacyNDA ) {
80
+ logger . debug ( 'Associate Challenge Terms - v5 NDA exist, not in legacy. Adding to Legacy.' )
81
+ const m2mToken = await helper . getM2MToken ( )
82
+ const v5Term = await getV5Terms ( nda . id , m2mToken )
83
+ return termsService . addTermsToChallenge ( legacyChallengeId , v5Term . legacyId , config . LEGACY_SUBMITTER_ROLE_ID )
41
84
}
85
+
86
+ if ( ! nda && legacyNDA && legacyNDA . id ) {
87
+ logger . debug ( 'Associate Challenge Terms - Legacy NDA exist, not in V5. Removing from Legacy.' )
88
+ return termsService . removeTermsFromChallenge ( legacyChallengeId , legacyNDA . id , config . LEGACY_SUBMITTER_ROLE_ID )
89
+ }
90
+
91
+ logger . debug ( 'Associate Challenge Terms - Nothing to Do' )
42
92
}
43
93
44
94
/**
45
95
* Set the copilot payment on legacy
46
96
* @param {Number|String } legacyChallengeId the legacy challenge ID
47
97
* @param {Array } prizeSets the prizeSets array
98
+ * @param {String } createdBy the created by handle
99
+ * @param {String } updatedBy the updated by handle
48
100
*/
49
- async function setCopilotPayment ( legacyChallengeId , prizeSets = [ ] ) {
101
+ async function setCopilotPayment ( legacyChallengeId , prizeSets = [ ] , createdBy , updatedBy ) {
50
102
try {
51
103
const copilotPayment = _ . get ( _ . find ( prizeSets , p => p . type === config . COPILOT_PAYMENT_TYPE ) , 'prizes[0].value' , null )
52
- await copilotPaymentService . setCopilotPayment ( legacyChallengeId , copilotPayment )
104
+ logger . debug ( `Setting Copilot Payment: ${ copilotPayment } for legacyId ${ legacyChallengeId } ` )
105
+ await copilotPaymentService . setCopilotPayment ( legacyChallengeId , copilotPayment , createdBy , updatedBy )
53
106
} catch ( e ) {
54
107
logger . error ( 'Failed to set the copilot payment!' )
55
108
logger . debug ( e )
@@ -137,9 +190,10 @@ async function getLegacyTrackInformation (trackId, typeId, tags, m2mToken) {
137
190
* @param {String } m2mToken the m2m token
138
191
* @param {Boolean } isCreated flag indicate the DTO is used in creating challenge
139
192
* @param {Array } informixGroupIds IDs from Informix associated with the group
193
+ * @param {Array<Object> } informixTermsIds IDs from Informix [{termsId, roleId}]
140
194
* @returns the DTO for saving a draft contest.(refer SaveDraftContestDTO in ap-challenge-microservice)
141
195
*/
142
- async function parsePayload ( payload , m2mToken , isCreated = true , informixGroupIds ) {
196
+ async function parsePayload ( payload , m2mToken , isCreated = true , informixGroupIds , informixTermsArray ) {
143
197
try {
144
198
let projectId
145
199
if ( _ . get ( payload , 'legacy.directProjectId' ) ) {
@@ -256,6 +310,40 @@ async function parsePayload (payload, m2mToken, isCreated = true, informixGroupI
256
310
} else if ( informixGroupIds && informixGroupIds . length > 0 ) {
257
311
data . groupsToBeDeleted = _ . map ( informixGroupIds , g => _ . toString ( g ) )
258
312
}
313
+
314
+ // if (payload.terms && _.get(payload, 'terms.length', 0) > 0) {
315
+ // const oldTerms = informixGroupIds
316
+ // const newTerms = []
317
+
318
+ // for (const v5TermsObject of payload.terms) {
319
+ // try {
320
+ // const termsInfo = await getV5Terms(v5TermsObject.id, m2mToken)
321
+ // if (!_.isEmpty(_.get(termsInfo, 'legacyId'))) {
322
+ // const roleInfo = await getV5Role(v5TermsObject.roleId, m2mToken)
323
+ // if (!_.isEmpty(_.get(roleInfo, 'legacyId'))) {
324
+ // newTerms.push({ id: _.get(termsInfo, 'legacyId'), roleId: _.get(roleInfo, 'legacyId') })
325
+ // }
326
+ // }
327
+ // } catch (e) {
328
+ // logger.warn(`Failed to load details for terms ${v5TermsObject}`)
329
+ // }
330
+ // }
331
+ // data.termsToBeAdded = _.difference(newTerms, oldTerms)
332
+ // data.termsToBeDeleted = _.difference(oldTerms, newTerms)
333
+ // if (data.termsToBeAdded.length > 0) {
334
+ // logger.debug(`parsePayload :: Adding Terms ${JSON.stringify(data.termsToBeAdded)}`)
335
+ // }
336
+ // if (data.termsToBeDeleted.length > 0) {
337
+ // logger.debug(`parsePayload :: Deleting Terms ${JSON.stringify(data.termsToBeDeleted)}`)
338
+ // }
339
+ // }
340
+ // // TODO Do not remove terms
341
+ // // } else if (informixTermsArray && informixTermsArray.length > 0) {
342
+ // // data.termsToBeDeleted = _.map(informixTermsArray, o => ({ id: o.id, roleId: o.roleId }))
343
+ // // }
344
+ // logger.debug(`parsePayload V5 Terms ${JSON.stringify(payload.terms)}`)
345
+ // logger.debug(`parsePayload legacy Terms ${JSON.stringify(informixTermsArray)}`)
346
+
259
347
return data
260
348
} catch ( err ) {
261
349
// Debugging
@@ -309,7 +397,8 @@ async function processCreate (message) {
309
397
const newChallenge = await helper . postRequest ( `${ config . V4_CHALLENGE_API_URL } ` , { param : _ . omit ( saveDraftContestDTO , [ 'groupsToBeAdded' , 'groupsToBeDeleted' ] ) } , m2mToken )
310
398
await helper . forceV4ESFeeder ( newChallenge . body . result . content . id )
311
399
await associateChallengeGroups ( saveDraftContestDTO . groupsToBeAdded , saveDraftContestDTO . groupsToBeDeleted , newChallenge . body . result . content . id )
312
- await setCopilotPayment ( newChallenge . body . result . content . id , _ . get ( message , 'payload.prizeSets' ) )
400
+ // await associateChallengeTerms(saveDraftContestDTO.termsToBeAdded, saveDraftContestDTO.termsToBeRemoved, newChallenge.body.result.content.id)
401
+ await setCopilotPayment ( newChallenge . body . result . content . id , _ . get ( message , 'payload.prizeSets' ) , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) )
313
402
await helper . patchRequest ( `${ config . V5_CHALLENGE_API_URL } /${ challengeUuid } ` , {
314
403
legacy : {
315
404
...message . payload . legacy ,
@@ -402,34 +491,37 @@ async function processUpdate (message) {
402
491
}
403
492
} catch ( e ) {
404
493
// postponne kafka event
405
- logger . info ( 'Challenge does not exist yet. Will post the same message back to the bus API' )
406
- logger . error ( `Error: ${ JSON . stringify ( e ) } ` )
407
-
408
- const retryCountIdentifier = `${ config . KAFKA_GROUP_ID . split ( ' ' ) . join ( '_' ) } _retry_count`
409
- let currentRetryCount = parseInt ( _ . get ( message . payload , retryCountIdentifier , 1 ) , 10 )
410
- if ( currentRetryCount <= config . MAX_RETRIES ) {
411
- await new Promise ( ( resolve ) => {
412
- setTimeout ( async ( ) => {
413
- currentRetryCount += 1
414
- await helper . postBusEvent ( config . UPDATE_CHALLENGE_TOPIC , { ...message . payload , [ retryCountIdentifier ] : currentRetryCount } )
415
- resolve ( )
416
- } , config . RETRY_TIMEOUT * currentRetryCount )
417
- } )
418
- } else {
419
- logger . error ( `Failed to process message after ${ config . MAX_RETRIES } retries. Aborting...` )
420
- }
421
- return
494
+ logger . warn ( `Error getting challenge by id, RETRY TURNED OFF ${ JSON . stringify ( e ) } ` )
495
+ // logger.info('Challenge does not exist yet. Will post the same message back to the bus API')
496
+ // logger.error(`Error: ${JSON.stringify(e)}`)
497
+
498
+ // const retryCountIdentifier = `${config.KAFKA_GROUP_ID.split(' ').join('_')}_retry_count`
499
+ // let currentRetryCount = parseInt(_.get(message.payload, retryCountIdentifier, 1), 10)
500
+ // if (currentRetryCount <= config.MAX_RETRIES) {
501
+ // await new Promise((resolve) => {
502
+ // setTimeout(async () => {
503
+ // currentRetryCount += 1
504
+ // await helper.postBusEvent(config.UPDATE_CHALLENGE_TOPIC, { ...message.payload, [retryCountIdentifier]: currentRetryCount })
505
+ // resolve()
506
+ // }, config.RETRY_TIMEOUT * currentRetryCount)
507
+ // })
508
+ // } else {
509
+ // logger.error(`Failed to process message after ${config.MAX_RETRIES} retries. Aborting...`)
510
+ // }
511
+ // return
422
512
}
423
513
424
514
const v4GroupIds = await groupService . getGroupsForChallenge ( message . payload . legacyId )
425
515
logger . info ( `GroupIDs Found in Informix: ${ JSON . stringify ( v4GroupIds ) } ` )
516
+ // const v4TermsIds = await termsService.getTermsForChallenge(message.payload.legacyId)
426
517
427
518
const saveDraftContestDTO = await parsePayload ( message . payload , m2mToken , false , v4GroupIds )
428
519
// logger.debug('Parsed Payload', saveDraftContestDTO)
429
520
try {
430
521
await helper . putRequest ( `${ config . V4_CHALLENGE_API_URL } /${ message . payload . legacyId } ` , { param : _ . omit ( saveDraftContestDTO , [ 'groupsToBeAdded' , 'groupsToBeDeleted' ] ) } , m2mToken )
431
522
await associateChallengeGroups ( saveDraftContestDTO . groupsToBeAdded , saveDraftContestDTO . groupsToBeDeleted , message . payload . legacyId )
432
- await setCopilotPayment ( message . payload . legacyId , _ . get ( message , 'payload.prizeSets' ) )
523
+ await associateChallengeTerms ( message . payload . terms , message . payload . legacyId )
524
+ await setCopilotPayment ( message . payload . legacyId , _ . get ( message , 'payload.prizeSets' ) , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) )
433
525
434
526
if ( message . payload . status ) {
435
527
// logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`)
@@ -439,9 +531,7 @@ async function processUpdate (message) {
439
531
logger . info ( 'Activated!' )
440
532
}
441
533
if ( message . payload . status === constants . challengeStatuses . Completed && challenge . currentStatus !== constants . challengeStatuses . Completed ) {
442
- const challengeUuid = message . payload . id
443
- const v5Challenge = await helper . getRequest ( `${ config . V5_CHALLENGE_API_URL } /${ challengeUuid } ` , m2mToken )
444
- if ( v5Challenge . body . task . isTask ) {
534
+ if ( message . payload . task . isTask ) {
445
535
logger . info ( 'Challenge is a TASK' )
446
536
if ( ! message . payload . winners || message . payload . winners . length === 0 ) {
447
537
throw new Error ( 'Cannot close challenge without winners' )
0 commit comments