@@ -13,6 +13,71 @@ const constants = require('../constants')
1313const groupService = require ( './groupsService' )
1414const termsService = require ( './termsService' )
1515const copilotPaymentService = require ( './copilotPaymentService' )
16+ const timelineService = require ( './timelineService' )
17+ const paymentService = require ( './paymentService' )
18+
19+ /**
20+ * Sync the information from the v5 phases into legacy
21+ * @param {Number } legacyId the legacy challenge ID
22+ * @param {Array } v4Phases the v4 phases
23+ * @param {Array } v5Phases the v5 phases
24+ */
25+ async function syncChallengePhases ( legacyId , v5Phases ) {
26+ const phaseTypes = await timelineService . getPhaseTypes ( )
27+ const phasesFromIFx = await timelineService . getChallengePhases ( legacyId )
28+ for ( const phase of phasesFromIFx ) {
29+ const phaseName = _ . get ( _ . find ( phaseTypes , pt => pt . phase_type_id === phase . phase_type_id ) , 'name' )
30+ const v5Equivalent = _ . find ( v5Phases , p => p . name === phaseName )
31+ if ( v5Equivalent ) {
32+ // Compare duration and status
33+ if ( v5Equivalent . duration * 1000 !== phase . duration ||
34+ ( v5Equivalent . isOpen && _ . toInteger ( phase . phase_status_id ) === constants . PhaseStatusTypes . Closed ) ||
35+ ( ! v5Equivalent . isOpen && _ . toInteger ( phase . phase_status_id ) === constants . PhaseStatusTypes . Open ) ) {
36+ // update phase
37+ await timelineService . updatePhase (
38+ phase . project_phase_id ,
39+ legacyId ,
40+ v5Equivalent . scheduledStartDate ,
41+ v5Equivalent . scheduledEndDate ,
42+ v5Equivalent . duration * 1000 ,
43+ v5Equivalent . isOpen ? constants . PhaseStatusTypes . Open : constants . PhaseStatusTypes . Closed )
44+ }
45+ }
46+ }
47+ }
48+
49+ /**
50+ * Update the payments from v5 prize sets into legacy
51+ * @param {Array } v4Prizes the v4 prizes
52+ * @param {Number } v4NumOfCheckpointPrizes the number of checkpoints (v4)
53+ * @param {Number } v4CheckpointPrize the dollar ammount of each checkpoint prize (v4)
54+ * @param {Array } v5PrizeSets the v5 prize sets
55+ */
56+ async function updateMemberPayments ( legacyId , v5PrizeSets ) {
57+ const prizesFromIfx = await paymentService . getChallengePrizes ( legacyId , constants . prizeTypesIds . Contest )
58+ const [ checkpointPrizesFromIfx ] = await paymentService . getChallengePrizes ( legacyId , constants . prizeTypesIds . Checkpoint )
59+ const v5Prizes = _ . map ( _ . get ( _ . find ( v5PrizeSets , p => p . type === constants . prizeSetTypes . ChallengePrizes ) , 'prizes' , [ ] ) , prize => prize . value )
60+ const v5CheckPointPrizes = _ . map ( _ . get ( _ . find ( v5PrizeSets , p => p . type === constants . prizeSetTypes . CheckPoint ) , 'prizes' , [ ] ) , prize => prize . value )
61+ // compare prizes
62+ if ( v5Prizes && v5Prizes . length > 0 ) {
63+ v5Prizes . sort ( ( a , b ) => b - a )
64+ for ( let i = 0 ; i < v5Prizes . length ; i += 1 ) {
65+ const ifxPrize = _ . find ( prizesFromIfx , p => p . place === i + 1 )
66+ if ( ifxPrize ) {
67+ if ( _ . toInteger ( ifxPrize . prize_amount ) !== v5Prizes [ i ] ) {
68+ await paymentService . updatePrize ( ifxPrize . prize_id , legacyId , v5Prizes [ i ] , 1 )
69+ }
70+ }
71+ }
72+ }
73+ // compare checkpoint prizes
74+ if ( v5CheckPointPrizes && v5CheckPointPrizes . length > 0 ) {
75+ // we assume that all checkpoint prizes will be the same
76+ if ( v5CheckPointPrizes . length !== checkpointPrizesFromIfx . number_of_submissions || v5CheckPointPrizes [ 0 ] !== _ . toInteger ( checkpointPrizesFromIfx . prize_amount ) ) {
77+ await paymentService . updatePrize ( checkpointPrizesFromIfx . prize_id , legacyId , v5CheckPointPrizes [ 0 ] , v5CheckPointPrizes . length )
78+ }
79+ }
80+ }
1681
1782/**
1883 * Get group information by V5 UUID
@@ -444,8 +509,8 @@ async function processUpdate (message) {
444509 logger . debug ( `Will skip creating on legacy as status is ${ constants . challengeStatuses . New } ` )
445510 return
446511 } else if ( ! message . payload . legacyId ) {
447- logger . debug ( 'Legacy ID does not exist. Will create...' )
448- return processCreate ( message )
512+ logger . debug ( 'Legacy ID does not exist. Will create the challenge first using the V4 API ...' )
513+ await processCreate ( message )
449514 }
450515 const m2mToken = await helper . getM2MToken ( )
451516
@@ -485,11 +550,12 @@ async function processUpdate (message) {
485550 const saveDraftContestDTO = await parsePayload ( message . payload , m2mToken , false , v4GroupIds )
486551 // logger.debug('Parsed Payload', saveDraftContestDTO)
487552 try {
488- await helper . putRequest ( ` ${ config . V4_CHALLENGE_API_URL } / ${ message . payload . legacyId } ` , { param : _ . omit ( saveDraftContestDTO , [ 'groupsToBeAdded' , 'groupsToBeDeleted' ] ) } , m2mToken )
489- await associateChallengeGroups ( saveDraftContestDTO . groupsToBeAdded , saveDraftContestDTO . groupsToBeDeleted , message . payload . legacyId )
490- await associateChallengeTerms ( message . payload . terms , message . payload . legacyId )
491- await setCopilotPayment ( message . payload . legacyId , _ . get ( message , 'payload.prizeSets' ) , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) )
553+ if ( challenge ) {
554+ // Only make the PUT request to the API if the challenge was available on the V4 API otherwise this will throw an error
555+ await helper . putRequest ( ` ${ config . V4_CHALLENGE_API_URL } / ${ message . payload . legacyId } ` , { param : _ . omit ( saveDraftContestDTO , [ 'groupsToBeAdded' , 'groupsToBeDeleted' ] ) } , m2mToken )
556+ }
492557
558+ // Handle activating challenges and closing out tasks
493559 if ( message . payload . status ) {
494560 // logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`)
495561 if ( message . payload . status === constants . challengeStatuses . Active && challenge . currentStatus !== constants . challengeStatuses . Active ) {
@@ -511,6 +577,15 @@ async function processUpdate (message) {
511577 }
512578 }
513579 }
580+
581+ // Direct IFX modifications
582+ await syncChallengePhases ( message . payload . legacyId , message . payload . phases )
583+ await updateMemberPayments ( message . payload . legacyId , message . payload . prizeSets )
584+ await associateChallengeGroups ( saveDraftContestDTO . groupsToBeAdded , saveDraftContestDTO . groupsToBeDeleted , message . payload . legacyId )
585+ await associateChallengeTerms ( message . payload . terms , message . payload . legacyId )
586+ await setCopilotPayment ( message . payload . legacyId , _ . get ( message , 'payload.prizeSets' ) , _ . get ( message , 'payload.createdBy' ) , _ . get ( message , 'payload.updatedBy' ) )
587+
588+ // Force the V4 ES feeder
514589 await helper . forceV4ESFeeder ( message . payload . legacyId )
515590 } catch ( e ) {
516591 logger . error ( 'processUpdate Catch' , e )
0 commit comments