@@ -46,14 +46,14 @@ const INSERT_PAYMENT_STATUS_REASON_XREF = 'INSERT INTO payment_detail_status_rea
46
46
* @param {String } sql the sql
47
47
* @return {Object } Informix statement
48
48
*/
49
- async function prepare ( connection , sql ) {
49
+ async function prepare ( connection , sql ) {
50
50
logger . debug ( `Preparing SQL ${ sql } ` )
51
51
const stmt = await connection . prepareAsync ( sql )
52
52
return Promise . promisifyAll ( stmt )
53
53
}
54
54
55
- async function paymentExists ( payment ) {
56
- const connection = await helper . getInformixConnection ( )
55
+ async function paymentExists ( payment , connection ) {
56
+ if ( ! connection ) connection = await helper . getInformixConnection ( )
57
57
try {
58
58
const query = util . format ( QUERY_PAYMENT , payment . memberId , payment . v5ChallengeId , payment . typeId )
59
59
logger . debug ( `Checking if paymentExists - ${ query } ` )
@@ -62,30 +62,38 @@ async function paymentExists(payment) {
62
62
logger . error ( `Error in 'paymentExists' ${ e } ` )
63
63
throw e
64
64
} finally {
65
- await connection . closeAsync ( )
65
+ if ( ! existingConnection ) await connection . closeAsync ( )
66
66
}
67
67
}
68
68
69
69
/**
70
70
* Create payment and save it to db
71
71
* @param {Object } payment the payment info
72
72
*/
73
- async function createPayment ( payment ) {
74
- const connection = await helper . getInformixConnection ( )
75
- const paymentDetailId = await paymentDetailIdGen . getNextId ( )
76
- const paymentId = await paymentIdGen . getNextId ( )
73
+ async function createPayment ( payment ) {
77
74
try {
75
+ const connection = await helper . getInformixConnection ( )
78
76
await connection . beginTransactionAsync ( )
79
- const insertDetail = await prepare ( connection , INSERT_PAYMENT_DETAIL )
80
- await insertDetail . executeAsync ( [ paymentDetailId , payment . amount , payment . amount , payment . statusId , payment . modificationRationaleId , payment . desc , payment . typeId , payment . methodId , payment . projectId , payment . charityInd , payment . amount , payment . installmentNumber , payment . createUser , payment . v5ChallengeId ] )
81
- const insertPayment = await prepare ( connection , INSERT_PAYMENT )
82
- await insertPayment . executeAsync ( [ paymentId , payment . memberId , paymentDetailId ] )
83
- const insertDetailXref = await prepare ( connection , INSERT_PAYMENT_DETAIL_XREF )
84
- await insertDetailXref . executeAsync ( [ paymentId , paymentDetailId ] )
85
- const insertStatusXref = await prepare ( connection , INSERT_PAYMENT_STATUS_REASON_XREF )
86
- await insertStatusXref . executeAsync ( [ paymentDetailId , config . V5_PAYMENT_DETAIL_STATUS_REASON_ID ] )
87
- logger . info ( `Payment ${ paymentId } with detail ${ paymentDetailId } has been inserted` )
88
- await connection . commitTransactionAsync ( )
77
+
78
+ const paymentExists = await paymentExists ( payment , connection )
79
+ logger . debug ( `Payment Exists Response: ${ JSON . stringify ( paymentExists ) } ` )
80
+ if ( ! paymentExists || paymentExists . length === 0 ) {
81
+ const paymentDetailId = await paymentDetailIdGen . getNextId ( )
82
+ const paymentId = await paymentIdGen . getNextId ( )
83
+ const insertDetail = await prepare ( connection , INSERT_PAYMENT_DETAIL )
84
+ await insertDetail . executeAsync ( [ paymentDetailId , payment . amount , payment . amount , payment . statusId , payment . modificationRationaleId , payment . desc , payment . typeId , payment . methodId , payment . projectId , payment . charityInd , payment . amount , payment . installmentNumber , payment . createUser , payment . v5ChallengeId ] )
85
+ const insertPayment = await prepare ( connection , INSERT_PAYMENT )
86
+ await insertPayment . executeAsync ( [ paymentId , payment . memberId , paymentDetailId ] )
87
+ const insertDetailXref = await prepare ( connection , INSERT_PAYMENT_DETAIL_XREF )
88
+ await insertDetailXref . executeAsync ( [ paymentId , paymentDetailId ] )
89
+ const insertStatusXref = await prepare ( connection , INSERT_PAYMENT_STATUS_REASON_XREF )
90
+ await insertStatusXref . executeAsync ( [ paymentDetailId , config . V5_PAYMENT_DETAIL_STATUS_REASON_ID ] )
91
+ logger . info ( `Payment ${ paymentId } with detail ${ paymentDetailId } has been inserted` )
92
+ await connection . commitTransactionAsync ( )
93
+ } else {
94
+ logger . error ( `Payment Exists for ${ payment . v5ChallengeId } , skipping - ${ JSON . stringify ( paymentExists ) } ` )
95
+ await connection . commitTransactionAsync ( )
96
+ }
89
97
} catch ( e ) {
90
98
logger . error ( `Error in 'createPayment' ${ e } , rolling back transaction` )
91
99
await connection . rollbackTransactionAsync ( )
0 commit comments