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

Commit e85068b

Browse files
committed
Fixing the migration and sync scripts to manage copilot payments
1 parent 9668352 commit e85068b

File tree

4 files changed

+59
-29
lines changed

4 files changed

+59
-29
lines changed

config/default.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ module.exports = {
5656

5757
AMAZON: {
5858
// Uncomment for local deployment
59-
// AWS_ACCESS_KEY_ID: process.env.AWS_FAKE_ID,
60-
// AWS_SECRET_ACCESS_KEY: process.env.AWS_FAKE_KEY,
59+
AWS_ACCESS_KEY_ID: process.env.AWS_FAKE_ID,
60+
AWS_SECRET_ACCESS_KEY: process.env.AWS_FAKE_KEY,
6161
AWS_REGION: process.env.AWS_REGION || 'ap-northeast-1', // aws region
6262
IS_LOCAL_DB: process.env.IS_LOCAL_DB ? process.env.IS_LOCAL_DB === 'true' : false, // true or uninitialize if we use local instance
6363
DYNAMODB_URL: process.env.DYNAMODB_URL || 'http://localhost:8000', // just for local development

src/scripts/migrations/008-fix-copilot-payments.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,43 @@ const migrationFunction = {
2323
logger.info(`Found ${challenges.length} challenges`)
2424
if (challenges.length > 0) {
2525
for (const challenge of challenges) {
26-
const copilotPayment = _.get(_.find(_.get(challenge, 'prizeSets', []), p => p.type === config.COPILOT_PAYMENT_TYPE), 'prizes[0].value', null)
26+
// for some reason they're still coming back with migration number 8.2
27+
if (challenge.legacy && challenge.legacy.migration && _.toString(challenge.legacy.migration) === '8.2') {
28+
logger.error(`Right Version ${challenge.legacyId} ${challenge.legacy.migration}`)
29+
continue
30+
}
2731
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)
32+
if (!legacyId) {
33+
logger.error(`No Legacy ID on challenge ${challenge.id}`)
34+
continue
35+
}
36+
const legacyCopilotPayment = await challengeIfxService.getCopilotPaymentFromIfx(legacyId)
37+
if (legacyCopilotPayment && legacyCopilotPayment.value > 0) {
38+
let updatedChallenge
39+
try {
40+
[updatedChallenge] = await challengeService.getChallengeFromV5API(legacyId)
41+
} catch (e) {
42+
logger.debug('Unable to get challenge from v5... Skipping')
43+
continue
44+
}
45+
if (updatedChallenge) {
46+
// const copilotPayment = await challengeIfxService.getCopilotPaymentFromIfx(legacyId)
47+
const prizeSet = { type: 'copilot', description: 'Copilot Payment' }
48+
prizeSet.prizes = []
49+
prizeSet.prizes.push({ value: legacyCopilotPayment.value, type: 'USD' })
50+
updatedChallenge.prizeSets.push(prizeSet)
51+
updatedChallenge.legacy.migration = 8.2
52+
await challengeService.save(updatedChallenge)
3453
}
3554
} else {
36-
await challengeIfxService.createCopilotPaymentInIfx(legacyId, copilotPayment, challenge.createdBy)
55+
if (!challenge.legacy) {
56+
challenge.legacy = { migration: 8.2 }
57+
} else {
58+
challenge.legacy.migration = 8.2
59+
}
60+
// logger.debug(`No Change - Saving Challenge ${challenge.legacy.migration}`)
61+
await challengeService.save(challenge)
3762
}
38-
challenge.legacy.migration = 8
39-
challengeService.save(challenge)
4063
}
4164
} else {
4265
finish = true
@@ -55,22 +78,15 @@ async function getChallengesMissingData (page = 0, perPage = 10) {
5578
from: page * perPage,
5679
body: {
5780
query: {
58-
bool: {
59-
must: {
60-
exists: {
61-
field: 'prizeSets'
62-
}
63-
},
64-
must_not: {
65-
match_phrase: {
66-
'legacy.migration': 8
67-
}
81+
range: {
82+
'legacy.migration': {
83+
lt: 8.2
6884
}
6985
}
7086
}
7187
}
7288
}
73-
// logger.debug(`ES Query ${JSON.stringify(esQuery)}`)
89+
logger.debug(`ES Query ${JSON.stringify(esQuery)}`)
7490
// Search with constructed query
7591
let docs
7692
try {

src/services/challengeInformixService.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ const { executeQueryAsync } = require('../util/informixWrapper')
99
* @param {Number} legacyId the legacy ID
1010
*/
1111
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)
12+
const sql = `SELECT limit 1 * FROM project_info WHERE project_id = ${_.toInteger(legacyId)} AND project_info_type_id = 49`
13+
const result = await execQuery(sql)
14+
if (result && result[0]) return result[0]
15+
return false
1416
}
1517

1618
/**
@@ -32,7 +34,7 @@ async function createCopilotPaymentInIfx (legacyId, amount, createdBy) {
3234
modify_date
3335
)
3436
VALUES
35-
(${legacyId}, 49, ${amount}, ${createdBy}, CURRENT, ${createdBy}, CURRENT)`
37+
(${_.toInteger(legacyId)}, 49, ${amount}, ${createdBy}, CURRENT, ${createdBy}, CURRENT)`
3638
return execQuery(sql)
3739
}
3840

@@ -43,7 +45,7 @@ async function createCopilotPaymentInIfx (legacyId, amount, createdBy) {
4345
* @param {String} updatedBy the update user handle
4446
*/
4547
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}`
48+
const sql = `UPDATE project_info SET value = ${newValue}, modify_user = ${updatedBy}, modify_date = CURRENT WHERE project_info_type_id = 49 AND project_id = ${_.toInteger(legacyId)}`
4749
return execQuery(sql)
4850
}
4951

@@ -52,7 +54,7 @@ async function updateCopilotPaymentInIfx (legacyId, newValue, updatedBy) {
5254
* @param {Number} legacyId the legacy challenge id
5355
*/
5456
async function deleteCopilotPaymentFromIfx (legacyId) {
55-
const sql = `DELETE FROM project_info WHERE project_info_type_id = 49 AND project_id = ${legacyId}`
57+
const sql = `DELETE FROM project_info WHERE project_info_type_id = 49 AND project_id = ${_.toInteger(legacyId)}`
5658
return execQuery(sql)
5759
}
5860

src/services/challengeService.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const resourceService = require('./resourceService')
1515
const resourceInformixService = require('./resourceInformixService')
1616
const translationService = require('./translationService')
1717
const { V4_TRACKS } = require('../util/conversionMappings')
18+
const { getCopilotPaymentFromIfx } = require('./challengeInformixService')
1819

1920
let allV5Terms
2021

@@ -34,9 +35,12 @@ const challengePropertiesToOmitFromDynamo = [
3435

3536
async function save (challenge) {
3637
if (challenge.id) {
37-
// logger.warn(`Updating Challenge ${JSON.stringify(challenge)}`)
38+
// logger.debug(`Update Challenge ${challenge.id}`)
39+
// return
3840
return updateChallenge(challenge)
3941
}
42+
// logger.debug(`Create Challenge ${challenge.id}`)
43+
// return
4044
return createChallenge(challenge)
4145
}
4246
/**
@@ -644,6 +648,14 @@ async function buildV5Challenge (legacyId, challengeListing, challengeDetails) {
644648
prizeSets.push(prizeSet)
645649
}
646650

651+
const copilotPayment = await getCopilotPaymentFromIfx(legacyId)
652+
if (copilotPayment && copilotPayment.value > 0) {
653+
const prizeSet = { type: 'copilot', description: 'Copilot Payment' }
654+
prizeSet.prizes = []
655+
prizeSet.prizes.push({ value: copilotPayment.value, type: 'USD' })
656+
prizeSets.push(prizeSet)
657+
}
658+
647659
const tags = _.uniq(_.compact(_.concat(challengeListing.technologies, challengeListing.platforms, v5TrackProperties.tags)))
648660

649661
const winners = _.map(challengeListing.winners, w => {

0 commit comments

Comments
 (0)