|
| 1 | +const logger = require('../common/logger') |
| 2 | +const _ = require('lodash') |
| 3 | +const util = require('util') |
| 4 | +const helper = require('../common/helper') |
| 5 | + |
| 6 | +const QUERY_GET_COPILOT_PAYMENT = 'SELECT limit 1 * FROM project_info WHERE project_info_type_id = 49 AND project_id = %d' |
| 7 | +const QUERY_INSERT_COPILOT_PAYMENT = ` |
| 8 | + INSERT INTO project_info |
| 9 | + ( |
| 10 | + project_id, |
| 11 | + project_info_type_id, |
| 12 | + value, |
| 13 | + create_user, |
| 14 | + create_date, |
| 15 | + modify_user, |
| 16 | + modify_date |
| 17 | + ) |
| 18 | + VALUES |
| 19 | + (?, 49, ?, ?, CURRENT, ?, CURRENT)` |
| 20 | +const QUERY_UPDATE_COPILOT_PAYMENT = 'UPDATE project_info SET value = ?, modify_user = ?, modify_date = CURRENT WHERE project_info_type_id = 49 AND project_id = ?' |
| 21 | +const QUERY_DELETE_COPILOT_PAYMENT = 'DELETE FROM project_info WHERE project_info_type_id = 49 AND project_id = ?' |
| 22 | + |
| 23 | +/** |
| 24 | + * Prepare Informix statement |
| 25 | + * @param {Object} connection the Informix connection |
| 26 | + * @param {String} sql the sql |
| 27 | + * @return {Object} Informix statement |
| 28 | + */ |
| 29 | +async function prepare (connection, sql) { |
| 30 | + // logger.debug(`Preparing SQL ${sql}`) |
| 31 | + const stmt = await connection.prepareAsync(sql) |
| 32 | + return Promise.promisifyAll(stmt) |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Set the copilot payment |
| 37 | + * @param {Number} challengeLegacyId the legacy challenge ID |
| 38 | + * @param {Number} amount the $ amount of the copilot payment |
| 39 | + * @param {String} createdBy the create user handle |
| 40 | + * @param {String} updatedBy the update user handle |
| 41 | + */ |
| 42 | +async function setCopilotPayment (challengeLegacyId, amount, createdBy, updatedBy) { |
| 43 | + const connection = await helper.getInformixConnection() |
| 44 | + try { |
| 45 | + // await connection.beginTransactionAsync() |
| 46 | + const copilotPayment = await getCopilotPayment(connection, challengeLegacyId) |
| 47 | + if (copilotPayment) { |
| 48 | + if (!amount) { |
| 49 | + await deleteCopilotPayment(connection, challengeLegacyId) |
| 50 | + } else if (_.toString(copilotPayment.value) !== _.toString(amount)) { |
| 51 | + await updateCopilotPayment(connection, challengeLegacyId, amount, updatedBy) |
| 52 | + } |
| 53 | + } else { |
| 54 | + await createCopilotPayment(connection, challengeLegacyId, amount, createdBy) |
| 55 | + } |
| 56 | + } catch (e) { |
| 57 | + logger.error(`Error in 'setCopilotPayment' ${e}`) |
| 58 | + // await connection.rollbackTransactionAsync() |
| 59 | + throw e |
| 60 | + } finally { |
| 61 | + await connection.closeAsync() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Gets the copilot payment for a legacyId |
| 67 | + * @param {Object} connection |
| 68 | + * @param {Number} challengeLegacyId |
| 69 | + */ |
| 70 | +async function getCopilotPayment (connection, challengeLegacyId) { |
| 71 | + const result = await connection.queryAsync(util.format(QUERY_GET_COPILOT_PAYMENT, challengeLegacyId)) |
| 72 | + return _.get(result, '[0]', null) |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Create the copilot payment record |
| 77 | + * @param {Object} connection the connection |
| 78 | + * @param {Number} challengeLegacyId the legacy challenge id |
| 79 | + * @param {Number} amount the $ amount of the copilot payment |
| 80 | + * @param {String} createdBy the create user handle |
| 81 | + */ |
| 82 | +async function createCopilotPayment (connection, challengeLegacyId, amount, createdBy) { |
| 83 | + const query = await prepare(connection, QUERY_INSERT_COPILOT_PAYMENT) |
| 84 | + return query.executeAsync([challengeLegacyId, amount, createdBy, createdBy]) |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Update the existing copilot payment for a legacyId |
| 89 | + * @param {Object} connection |
| 90 | + * @param {Number} challengeLegacyId |
| 91 | + * @param {*} updatedBy the update user handle |
| 92 | + */ |
| 93 | +async function updateCopilotPayment (connection, challengeLegacyId, newValue, updatedBy) { |
| 94 | + const query = await prepare(connection, QUERY_UPDATE_COPILOT_PAYMENT) |
| 95 | + return query.executeAsync([newValue, updatedBy, challengeLegacyId]) |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Delete the existing copilot payment for a legacyId |
| 100 | + * @param {Object} connection |
| 101 | + * @param {Number} challengeLegacyId |
| 102 | + */ |
| 103 | +async function deleteCopilotPayment (connection, challengeLegacyId) { |
| 104 | + const query = await prepare(connection, QUERY_DELETE_COPILOT_PAYMENT) |
| 105 | + return query.executeAsync([challengeLegacyId]) |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = { |
| 109 | + setCopilotPayment |
| 110 | +} |
0 commit comments