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

Commit d3a1470

Browse files
authored
Merge pull request #37 from topcoder-platform/copilot-payments
add support for copilot payment in ifx
2 parents 13e49fd + 8a1f2d6 commit d3a1470

File tree

4 files changed

+168
-4
lines changed

4 files changed

+168
-4
lines changed

config/default.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,7 @@ module.exports = {
225225
WRITE: process.env.SCOPE_CHALLENGES_WRITE || 'write:challenges',
226226
ALL: process.env.SCOPE_CHALLENGES_ALL || 'all:challenges'
227227
}
228-
}
228+
},
229+
230+
COPILOT_PAYMENT_TYPE: process.env.COPILOT_PAYMENT_TYPE || 'copilot'
229231
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Fix the copilot payments on legacy:
3+
*/
4+
global.Promise = require('bluebird')
5+
6+
const config = require('config')
7+
const _ = require('lodash')
8+
const logger = require('../../util/logger')
9+
const challengeIfxService = require('../../services/challengeInformixService')
10+
const challengeService = require('../../services/challengeService')
11+
const { getESClient } = require('../../util/helper')
12+
13+
const migrationFunction = {
14+
run: async () => {
15+
const perPage = config.get('MIGRATION_SCRIPT_BATCH_SIZE')
16+
let finish = false
17+
let page = 0
18+
let batch = 1
19+
20+
while (!finish) {
21+
logger.info(`Batch-${batch} - Loading challenges`)
22+
const challenges = await getChallengesMissingData(page, perPage)
23+
logger.info(`Found ${challenges.length} challenges`)
24+
if (challenges.length > 0) {
25+
for (const challenge of challenges) {
26+
const copilotPayment = _.get(_.find(_.get(challenge, 'prizeSets', []), p => p.type === config.COPILOT_PAYMENT_TYPE), 'prizes[0].value', null)
27+
const legacyId = _.get(challenge, 'legacyId')
28+
const existing = await challengeIfxService.getCopilotPaymentFromIfx(legacyId)
29+
if (existing) {
30+
if (!copilotPayment) {
31+
await challengeIfxService.deleteCopilotPaymentFromIfx(legacyId)
32+
} else if (_.toString(existing.value) !== _.toString(copilotPayment)) {
33+
await challengeIfxService.updateCopilotPaymentInIfx(legacyId, copilotPayment, challenge.updatedBy)
34+
}
35+
} else {
36+
await challengeIfxService.createCopilotPaymentInIfx(legacyId, copilotPayment, challenge.createdBy)
37+
}
38+
challenge.legacy.migration = 8
39+
challengeService.save(challenge)
40+
}
41+
} else {
42+
finish = true
43+
}
44+
page++
45+
batch++
46+
}
47+
}
48+
}
49+
50+
async function getChallengesMissingData (page = 0, perPage = 10) {
51+
const esQuery = {
52+
index: config.get('ES.CHALLENGE_ES_INDEX'),
53+
type: config.get('ES.CHALLENGE_ES_TYPE'),
54+
size: perPage,
55+
from: page * perPage,
56+
body: {
57+
query: {
58+
bool: {
59+
must: {
60+
exists: {
61+
field: 'prizeSets'
62+
}
63+
},
64+
must_not: {
65+
match_phrase: {
66+
'legacy.migration': 8
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
// logger.debug(`ES Query ${JSON.stringify(esQuery)}`)
74+
// Search with constructed query
75+
let docs
76+
try {
77+
docs = await getESClient().search(esQuery)
78+
} catch (e) {
79+
// Catch error when the ES is fresh and has no data
80+
docs = {
81+
hits: {
82+
total: 0,
83+
hits: []
84+
}
85+
}
86+
}
87+
// Extract data from hits
88+
return _.map(docs.hits.hits, item => (item._source))
89+
}
90+
91+
module.exports = migrationFunction

src/services/challengeInformixService.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,59 @@ const logger = require('../util/logger')
33
const helper = require('../util/helper')
44
// const getErrorService = require('./errorService')
55
const { executeQueryAsync } = require('../util/informixWrapper')
6+
7+
/**
8+
* Gets the copilot payment for a legacyId
9+
* @param {Number} legacyId the legacy ID
10+
*/
11+
async function getCopilotPaymentFromIfx (legacyId) {
12+
const sql = `SELECT limit 1 * FROM project_info WHERE project_info_type_id = 49 AND project_id = ${legacyId}`
13+
return execQuery(sql)
14+
}
15+
16+
/**
17+
* Create the copilot payment record
18+
* @param {Number} legacyId the legacy challenge id
19+
* @param {Number} amount the $ amount of the copilot payment
20+
* @param {String} createdBy the create user handle
21+
*/
22+
async function createCopilotPaymentInIfx (legacyId, amount, createdBy) {
23+
const sql = `
24+
INSERT INTO project_info
25+
(
26+
project_id,
27+
project_info_type_id,
28+
value,
29+
create_user,
30+
create_date,
31+
modify_user,
32+
modify_date
33+
)
34+
VALUES
35+
(${legacyId}, 49, ${amount}, ${createdBy}, CURRENT, ${createdBy}, CURRENT)`
36+
return execQuery(sql)
37+
}
38+
39+
/**
40+
* Update the existing copilot payment for a legacyId
41+
* @param {Number} legacyId the legacy challenge id
42+
* @param {Number} newValue the new value
43+
* @param {String} updatedBy the update user handle
44+
*/
45+
async function updateCopilotPaymentInIfx (legacyId, newValue, updatedBy) {
46+
const sql = `UPDATE project_info SET value = ${newValue}, modify_user = ${updatedBy}, modify_date = CURRENT WHERE project_info_type_id = 49 AND project_id = ${legacyId}`
47+
return execQuery(sql)
48+
}
49+
50+
/**
51+
* Delete the existing copilot payment for a legacyId
52+
* @param {Number} legacyId the legacy challenge id
53+
*/
54+
async function deleteCopilotPaymentFromIfx (legacyId) {
55+
const sql = `DELETE FROM project_info WHERE project_info_type_id = 49 AND project_id = ${legacyId}`
56+
return execQuery(sql)
57+
}
58+
659
/**
760
* Get challenge scorecard information from informix
861
* @param {Object} filter {id, ids}
@@ -623,5 +676,9 @@ module.exports = {
623676
getPhaseFromIfx,
624677
getTermsFromIfx,
625678
getChallengeSubmissions,
626-
getChallengeRegistrants
679+
getChallengeRegistrants,
680+
getCopilotPaymentFromIfx,
681+
createCopilotPaymentInIfx,
682+
updateCopilotPaymentInIfx,
683+
deleteCopilotPaymentFromIfx
627684
}

src/services/syncService.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const resourceService = require('./resourceService')
66
const challengeSyncStatusService = require('../services/challengeSyncStatusService')
77
const challengeMigrationStatusService = require('../services/challengeMigrationStatusService')
88
const migrationService = require('../services/migrationService')
9+
const challengeIfxService = require('../services/challengeInformixService')
910
const { V4_TRACKS } = require('../util/conversionMappings')
1011

1112
async function syncLegacyId (legacyId, force) {
@@ -89,12 +90,25 @@ async function processChallenge (legacyId, challengeListing, challengeDetails) {
8990
logger.debug(`v4 prizes: ${JSON.stringify(challengeV4Prizes)}`)
9091
const challengeV5APIPrizes = _.get(v5ChallengeFromAPI, 'prizeSets', [])
9192
logger.debug(`v5 prizes: ${JSON.stringify(challengeV5APIPrizes)}`)
92-
const prizeSets = [
93+
const prizeSets = _.filter([
9394
..._.intersectionBy(challengeV4Prizes, challengeV5APIPrizes, 'type'),
9495
..._.differenceBy(challengeV5APIPrizes, challengeV4Prizes, 'type')
95-
]
96+
], entry => entry.type !== config.COPILOT_PAYMENT_TYPE)
9697
logger.debug(`intersection: ${JSON.stringify(prizeSets)}`)
9798

99+
const copilotPayment = await challengeIfxService.getCopilotPaymentFromIfx(legacyId)
100+
if (copilotPayment) {
101+
prizeSets.push({
102+
prizes: [
103+
{
104+
type: 'USD',
105+
value: copilotPayment.value
106+
}
107+
],
108+
type: config.COPILOT_PAYMENT_TYPE
109+
})
110+
}
111+
98112
const updatedV5Object = {
99113
..._.omit(v5ChallengeFromAPI, ['prizeSets']),
100114
..._.omit(v5ChallengeObjectFromV4, ommittedFields),

0 commit comments

Comments
 (0)