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

Commit 49f2052

Browse files
authored
Merge pull request #26 from topcoder-platform/copilot-payments
write copilot payments into ifx
2 parents bed3ec7 + cd38310 commit 49f2052

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

config/default.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,7 @@ module.exports = {
6363
// PHASE IDs
6464
REGISTRATION_PHASE_ID: process.env.REGISTRATION_PHASE_ID || 'a93544bc-c165-4af4-b55e-18f3593b457a',
6565
SUBMISSION_PHASE_ID: process.env.SUBMISSION_PHASE_ID || '6950164f-3c5e-4bdc-abc8-22aaf5a1bd49',
66-
CHECKPOINT_SUBMISSION_PHASE_ID: process.env.CHECKPOINT_SUBMISSION_PHASE_ID || 'd8a2cdbe-84d1-4687-ab75-78a6a7efdcc8'
66+
CHECKPOINT_SUBMISSION_PHASE_ID: process.env.CHECKPOINT_SUBMISSION_PHASE_ID || 'd8a2cdbe-84d1-4687-ab75-78a6a7efdcc8',
67+
68+
COPILOT_PAYMENT_TYPE: process.env.COPILOT_PAYMENT_TYPE || 'copilot'
6769
}

src/services/ProcessorService.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const logger = require('../common/logger')
1111
const helper = require('../common/helper')
1212
const constants = require('../constants')
1313
const groupService = require('./groupsService')
14+
const copilotPaymentService = require('./copilotPaymentService')
1415
// TODO: Remove this
1516
// const showdown = require('showdown')
1617
// const converter = new showdown.Converter()
@@ -40,6 +41,21 @@ async function associateChallengeGroups (toBeAdded = [], toBeDeleted = [], chall
4041
}
4142
}
4243

