Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit e3000ad

Browse files
Fix issue with double payments
1 parent 681233c commit e3000ad

File tree

4 files changed

+58
-40
lines changed

4 files changed

+58
-40
lines changed

src/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ const dataHandler = (messageSet, topic, partition) => Promise.each(messageSet, a
6060
}
6161

6262
try {
63+
// delay for a random amount of time between 5-20 sec
64+
// to minimize the chance of having two processes doing the same at the same time
65+
await helper.delay(helper.getRandomInt(5 * 1000, 20 * 1000))
6366
await processorService.processUpdate(messageJSON)
6467
logger.debug('Successfully processed message')
6568
} catch (err) {

src/common/helper.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,29 @@ async function getCopilotId (challengeId) {
146146
return _.get(_.head(res.body), 'memberId')
147147
}
148148

149+
/**
150+
* Creates a delay promise
151+
* @param {Number} ms the milliseconds to delay
152+
* @returns a promise
153+
*/
154+
function delay (ms) {
155+
return new Promise(resolve => {
156+
setTimeout(resolve, ms)
157+
})
158+
}
159+
160+
/**
161+
* Generate a random integer
162+
* @param {Number} min the min
163+
* @param {Number} max the max
164+
* @returns the random integer
165+
*/
166+
function getRandomInt(min, max) {
167+
min = Math.ceil(min)
168+
max = Math.floor(max)
169+
return Math.floor(Math.random() * (max - min + 1)) + min
170+
}
171+
149172
module.exports = {
150173
getInformixConnection,
151174
getKafkaOptions,
@@ -154,5 +177,7 @@ module.exports = {
154177
putRequest,
155178
postRequest,
156179
getUserId,
157-
getCopilotId
180+
getCopilotId,
181+
delay,
182+
getRandomInt
158183
}

src/services/paymentService.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ const INSERT_PAYMENT_STATUS_REASON_XREF = 'INSERT INTO payment_detail_status_rea
4646
* @param {String} sql the sql
4747
* @return {Object} Informix statement
4848
*/
49-
async function prepare (connection, sql) {
49+
async function prepare(connection, sql) {
5050
logger.debug(`Preparing SQL ${sql}`)
5151
const stmt = await connection.prepareAsync(sql)
5252
return Promise.promisifyAll(stmt)
5353
}
5454

55-
async function paymentExists(payment) {
56-
const connection = await helper.getInformixConnection()
55+
async function paymentExists(payment, connection) {
56+
if (!connection) connection = await helper.getInformixConnection()
5757
try {
5858
const query = util.format(QUERY_PAYMENT, payment.memberId, payment.v5ChallengeId, payment.typeId)
5959
logger.debug(`Checking if paymentExists - ${query}`)
@@ -62,30 +62,38 @@ async function paymentExists(payment) {
6262
logger.error(`Error in 'paymentExists' ${e}`)
6363
throw e
6464
} finally {
65-
await connection.closeAsync()
65+
if (!existingConnection) await connection.closeAsync()
6666
}
6767
}
6868

6969
/**
7070
* Create payment and save it to db
7171
* @param {Object} payment the payment info
7272
*/
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) {
7774
try {
75+
const connection = await helper.getInformixConnection()
7876
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+
}
8997
} catch (e) {
9098
logger.error(`Error in 'createPayment' ${e}, rolling back transaction`)
9199
await connection.rollbackTransactionAsync()

src/services/processorService.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ async function processUpdate(message) {
5959
typeId: config.WINNER_PAYMENT_TYPE_ID
6060
}, basePayment)
6161

62-
const paymentExists = await paymentService.paymentExists(payment)
63-
logger.debug(`Payment Exists Response: ${JSON.stringify(paymentExists)}`)
64-
if(!paymentExists || paymentExists.length === 0) {
65-
await paymentService.createPayment(payment)
66-
} else {
67-
logger.error(`Payment Exists for ${v5ChallengeId}, skipping - ${JSON.stringify(paymentExists)}`)
68-
}
62+
await paymentService.createPayment(payment)
6963
}
7064
} catch (error) {
7165
logger.error(`For challenge ${v5ChallengeId}, add winner payments error: ${error}`)
@@ -86,13 +80,7 @@ async function processUpdate(message) {
8680
typeId: config.CHECKPOINT_WINNER_PAYMENT_TYPE_ID
8781
}, basePayment)
8882

89-
const paymentExists = await paymentService.paymentExists(payment)
90-
logger.debug(`Payment Exists Response: ${JSON.stringify(paymentExists)}`)
91-
if(!paymentExists || paymentExists.length === 0) {
92-
await paymentService.createPayment(payment)
93-
} else {
94-
logger.error(`Payment Exists for ${v5ChallengeId}, skipping - ${JSON.stringify(paymentExists)}`)
95-
}
83+
await paymentService.createPayment(payment)
9684
}
9785
} catch (error) {
9886
logger.error(`For challenge ${v5ChallengeId}, add checkpoint winner payments error: ${error}`)
@@ -116,13 +104,7 @@ async function processUpdate(message) {
116104
desc: (copilotPaymentDesc ? copilotPaymentDesc : `${message.payload.name} - Copilot`),
117105
typeId: config.COPILOT_PAYMENT_TYPE_ID
118106
}, basePayment)
119-
const paymentExists = await paymentService.paymentExists(copilotPayment)
120-
logger.debug(`Copilot Payment Exists Response: ${JSON.stringify(paymentExists)}`)
121-
if(!paymentExists || paymentExists.length === 0) {
122-
await paymentService.createPayment(copilotPayment)
123-
} else {
124-
logger.error(`Copilot Payment Exists for ${v5ChallengeId}, skipping - ${JSON.stringify(paymentExists)}`)
125-
}
107+
await paymentService.createPayment(copilotPayment)
126108
} catch (error) {
127109
logger.error(`For challenge ${v5ChallengeId}, add copilot payments error: ${error}`)
128110
}

0 commit comments

Comments
 (0)