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

Commit dc5f6ab

Browse files
Create script to sync terms with legacy
1 parent 60de4dd commit dc5f6ab

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

config/default.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,8 @@ module.exports = {
7272
LEGACY_TERMS_NDA_ID: process.env.LEGACY_TERMS_NDA_ID || 21343,
7373
LEGACY_SUBMITTER_ROLE_ID: process.env.LEGACY_SUBMITTER_ROLE_ID || 1,
7474

75-
COPILOT_PAYMENT_TYPE: process.env.COPILOT_PAYMENT_TYPE || 'copilot'
75+
COPILOT_PAYMENT_TYPE: process.env.COPILOT_PAYMENT_TYPE || 'copilot',
76+
77+
// V5 Term UUID
78+
V5_TERM_UUID: process.env.V5_TERM_UUID || '317cd8f9-d66c-4f2a-8774-63c612d99cd4'
7679
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"lint:fix": "standard --fix",
1010
"mock-api": "NODE_ENV=test node test/mock/mock",
1111
"test": "nyc --reporter=html --reporter=text mocha test/unit/test.js --timeout 20000 --exit",
12-
"e2e": "nyc --reporter=html --reporter=text mocha test/e2e/test.js --timeout 20000 --exit"
12+
"e2e": "nyc --reporter=html --reporter=text mocha test/e2e/test.js --timeout 20000 --exit",
13+
"sync-terms": "node src/scripts/sync-terms.js"
1314
},
1415
"repository": "https://github.com/topcoder-platform/legacy-challenge-processor",
1516
"author": "TCSCODER",

src/scripts/sync-terms.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require('../bootstrap')
2+
const util = require('util')
3+
const config = require('config')
4+
const _ = require('lodash')
5+
const logger = require('../common/logger')
6+
const helper = require('../common/helper')
7+
8+
const QUERY_GET_ENTRIES = 'SELECT user_id FROM user_terms_of_use_xref WHERE terms_of_use_id = %d'
9+
const QUERY_INSERT_ENTRY = 'INSERT INTO user_terms_of_use_xref (user_id, terms_of_use_id, create_date, modify_date) VALUES (?, ?, ?, ?)'
10+
11+
/**
12+
* Prepare Informix statement
13+
* @param {Object} connection the Informix connection
14+
* @param {String} sql the sql
15+
* @return {Object} Informix statement
16+
*/
17+
async function prepare (connection, sql) {
18+
// logger.debug(`Preparing SQL ${sql}`)
19+
const stmt = await connection.prepareAsync(sql)
20+
return Promise.promisifyAll(stmt)
21+
}
22+
23+
/**
24+
* Gets the entries from ifx
25+
* @param {Number} termsOfUseId the legacy terms of use ID
26+
*/
27+
async function getTermsFromIfx (termsOfUseId) {
28+
const connection = await helper.getInformixConnection()
29+
let result = null
30+
try {
31+
result = await connection.queryAsync(util.format(QUERY_GET_ENTRIES, termsOfUseId))
32+
} catch (e) {
33+
logger.error(`Error in 'getTermsFromIfx' ${e}`)
34+
throw e
35+
} finally {
36+
await connection.closeAsync()
37+
}
38+
return _.map(result, r => _.toInteger(r.user_id))
39+
}
40+
41+
/**
42+
* Creates a new entry in IFX
43+
* @param {Number} termsOfUseId the legacy terms of use ID
44+
* @param {Number} memberId the member ID
45+
*/
46+
async function createEntry (termsOfUseId, memberId) {
47+
const connection = await helper.getInformixConnection()
48+
let result = null
49+
try {
50+
// await connection.beginTransactionAsync()
51+
const currentDateIso = new Date().toISOString().replace('T', ' ').replace('Z', '').split('.')[0]
52+
const query = await prepare(connection, QUERY_INSERT_ENTRY)
53+
result = await query.executeAsync([termsOfUseId, memberId, currentDateIso, currentDateIso])
54+
// await connection.commitTransactionAsync()
55+
} catch (e) {
56+
logger.error(`Error in 'createEntry' ${e}, rolling back transaction`)
57+
await connection.rollbackTransactionAsync()
58+
throw e
59+
} finally {
60+
logger.info(`Entry for term ${termsOfUseId} and member ${memberId} has been created`)
61+
await connection.closeAsync()
62+
}
63+
return result
64+
}
65+
66+
/**
67+
* Application entry point
68+
*/
69+
async function main () {
70+
try {
71+
let res
72+
const m2mToken = await helper.getM2MToken()
73+
logger.info(`Fetching details for term ${config.V5_TERM_UUID}`)
74+
res = await helper.getRequest(`${config.V5_TERMS_API_URL}/${config.V5_TERM_UUID}`, m2mToken)
75+
const legacyTermId = _.get(res, 'body.legacyId')
76+
if (!legacyTermId) {
77+
throw new Error(`Term ${config.V5_TERM_UUID} does not have a legacyId`)
78+
}
79+
logger.info(`Fetching users that have agreed to ${config.V5_TERM_UUID}`)
80+
res = await helper.getRequest(`${config.V5_TERMS_API_URL}/${config.V5_TERM_UUID}/users`, m2mToken)
81+
const v5Entries = _.get(res, 'body.result', [])
82+
logger.debug(`Found ${v5Entries.length} users`)
83+
84+
logger.info(`Fetching users from legacy for ID: ${legacyTermId}`)
85+
const legacyIntries = await getTermsFromIfx(legacyTermId)
86+
logger.debug(`Found ${legacyIntries.length} users`)
87+
for (const memberId of v5Entries) {
88+
if (legacyIntries.indexOf(memberId) === -1) {
89+
await createEntry(legacyTermId, memberId)
90+
}
91+
}
92+
logger.info('Completed!')
93+
} catch (e) {
94+
logger.logFullError(e)
95+
}
96+
}
97+
98+
main()

0 commit comments

Comments
 (0)