@@ -19,8 +19,9 @@ async function processUpdate(message) {
1919 const legacyId = _ . get ( message , 'payload.legacyId' , null )
2020 const v5ChallengeId = _ . get ( message , 'payload.id' , null )
2121
22- if ( ! legacyId ) {
23- logger . warn ( `payload of challenge ${ v5ChallengeId } does not contain a legacy id` )
22+ if ( ! v5ChallengeId ) {
23+ logger . error ( 'Payload of challenge does not contain a v5 Challenge UUID' )
24+ return false
2425 }
2526 //const grossAmount = _.sumBy(_.flatMap(message.payload.prizeSets, 'prizes'), 'value')
2627
@@ -29,7 +30,7 @@ async function processUpdate(message) {
2930 statusId : config . PAYMENT_STATUS_ID ,
3031 modificationRationaleId : config . MODIFICATION_RATIONALE_ID ,
3132 methodId : config . PAYMENT_METHOD_ID ,
32- projectId : legacyId ,
33+ projectId : legacyId , // this is not projectId from v5 - legacy calls a challenge a project
3334 charityInd : config . CHARITY_IND ,
3435 installmentNumber : config . INSTALLMENT_NUMBER ,
3536 createUser : createUserId ,
@@ -42,21 +43,29 @@ async function processUpdate(message) {
4243 // const winnerPaymentDesc = _.get(_.find(message.payload.prizeSets, ['type', 'placement']), 'description', '')
4344 const winnerMembers = _ . sortBy ( _ . get ( message . payload , 'winners' , [ ] ) , [ 'placement' ] )
4445 if ( _ . isEmpty ( winnerPrizes ) ) {
45- logger . warn ( `For challenge ${ legacyId } , no winner payment avaiable` )
46+ logger . warn ( `For challenge ${ v5ChallengeId } , no winner payment avaiable` )
4647 } else if ( winnerPrizes . length !== winnerMembers . length ) {
47- logger . error ( `For challenge ${ legacyId } , there is ${ winnerPrizes . length } user prizes but ${ winnerMembers . length } winners` )
48+ logger . error ( `For challenge ${ v5ChallengeId } , there is ${ winnerPrizes . length } user prizes but ${ winnerMembers . length } winners` )
4849 } else {
4950 try {
5051 for ( let i = 1 ; i <= winnerPrizes . length ; i ++ ) {
51- await paymentService . createPayment ( _ . assign ( {
52+ const payment = _ . assign ( {
5253 memberId : winnerMembers [ i - 1 ] . userId ,
5354 amount : winnerPrizes [ i - 1 ] . value ,
5455 desc : `Task - ${ message . payload . name } - ${ i } Place` ,
5556 typeId : config . WINNER_PAYMENT_TYPE_ID
56- } , basePayment ) )
57+ } , basePayment )
58+
59+ const paymentExists = await paymentService . paymentExists ( payment )
60+ logger . debug ( `Payment Exists Response: ${ JSON . stringify ( paymentExists ) } ` )
61+ if ( ! paymentExists || paymentExists . length === 0 ) {
62+ await paymentService . createPayment ( payment )
63+ } else {
64+ logger . error ( `Payment Exists for ${ v5ChallengeId } , skipping - ${ JSON . stringify ( paymentExists ) } ` )
65+ }
5766 }
5867 } catch ( error ) {
59- logger . error ( `For challenge ${ legacyId } , add winner payments error: ${ error } ` )
68+ logger . error ( `For challenge ${ v5ChallengeId } , add winner payments error: ${ error } ` )
6069 }
6170 }
6271
@@ -66,9 +75,9 @@ async function processUpdate(message) {
6675 const copilotPaymentDesc = _ . get ( _ . find ( message . payload . prizeSets , [ 'type' , 'copilot' ] ) , 'description' , '' )
6776
6877 if ( ! copilotAmount ) {
69- logger . warn ( `For challenge ${ legacyId } , no copilot payment avaiable ` )
78+ logger . warn ( `For challenge ${ v5ChallengeId } , no copilot payment available ` )
7079 } else if ( ! copilotId ) {
71- logger . warn ( `For challenge ${ legacyId } , no copilot memberId avaiable ` )
80+ logger . warn ( `For challenge ${ v5ChallengeId } , no copilot memberId available ` )
7281 } else {
7382 try {
7483 const copilotPayment = _ . assign ( {
@@ -77,13 +86,19 @@ async function processUpdate(message) {
7786 desc : ( copilotPaymentDesc ? copilotPaymentDesc : `Task - ${ message . payload . name } - Copilot` ) ,
7887 typeId : config . COPILOT_PAYMENT_TYPE_ID
7988 } , basePayment )
80- await paymentService . createPayment ( copilotPayment )
89+ const paymentExists = await paymentService . paymentExists ( copilotPayment )
90+ logger . debug ( `Copilot Payment Exists Response: ${ JSON . stringify ( paymentExists ) } ` )
91+ if ( ! paymentExists || paymentExists . length === 0 ) {
92+ await paymentService . createPayment ( copilotPayment )
93+ } else {
94+ logger . error ( `Copilot Payment Exists for ${ v5ChallengeId } , skipping - ${ JSON . stringify ( paymentExists ) } ` )
95+ }
8196 } catch ( error ) {
82- logger . error ( `For challenge ${ legacyId } , add copilot payments error: ${ error } ` )
97+ logger . error ( `For challenge ${ v5ChallengeId } , add copilot payments error: ${ error } ` )
8398 }
8499 }
85100 } catch ( error ) {
86- logger . error ( `For challenge ${ legacyId } , error occurred while parsing and preparing payment detail. Error: ${ error } ` )
101+ logger . error ( `For challenge ${ v5ChallengeId } , error occurred while parsing and preparing payment detail. Error: ${ error } ` )
87102 }
88103}
89104
0 commit comments