@@ -19,8 +19,9 @@ async function processUpdate(message) {
19
19
const legacyId = _ . get ( message , 'payload.legacyId' , null )
20
20
const v5ChallengeId = _ . get ( message , 'payload.id' , null )
21
21
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
24
25
}
25
26
//const grossAmount = _.sumBy(_.flatMap(message.payload.prizeSets, 'prizes'), 'value')
26
27
@@ -29,7 +30,7 @@ async function processUpdate(message) {
29
30
statusId : config . PAYMENT_STATUS_ID ,
30
31
modificationRationaleId : config . MODIFICATION_RATIONALE_ID ,
31
32
methodId : config . PAYMENT_METHOD_ID ,
32
- projectId : legacyId ,
33
+ projectId : legacyId , // this is not projectId from v5 - legacy calls a challenge a project
33
34
charityInd : config . CHARITY_IND ,
34
35
installmentNumber : config . INSTALLMENT_NUMBER ,
35
36
createUser : createUserId ,
@@ -42,21 +43,29 @@ async function processUpdate(message) {
42
43
// const winnerPaymentDesc = _.get(_.find(message.payload.prizeSets, ['type', 'placement']), 'description', '')
43
44
const winnerMembers = _ . sortBy ( _ . get ( message . payload , 'winners' , [ ] ) , [ 'placement' ] )
44
45
if ( _ . isEmpty ( winnerPrizes ) ) {
45
- logger . warn ( `For challenge ${ legacyId } , no winner payment avaiable` )
46
+ logger . warn ( `For challenge ${ v5ChallengeId } , no winner payment avaiable` )
46
47
} 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` )
48
49
} else {
49
50
try {
50
51
for ( let i = 1 ; i <= winnerPrizes . length ; i ++ ) {
51
- await paymentService . createPayment ( _ . assign ( {
52
+ const payment = _ . assign ( {
52
53
memberId : winnerMembers [ i - 1 ] . userId ,
53
54
amount : winnerPrizes [ i - 1 ] . value ,
54
55
desc : `Task - ${ message . payload . name } - ${ i } Place` ,
55
56
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
+ }
57
66
}
58
67
} catch ( error ) {
59
- logger . error ( `For challenge ${ legacyId } , add winner payments error: ${ error } ` )
68
+ logger . error ( `For challenge ${ v5ChallengeId } , add winner payments error: ${ error } ` )
60
69
}
61
70
}
62
71
@@ -66,9 +75,9 @@ async function processUpdate(message) {
66
75
const copilotPaymentDesc = _ . get ( _ . find ( message . payload . prizeSets , [ 'type' , 'copilot' ] ) , 'description' , '' )
67
76
68
77
if ( ! copilotAmount ) {
69
- logger . warn ( `For challenge ${ legacyId } , no copilot payment avaiable ` )
78
+ logger . warn ( `For challenge ${ v5ChallengeId } , no copilot payment available ` )
70
79
} else if ( ! copilotId ) {
71
- logger . warn ( `For challenge ${ legacyId } , no copilot memberId avaiable ` )
80
+ logger . warn ( `For challenge ${ v5ChallengeId } , no copilot memberId available ` )
72
81
} else {
73
82
try {
74
83
const copilotPayment = _ . assign ( {
@@ -77,13 +86,19 @@ async function processUpdate(message) {
77
86
desc : ( copilotPaymentDesc ? copilotPaymentDesc : `Task - ${ message . payload . name } - Copilot` ) ,
78
87
typeId : config . COPILOT_PAYMENT_TYPE_ID
79
88
} , 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
+ }
81
96
} catch ( error ) {
82
- logger . error ( `For challenge ${ legacyId } , add copilot payments error: ${ error } ` )
97
+ logger . error ( `For challenge ${ v5ChallengeId } , add copilot payments error: ${ error } ` )
83
98
}
84
99
}
85
100
} 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 } ` )
87
102
}
88
103
}
89
104
0 commit comments