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

Commit d8e5cd8

Browse files
author
James Cori
committed
Merge branch 'develop'
2 parents 66fca05 + 6340b5e commit d8e5cd8

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

src/services/paymentService.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Payment Service
33
* Interacts with InformixDB
44
*/
5+
const util = require('util')
56
const logger = require('../common/logger')
67
const helper = require('../common/helper')
78
const config = require('config');
@@ -12,11 +13,25 @@ const paymentDetailIdGen = new IDGenerator('PAYMENT_DETAIL_SEQ')
1213
// the paymentId's generator
1314
const paymentIdGen = new IDGenerator('PAYMENT_SEQ')
1415

16+
// TODO - NOT checking on pd.net_amount = %d for now because you could edit an amount it'd create another payment.
17+
const QUERY_PAYMENT = `SELECT
18+
p.payment_id, pd.payment_detail_id, pd.payment_desc, pd.payment_type_id, pd.payment_method_id,
19+
p.create_date, pd.create_date as modify_date, pd.net_amount,
20+
pd.payment_status_id, p.user_id,
21+
pd.date_modified, pd.date_paid, pd.gross_amount,
22+
pd.parent_payment_id, pd.total_amount, pd.installment_number, pd.jira_issue_id
23+
FROM payment p
24+
INNER JOIN payment_detail pd ON pd.payment_detail_id = p.most_recent_detail_id
25+
WHERE
26+
p.user_id = %d
27+
AND pd.jira_issue_id = '%s'
28+
AND pd.payment_type_id = %d
29+
`
1530
// the insert statement of payment detail
1631
const INSERT_PAYMENT_DETAIL = `INSERT INTO payment_detail (
17-
payment_detail_id, net_amount, gross_amount, payment_status_id, modification_rationale_id,
18-
payment_desc, payment_type_id, date_modified, date_due, payment_method_id, component_project_id,
19-
create_date, charity_ind, total_amount, installment_number, create_user,
32+
payment_detail_id, net_amount, gross_amount, payment_status_id, modification_rationale_id,
33+
payment_desc, payment_type_id, date_modified, date_due, payment_method_id, component_project_id,
34+
create_date, charity_ind, total_amount, installment_number, create_user,
2035
jira_issue_id) VALUES(?,?,?,?,?,?,?, CURRENT, EXTEND(CURRENT + INTERVAL (15) DAY(5) TO DAY, YEAR TO DAY),?,?, CURRENT,?,?,?,?,?)`
2136
// the insert statement of payment
2237
const INSERT_PAYMENT = 'INSERT INTO payment (payment_id, user_id, most_recent_detail_id, create_date, modify_date, has_global_ad) VALUES(?,?,?, CURRENT, CURRENT, "f")'
@@ -37,6 +52,20 @@ async function prepare (connection, sql) {
3752
return Promise.promisifyAll(stmt)
3853
}
3954

55+
async function paymentExists(payment) {
56+
const connection = await helper.getInformixConnection()
57+
try {
58+
const query = util.format(QUERY_PAYMENT, payment.memberId, payment.v5ChallengeId, payment.typeId)
59+
logger.debug(`Checking if paymentExists - ${query}`)
60+
return connection.queryAsync(query)
61+
} catch (e) {
62+
logger.error(`Error in 'paymentExists' ${e}`)
63+
throw e
64+
} finally {
65+
await connection.closeAsync()
66+
}
67+
}
68+
4069
/**
4170
* Create payment and save it to db
4271
* @param {Object} payment the payment info
@@ -67,5 +96,6 @@ async function createPayment (payment) {
6796
}
6897

6998
module.exports = {
70-
createPayment
99+
createPayment,
100+
paymentExists
71101
}

src/services/processorService.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)