|
| 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