44+
/**
45+
* Set the copilot payment on legacy
46+
* @param {Number|String} legacyChallengeId the legacy challenge ID
47+
* @param {Array} prizeSets the prizeSets array
48+
*/
49+
async function setCopilotPayment (legacyChallengeId, prizeSets = []) {
50+
try {
51+
const copilotPayment = _.get(_.find(prizeSets, p => p.type === config.COPILOT_PAYMENT_TYPE), 'prizes[0].value', null)
52+
await copilotPaymentService.setCopilotPayment(legacyChallengeId, copilotPayment)
53+
} catch (e) {
54+
logger.error('Failed to set the copilot payment!')
55+
logger.debug(e)
56+
}
57+
}
58+
4359
/**
4460
* Get technologies from V4 API
4561
* @param {String} m2mToken token for accessing the API
@@ -293,6 +309,7 @@ async function processCreate (message) {
293309
const newChallenge = await helper.postRequest(`${config.V4_CHALLENGE_API_URL}`, { param: _.omit(saveDraftContestDTO, ['groupsToBeAdded', 'groupsToBeDeleted']) }, m2mToken)
294310
await helper.forceV4ESFeeder(newChallenge.body.result.content.id)
295311
await associateChallengeGroups(saveDraftContestDTO.groupsToBeAdded, saveDraftContestDTO.groupsToBeDeleted, newChallenge.body.result.content.id)
312+
await setCopilotPayment(newChallenge.body.result.content.id, _.get(message, 'payload.prizeSets'))
296313
await helper.patchRequest(`${config.V5_CHALLENGE_API_URL}/${challengeUuid}`, {
297314
legacy: {
298315
...message.payload.legacy,
@@ -412,6 +429,7 @@ async function processUpdate (message) {
412429
try {
413430
await helper.putRequest(`${config.V4_CHALLENGE_API_URL}/${message.payload.legacyId}`, { param: _.omit(saveDraftContestDTO, ['groupsToBeAdded', 'groupsToBeDeleted']) }, m2mToken)
414431
await associateChallengeGroups(saveDraftContestDTO.groupsToBeAdded, saveDraftContestDTO.groupsToBeDeleted, message.payload.legacyId)
432+
await setCopilotPayment(message.payload.legacyId, _.get(message, 'payload.prizeSets'))
415433

416434
if (message.payload.status) {
417435
// logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`)

src/services/copilotPaymentService.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
const logger = require('../common/logger')
2+
const _ = require('lodash')
3+
const util = require('util')
4+
const helper = require('../common/helper')
5+
6+
const QUERY_GET_COPILOT_PAYMENT = 'SELECT limit 1 * FROM project_info WHERE project_info_type_id = 49 AND project_id = %d'
7+
const QUERY_INSERT_COPILOT_PAYMENT = `
8+
INSERT INTO project_info
9+
(
10+
project_id,
11+
project_info_type_id,
12+
value,
13+
create_user,
14+
create_date,
15+
modify_user,
16+
modify_date
17+
)
18+
VALUES
19+
(?, 49, ?, ?, CURRENT, ?, CURRENT)`
20+
const QUERY_UPDATE_COPILOT_PAYMENT = 'UPDATE project_info SET value = ?, modify_user = ?, modify_date = CURRENT WHERE project_info_type_id = 49 AND project_id = ?'
21+
const QUERY_DELETE_COPILOT_PAYMENT = 'DELETE FROM project_info WHERE project_info_type_id = 49 AND project_id = ?'
22+
23+
/**
24+
* Prepare Informix statement
25+
* @param {Object} connection the Informix connection
26+
* @param {String} sql the sql
27+
* @return {Object} Informix statement
28+
*/
29+
async function prepare (connection, sql) {
30+
// logger.debug(`Preparing SQL ${sql}`)
31+
const stmt = await connection.prepareAsync(sql)
32+
return Promise.promisifyAll(stmt)
33+
}
34+
35+
/**
36+
* Set the copilot payment
37+
* @param {Number} challengeLegacyId the legacy challenge ID
38+
* @param {Number} amount the $ amount of the copilot payment
39+
* @param {String} createdBy the create user handle
40+
* @param {String} updatedBy the update user handle
41+
*/
42+
async function setCopilotPayment (challengeLegacyId, amount, createdBy, updatedBy) {
43+
const connection = await helper.getInformixConnection()
44+
try {
45+
// await connection.beginTransactionAsync()
46+
const copilotPayment = await getCopilotPayment(connection, challengeLegacyId)
47+
if (copilotPayment) {
48+
if (!amount) {
49+
await deleteCopilotPayment(connection, challengeLegacyId)
50+
} else if (_.toString(copilotPayment.value) !== _.toString(amount)) {
51+
await updateCopilotPayment(connection, challengeLegacyId, amount, updatedBy)
52+
}
53+
} else {
54+
await createCopilotPayment(connection, challengeLegacyId, amount, createdBy)
55+
}
56+
} catch (e) {
57+
logger.error(`Error in 'setCopilotPayment' ${e}`)
58+
// await connection.rollbackTransactionAsync()
59+
throw e
60+
} finally {
61+
await connection.closeAsync()
62+
}
63+
}
64+
65+
/**
66+
* Gets the copilot payment for a legacyId
67+
* @param {Object} connection
68+
* @param {Number} challengeLegacyId
69+
*/
70+
async function getCopilotPayment (connection, challengeLegacyId) {
71+
const result = await connection.queryAsync(util.format(QUERY_GET_COPILOT_PAYMENT, challengeLegacyId))
72+
return _.get(result, '[0]', null)
73+
}
74+
75+
/**
76+
* Create the copilot payment record
77+
* @param {Object} connection the connection
78+
* @param {Number} challengeLegacyId the legacy challenge id
79+
* @param {Number} amount the $ amount of the copilot payment
80+
* @param {String} createdBy the create user handle
81+
*/
82+
async function createCopilotPayment (connection, challengeLegacyId, amount, createdBy) {
83+
const query = await prepare(connection, QUERY_INSERT_COPILOT_PAYMENT)
84+
return query.executeAsync([challengeLegacyId, amount, createdBy, createdBy])
85+
}
86+
87+
/**
88+
* Update the existing copilot payment for a legacyId
89+
* @param {Object} connection
90+
* @param {Number} challengeLegacyId
91+
* @param {*} updatedBy the update user handle
92+
*/
93+
async function updateCopilotPayment (connection, challengeLegacyId, newValue, updatedBy) {
94+
const query = await prepare(connection, QUERY_UPDATE_COPILOT_PAYMENT)
95+
return query.executeAsync([newValue, updatedBy, challengeLegacyId])
96+
}
97+
98+
/**
99+
* Delete the existing copilot payment for a legacyId
100+
* @param {Object} connection
101+
* @param {Number} challengeLegacyId
102+
*/
103+
async function deleteCopilotPayment (connection, challengeLegacyId) {
104+
const query = await prepare(connection, QUERY_DELETE_COPILOT_PAYMENT)
105+
return query.executeAsync([challengeLegacyId])
106+
}
107+
108+
module.exports = {
109+
setCopilotPayment
110+
}

0 commit comments

Comments
 (0)