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

Commit 1f6949a

Browse files
authored
Merge pull request #3 from xxcxy/develop
fix LPP updates #2
2 parents 3e684ec + ba159bc commit 1f6949a

File tree

5 files changed

+69
-27
lines changed

5 files changed

+69
-27
lines changed

config/default.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ module.exports = {
4747

4848
PAYMENT_METHOD_ID: process.env.PAYMENT_METHOD_ID || 1,
4949
CHARITY_IND: process.env.CHARITY_IND || 0,
50-
INSTALLMENT_NUMBER: process.env.INSTALLMENT_NUMBER || 1
50+
INSTALLMENT_NUMBER: process.env.INSTALLMENT_NUMBER || 1,
51+
COPILOT_ROLE_ID: process.env.COPILOT_ROLE_ID || 'cfe12b3f-2a24-4639-9d8b-ec86726f76bd'
5152
}

src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const dataHandler = (messageSet, topic, partition) => Promise.each(messageSet, a
4444
return
4545
}
4646

47-
if (_.upperCase(_.get(messageJSON.payload, 'type')) !== 'TASK' || _.upperCase(_.get(messageJSON.payload, 'status')) !== 'COMPLETED') {
47+
if (_.toUpper(_.get(messageJSON.payload, 'type')) !== 'TASK' || _.toUpper(_.get(messageJSON.payload, 'status')) !== 'COMPLETED') {
4848
logger.info(`The message type ${_.get(messageJSON.payload, 'type')}, status ${_.get(messageJSON.payload, 'status')} doesn't match {type: 'Task', status: 'Completed'}.`)
4949

5050
// commit the message and ignore it

src/common/helper.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,29 @@ async function getUserId (handle) {
130130
return res.body.userId
131131
}
132132

133+
/**
134+
* Function to get copilot id
135+
* @param {String} challengeId the challengeId
136+
* @returns {String} the userId
137+
*/
138+
async function getCopilotId (challengeId) {
139+
const token = await getM2MToken()
140+
const url = `${config.TC_API}/resources?challengeId=${challengeId}&roleId=${config.COPILOT_ROLE_ID}`
141+
const res = await request
142+
.get(url)
143+
.set('Authorization', `Bearer ${token}`)
144+
.set('Content-Type', 'application/json')
145+
.set('Accept', 'application/json')
146+
return _.get(_.head(res.body), 'memberId')
147+
}
148+
133149
module.exports = {
134150
getInformixConnection,
135151
getKafkaOptions,
136152
patchRequest,
137153
getRequest,
138154
putRequest,
139155
postRequest,
140-
getUserId
156+
getUserId,
157+
getCopilotId
141158
}

src/services/paymentService.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ async function createPayment (payment) {
3939
const paymentDetailId = await paymentDetailIdGen.getNextId()
4040
const paymentId = await paymentIdGen.getNextId()
4141
try {
42+
await connection.beginTransactionAsync()
4243
const insertDetail = await prepare(connection, INSERT_PAYMENT_DETAIL)
4344
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])
4445
const insertPayment = await prepare(connection, INSERT_PAYMENT)
4546
await insertPayment.executeAsync([paymentId, payment.memberId, paymentDetailId])
4647
const insertDetailXref = await prepare(connection, INSERT_PAYMENT_DETAIL_XREF)
4748
await insertDetailXref.executeAsync([paymentId, paymentDetailId])
4849
logger.info(`Payment ${paymentId} with detail ${paymentDetailId} has been inserted`)
50+
await connection.commitTransactionAsync()
4951
} catch (e) {
5052
logger.error(`Error in 'createPayment' ${e}, rolling back transaction`)
5153
await connection.rollbackTransactionAsync()

src/services/processorService.js

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,47 @@ async function processUpdate (message) {
3434
createUser: createUserId
3535
}
3636

37-
// the properties of userPayment
38-
try {
39-
const userPayment = _.assign({
40-
memberId: message.payload.task.memberId,
41-
amount: _.head(_.find(message.payload.prizeSets, ['type', 'placement']).prizes).value,
42-
desc: `Task - ${message.payload.name} - First Place`,
43-
typeId: config.WINNER_PAYMENT_TYPE_ID
44-
}, basePayment)
45-
await paymentService.createPayment(userPayment)
46-
} catch (error) {
47-
logger.error(`For challenge ${challengeId}, user prize info missing: ${error}`)
37+
// add winner payment
38+
const winnerPrizes = _.get(_.find(message.payload.prizeSets, ['type', 'placement']), 'prizes', [])
39+
const winnerMembers = _.sortBy(_.get(message.payload, 'winners', []), ['placement'])
40+
if (_.isEmpty(winnerPrizes)) {
41+
logger.warn(`For challenge ${challengeId}, no winner payment avaiable`)
42+
} else if (winnerPrizes.length !== winnerMembers.length) {
43+
logger.error(`For challenge ${challengeId}, there is ${winnerPrizes.length} user prizes but ${winnerMembers.length} winners`)
44+
} else {
45+
try {
46+
for (let i = 1; i <= winnerPrizes.length; i++) {
47+
await paymentService.createPayment(_.assign({
48+
memberId: winnerMembers[i - 1].userId,
49+
amount: winnerPrizes[i - 1].value,
50+
desc: `Task - ${message.payload.name} - ${i} Place`,
51+
typeId: config.WINNER_PAYMENT_TYPE_ID
52+
}, basePayment))
53+
}
54+
} catch (error) {
55+
logger.error(`For challenge ${challengeId}, add winner payments error: ${error}`)
56+
}
4857
}
4958

50-
// the properties of copilotPayment
51-
try {
52-
const copilotPayment = _.assign({
53-
memberId: createUserId,
54-
amount: _.head(_.find(message.payload.prizeSets, ['type', 'copilot']).prizes).value,
55-
desc: `Task - ${message.payload.name} - Copilot`,
56-
typeId: config.COPILOT_PAYMENT_TYPE_ID
57-
}, basePayment)
58-
await paymentService.createPayment(copilotPayment)
59-
} catch (error) {
60-
logger.debug(`For challenge ${challengeId}, no copilot payment avaiable`)
59+
// add copilot payment
60+
const copilotId = await helper.getCopilotId(message.payload.id)
61+
const copilotAmount = _.get(_.head(_.get(_.find(message.payload.prizeSets, ['type', 'copilot']), 'prizes', [])), 'value')
62+
if (!copilotAmount) {
63+
logger.warn(`For challenge ${challengeId}, no copilot payment avaiable`)
64+
} else if (!copilotId) {
65+
logger.warn(`For challenge ${challengeId}, no copilot memberId avaiable`)
66+
} else {
67+
try {
68+
const copilotPayment = _.assign({
69+
memberId: copilotId,
70+
amount: copilotAmount,
71+
desc: `Task - ${message.payload.name} - Copilot`,
72+
typeId: config.COPILOT_PAYMENT_TYPE_ID
73+
}, basePayment)
74+
await paymentService.createPayment(copilotPayment)
75+
} catch (error) {
76+
logger.error(`For challenge ${challengeId}, add copilot payments error: ${error}`)
77+
}
6178
}
6279
}
6380

@@ -68,18 +85,23 @@ processUpdate.schema = {
6885
timestamp: Joi.date().required(),
6986
'mime-type': Joi.string().required(),
7087
payload: Joi.object().keys({
71-
id: Joi.string(),
88+
id: Joi.string().required(),
7289
legacyId: Joi.number().integer().positive(),
7390
task: Joi.object().keys({
7491
memberId: Joi.string().allow(null)
7592
}).unknown(true).required(),
76-
name: Joi.string(),
93+
name: Joi.string().required(),
7794
prizeSets: Joi.array().items(Joi.object().keys({
7895
type: Joi.string().valid('copilot', 'placement').required(),
7996
prizes: Joi.array().items(Joi.object().keys({
8097
value: Joi.number().positive().required()
8198
}).unknown(true))
8299
}).unknown(true)).min(1),
100+
winners: Joi.array().items(Joi.object({
101+
userId: Joi.string().required(),
102+
handle: Joi.string(),
103+
placement: Joi.number().integer().positive().required()
104+
}).unknown(true)),
83105
type: Joi.string().required(),
84106
status: Joi.string().required(),
85107
createdBy: Joi.string().required()

0 commit comments

Comments
 (0)