|
| 1 | +require('../bootstrap') |
| 2 | +const util = require('util') |
| 3 | +const config = require('config') |
| 4 | +const request = require('superagent') |
| 5 | +const _ = require('lodash') |
| 6 | +const logger = require('../common/logger') |
| 7 | +const helper = require('../common/helper') |
| 8 | + |
| 9 | +const QUERY_GET_ENTRIES = 'SELECT user_id FROM user_terms_of_use_xref WHERE terms_of_use_id = %d' |
| 10 | +const QUERY_INSERT_ENTRY = 'INSERT INTO user_terms_of_use_xref (user_id, terms_of_use_id, create_date, modify_date) VALUES (?, ?, CURRENT, CURRENT)' |
| 11 | + |
| 12 | +/** |
| 13 | + * Prepare Informix statement |
| 14 | + * @param {Object} connection the Informix connection |
| 15 | + * @param {String} sql the sql |
| 16 | + * @return {Object} Informix statement |
| 17 | + */ |
| 18 | +async function prepare (connection, sql) { |
| 19 | + // logger.debug(`Preparing SQL ${sql}`) |
| 20 | + const stmt = await connection.prepareAsync(sql) |
| 21 | + return Promise.promisifyAll(stmt) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Gets the entries from ifx |
| 26 | + * @param {Number} termsOfUseId the legacy terms of use ID |
| 27 | + */ |
| 28 | +async function getTermsFromIfx (termsOfUseId) { |
| 29 | + const connection = await helper.getInformixConnection() |
| 30 | + let result = null |
| 31 | + try { |
| 32 | + result = await connection.queryAsync(util.format(QUERY_GET_ENTRIES, termsOfUseId)) |
| 33 | + } catch (e) { |
| 34 | + logger.error(`Error in 'getTermsFromIfx' ${e}`) |
| 35 | + throw e |
| 36 | + } finally { |
| 37 | + await connection.closeAsync() |
| 38 | + } |
| 39 | + return _.map(result, r => _.toInteger(r.user_id)) |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Creates a new entry in IFX |
| 44 | + * @param {Number} termsOfUseId the legacy terms of use ID |
| 45 | + * @param {Number} memberId the member ID |
| 46 | + */ |
| 47 | +async function createEntry (termsOfUseId, memberId) { |
| 48 | + const connection = await helper.getInformixConnection() |
| 49 | + let result = null |
| 50 | + try { |
| 51 | + // await connection.beginTransactionAsync() |
| 52 | + const query = await prepare(connection, QUERY_INSERT_ENTRY) |
| 53 | + if (config.SYNC_V5_WRITE_ENABLED) { |
| 54 | + result = await query.executeAsync([memberId, termsOfUseId]) |
| 55 | + } else { |
| 56 | + logger.debug(`INSERT INTO user_terms_of_use_xref (user_id, terms_of_use_id, create_date, modify_date) VALUES (${memberId}, ${termsOfUseId}, CURRENT, CURRENT)`) |
| 57 | + } |
| 58 | + // await connection.commitTransactionAsync() |
| 59 | + } catch (e) { |
| 60 | + logger.error(`Error in 'createEntry' ${e}, rolling back transaction`) |
| 61 | + await connection.rollbackTransactionAsync() |
| 62 | + throw e |
| 63 | + } finally { |
| 64 | + logger.info(`Entry for term ${termsOfUseId} and member ${memberId} has been created`) |
| 65 | + await connection.closeAsync() |
| 66 | + } |
| 67 | + return result |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Gets all paginated results from `/terms/:id/users` |
| 72 | + * @param {String} m2mToken the m2m token |
| 73 | + */ |
| 74 | +async function getAllTermUsers (m2mToken) { |
| 75 | + let allData = [] |
| 76 | + // get search is paginated, we need to get all pages' data |
| 77 | + let page = 1 |
| 78 | + while (true) { |
| 79 | + const result = await request |
| 80 | + .get(`${config.V5_TERMS_API_URL}/${config.SYNC_V5_TERM_UUID}/users`) |
| 81 | + .query({ page, perPage: 100 }) |
| 82 | + .set('Authorization', `Bearer ${m2mToken}`) |
| 83 | + .set('Content-Type', 'application/json') |
| 84 | + .set('Accept', 'application/json') |
| 85 | + |
| 86 | + const users = _.get(result, 'body.result', []) |
| 87 | + if (users.length === 0) { |
| 88 | + break |
| 89 | + } |
| 90 | + allData = allData.concat(users) |
| 91 | + page += 1 |
| 92 | + if (result.headers['x-total-pages'] && page > Number(result.headers['x-total-pages'])) { |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + return allData |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Application entry point |
| 101 | + */ |
| 102 | +async function main () { |
| 103 | + try { |
| 104 | + const m2mToken = await helper.getM2MToken() |
| 105 | + logger.info(`Fetching details for term ${config.SYNC_V5_TERM_UUID}`) |
| 106 | + const res = await helper.getRequest(`${config.V5_TERMS_API_URL}/${config.SYNC_V5_TERM_UUID}`, m2mToken) |
| 107 | + const legacyTermId = _.get(res, 'body.legacyId') |
| 108 | + if (!legacyTermId) { |
| 109 | + throw new Error(`Term ${config.SYNC_V5_TERM_UUID} does not have a legacyId`) |
| 110 | + } |
| 111 | + logger.info(`Fetching users that have agreed to ${config.SYNC_V5_TERM_UUID}`) |
| 112 | + const v5Entries = await getAllTermUsers(m2mToken) |
| 113 | + logger.debug(`Found ${v5Entries.length} users`) |
| 114 | + |
| 115 | + logger.info(`Fetching users from legacy for ID: ${legacyTermId}`) |
| 116 | + const legacyIntries = await getTermsFromIfx(legacyTermId) |
| 117 | + logger.debug(`Found ${legacyIntries.length} users`) |
| 118 | + for (const memberId of v5Entries) { |
| 119 | + if (legacyIntries.indexOf(memberId) === -1) { |
| 120 | + await createEntry(legacyTermId, memberId) |
| 121 | + } |
| 122 | + } |
| 123 | + logger.info('Completed!') |
| 124 | + } catch (e) { |
| 125 | + logger.logFullError(e) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +main() |
0 commit comments