@@ -14,6 +14,7 @@ const groupService = require('./groupsService')
14
14
const termsService = require ( './termsService' )
15
15
const copilotPaymentService = require ( './copilotPaymentService' )
16
16
const timelineService = require ( './timelineService' )
17
+ const metadataService = require ( './metadataService' )
17
18
18
19
/**
19
20
* Get group information by V5 UUID
@@ -107,16 +108,27 @@ async function associateChallengeTerms (v5Terms, legacyChallengeId, createdBy, u
107
108
108
109
/**
109
110
* Set the copilot payment on legacy
111
+ * @param {String } challengeId the V5 challenge ID
110
112
* @param {Number|String } legacyChallengeId the legacy challenge ID
111
113
* @param {Array } prizeSets the prizeSets array
112
114
* @param {String } createdBy the created by handle
113
115
* @param {String } updatedBy the updated by handle
116
+ * @param {String } m2mToken the m2m token
114
117
*/
115
- async function setCopilotPayment ( legacyChallengeId , prizeSets = [ ] , createdBy , updatedBy ) {
118
+ async function setCopilotPayment ( challengeId , legacyChallengeId , prizeSets = [ ] , createdBy , updatedBy , m2mToken ) {
116
119
try {
117
120
const copilotPayment = _ . get ( _ . find ( prizeSets , p => p . type === config . COPILOT_PAYMENT_TYPE ) , 'prizes[0].value' , null )
118
- logger . debug ( `Setting Copilot Payment: ${ copilotPayment } for legacyId ${ legacyChallengeId } ` )
119
- await copilotPaymentService . setCopilotPayment ( legacyChallengeId , copilotPayment , createdBy , updatedBy )
121
+ if ( copilotPayment ) {
122
+ logger . debug ( 'Fetching challenge copilot...' )
123
+ const res = await helper . getRequest ( `${ config . V5_RESOURCES_API_URL } ?challengeId=${ challengeId } &roleId=${ config . COPILOT_ROLE_ID } ` , m2mToken )
124
+ const [ copilotResource ] = res . body
125
+ if ( ! copilotResource ) {
126
+ logger . warn ( `Copilot does not exist for challenge ${ challengeId } (legacy: ${ legacyChallengeId } )` )
127
+ return
128
+ }
129
+ logger . debug ( `Setting Copilot Payment: ${ copilotPayment } for legacyId ${ legacyChallengeId } for copilot ${ copilotResource . memberId } ` )
130
+ await copilotPaymentService . setCopilotPayment ( legacyChallengeId , copilotPayment , createdBy , updatedBy )
131
+ }
120
132
} catch ( e ) {
121
133
logger . error ( 'Failed to set the copilot payment!' )
122
134
logger . debug ( e )
@@ -325,6 +337,21 @@ async function parsePayload (payload, m2mToken, isCreated = true, informixGroupI
325
337
data . groupsToBeDeleted = _ . map ( informixGroupIds , g => _ . toString ( g ) )
326
338
}
327
339
340
+ if ( payload . metadata && payload . metadata . length > 0 ) {
341
+ const fileTypes = _ . find ( payload . metadata , meta => meta . name === 'fileTypes' )
342
+ if ( fileTypes ) {
343
+ if ( _ . isArray ( fileTypes . value ) ) {
344
+ data . fileTypes = fileTypes . value
345
+ } else {
346
+ try {
347
+ data . fileTypes = JSON . parse ( fileTypes . value )
348
+ } catch ( e ) {
349
+ data . fileTypes = [ ]
350
+ }
351
+ }
352
+ }
353
+ }
354
+
328
355
return data
329
356
} catch ( err ) {
330
357
// Debugging
@@ -385,7 +412,7 @@ async function processCreate (message) {
385
412
await helper . forceV4ESFeeder ( newChallenge . body . result . content . id )
386
413
await associateChallengeGroups ( saveDraftContestDTO . groupsToBeAdded , saveDraftContestDTO . groupsToBeDeleted , newChallenge . body . result . content . id )
387
414
// await associateChallengeTerms(saveDraftContestDTO.termsToBeAdded, saveDraftContestDTO.termsToBeRemoved, newChallenge.body.result.content.id)
388
- await setCopilotPayment ( newChallenge . body . result . content . id , _ . get ( message , 'payload.prizeSets' ) , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) )
415
+ await setCopilotPayment ( challengeUuid , newChallenge . body . result . content . id , _ . get ( message , 'payload.prizeSets' ) , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) , m2mToken )
389
416
await helper . patchRequest ( `${ config . V5_CHALLENGE_API_URL } /${ challengeUuid } ` , {
390
417
legacy : {
391
418
...message . payload . legacy ,
@@ -466,7 +493,7 @@ async function processUpdate (message) {
466
493
return
467
494
} else if ( ! message . payload . legacyId ) {
468
495
logger . debug ( 'Legacy ID does not exist. Will create...' )
469
- return processCreate ( message )
496
+ await processCreate ( message )
470
497
}
471
498
const m2mToken = await helper . getM2MToken ( )
472
499
@@ -506,12 +533,45 @@ async function processUpdate (message) {
506
533
const saveDraftContestDTO = await parsePayload ( message . payload , m2mToken , false , v4GroupIds )
507
534
// logger.debug('Parsed Payload', saveDraftContestDTO)
508
535
try {
509
- await helper . putRequest ( `${ config . V4_CHALLENGE_API_URL } /${ message . payload . legacyId } ` , { param : _ . omit ( saveDraftContestDTO , [ 'groupsToBeAdded' , 'groupsToBeDeleted' ] ) } , m2mToken )
536
+ try {
537
+ if ( challenge ) {
538
+ await helper . putRequest ( `${ config . V4_CHALLENGE_API_URL } /${ message . payload . legacyId } ` , { param : _ . omit ( saveDraftContestDTO , [ 'groupsToBeAdded' , 'groupsToBeDeleted' ] ) } , m2mToken )
539
+ }
540
+ } catch ( e ) {
541
+ logger . warn ( 'Failed to update the challenge via the V4 API' )
542
+ logger . error ( e )
543
+ }
510
544
await associateChallengeGroups ( saveDraftContestDTO . groupsToBeAdded , saveDraftContestDTO . groupsToBeDeleted , message . payload . legacyId )
511
- await associateChallengeTerms ( message . payload . terms , message . payload . legacyId , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) )
512
- await setCopilotPayment ( message . payload . legacyId , _ . get ( message , 'payload.prizeSets' ) , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) )
513
-
514
- if ( message . payload . status ) {
545
+ await associateChallengeTerms ( message . payload . terms , message . payload . legacyId , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) || _ . get ( message , 'payload.createdBy' ) )
546
+ await setCopilotPayment ( message . payload . id , message . payload . legacyId , _ . get ( message , 'payload.prizeSets' ) , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) || _ . get ( message , 'payload.createdBy' ) , m2mToken )
547
+
548
+ // Update metadata in IFX
549
+ if ( message . payload . metadata && message . payload . metadata . length > 0 ) {
550
+ for ( const metadataKey of _ . keys ( constants . supportedMetadata ) ) {
551
+ const entry = _ . find ( message . payload . metadata , meta => meta . name === metadataKey )
552
+ if ( entry ) {
553
+ if ( metadataKey === 'submissionLimit' ) {
554
+ // data here is JSON stringified
555
+ try {
556
+ const parsedEntryValue = JSON . parse ( entry . value )
557
+ if ( parsedEntryValue . limit ) {
558
+ entry . value = parsedEntryValue . count
559
+ } else {
560
+ entry . value = null
561
+ }
562
+ } catch ( e ) {
563
+ entry . value = null
564
+ }
565
+ }
566
+ try {
567
+ await metadataService . createOrUpdateMetadata ( message . payload . legacyId , constants . supportedMetadata [ metadataKey ] , entry . value , _ . get ( message , 'payload.updatedBy' ) || _ . get ( message , 'payload.createdBy' ) )
568
+ } catch ( e ) {
569
+ logger . warn ( `Failed to set ${ metadataKey } (${ constants . supportedMetadata [ metadataKey ] } )` )
570
+ }
571
+ }
572
+ }
573
+ }
574
+ if ( message . payload . status && challenge ) {
515
575
// logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`)
516
576
if ( message . payload . status === constants . challengeStatuses . Active && challenge . currentStatus !== constants . challengeStatuses . Active ) {
517
577
logger . info ( 'Activating challenge...' )
@@ -532,7 +592,11 @@ async function processUpdate (message) {
532
592
}
533
593
}
534
594
}
535
- await helper . forceV4ESFeeder ( message . payload . legacyId )
595
+ try {
596
+ await helper . forceV4ESFeeder ( message . payload . legacyId )
597
+ } catch ( e ) {
598
+ logger . warn ( 'Failed to call V4 ES Feeder' )
599
+ }
536
600
} catch ( e ) {
537
601
logger . error ( 'processUpdate Catch' , e )
538
602
throw e
0 commit comments