diff --git a/config/default.js b/config/default.js index 7f1a251..0591642 100644 --- a/config/default.js +++ b/config/default.js @@ -63,5 +63,7 @@ module.exports = { // PHASE IDs REGISTRATION_PHASE_ID: process.env.REGISTRATION_PHASE_ID || 'a93544bc-c165-4af4-b55e-18f3593b457a', SUBMISSION_PHASE_ID: process.env.SUBMISSION_PHASE_ID || '6950164f-3c5e-4bdc-abc8-22aaf5a1bd49', - CHECKPOINT_SUBMISSION_PHASE_ID: process.env.CHECKPOINT_SUBMISSION_PHASE_ID || 'd8a2cdbe-84d1-4687-ab75-78a6a7efdcc8' + CHECKPOINT_SUBMISSION_PHASE_ID: process.env.CHECKPOINT_SUBMISSION_PHASE_ID || 'd8a2cdbe-84d1-4687-ab75-78a6a7efdcc8', + + COPILOT_PAYMENT_TYPE: process.env.COPILOT_PAYMENT_TYPE || 'copilot' } diff --git a/src/services/ProcessorService.js b/src/services/ProcessorService.js index 22244b6..c842ba5 100644 --- a/src/services/ProcessorService.js +++ b/src/services/ProcessorService.js @@ -11,6 +11,7 @@ const logger = require('../common/logger') const helper = require('../common/helper') const constants = require('../constants') const groupService = require('./groupsService') +const copilotPaymentService = require('./copilotPaymentService') // TODO: Remove this // const showdown = require('showdown') // const converter = new showdown.Converter() @@ -40,6 +41,21 @@ async function associateChallengeGroups (toBeAdded = [], toBeDeleted = [], chall } } +/** + * Set the copilot payment on legacy + * @param {Number|String} legacyChallengeId the legacy challenge ID + * @param {Array} prizeSets the prizeSets array + */ +async function setCopilotPayment (legacyChallengeId, prizeSets = []) { + try { + const copilotPayment = _.get(_.find(prizeSets, p => p.type === config.COPILOT_PAYMENT_TYPE), 'prizes[0].value', null) + await copilotPaymentService.setCopilotPayment(legacyChallengeId, copilotPayment) + } catch (e) { + logger.error('Failed to set the copilot payment!') + logger.debug(e) + } +} + /** * Get technologies from V4 API * @param {String} m2mToken token for accessing the API @@ -293,6 +309,7 @@ async function processCreate (message) { const newChallenge = await helper.postRequest(`${config.V4_CHALLENGE_API_URL}`, { param: _.omit(saveDraftContestDTO, ['groupsToBeAdded', 'groupsToBeDeleted']) }, m2mToken) await helper.forceV4ESFeeder(newChallenge.body.result.content.id) await associateChallengeGroups(saveDraftContestDTO.groupsToBeAdded, saveDraftContestDTO.groupsToBeDeleted, newChallenge.body.result.content.id) + await setCopilotPayment(newChallenge.body.result.content.id, _.get(message, 'payload.prizeSets')) await helper.patchRequest(`${config.V5_CHALLENGE_API_URL}/${challengeUuid}`, { legacy: { ...message.payload.legacy, @@ -412,6 +429,7 @@ async function processUpdate (message) { try { await helper.putRequest(`${config.V4_CHALLENGE_API_URL}/${message.payload.legacyId}`, { param: _.omit(saveDraftContestDTO, ['groupsToBeAdded', 'groupsToBeDeleted']) }, m2mToken) await associateChallengeGroups(saveDraftContestDTO.groupsToBeAdded, saveDraftContestDTO.groupsToBeDeleted, message.payload.legacyId) + await setCopilotPayment(message.payload.legacyId, _.get(message, 'payload.prizeSets')) if (message.payload.status) { // logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`) diff --git a/src/services/copilotPaymentService.js b/src/services/copilotPaymentService.js new file mode 100644 index 0000000..3662131 --- /dev/null +++ b/src/services/copilotPaymentService.js @@ -0,0 +1,110 @@ +const logger = require('../common/logger') +const _ = require('lodash') +const util = require('util') +const helper = require('../common/helper') + +const QUERY_GET_COPILOT_PAYMENT = 'SELECT limit 1 * FROM project_info WHERE project_info_type_id = 49 AND project_id = %d' +const QUERY_INSERT_COPILOT_PAYMENT = ` + INSERT INTO project_info + ( + project_id, + project_info_type_id, + value, + create_user, + create_date, + modify_user, + modify_date + ) + VALUES + (?, 49, ?, ?, CURRENT, ?, CURRENT)` +const QUERY_UPDATE_COPILOT_PAYMENT = 'UPDATE project_info SET value = ?, modify_user = ?, modify_date = CURRENT WHERE project_info_type_id = 49 AND project_id = ?' +const QUERY_DELETE_COPILOT_PAYMENT = 'DELETE FROM project_info WHERE project_info_type_id = 49 AND project_id = ?' + +/** + * Prepare Informix statement + * @param {Object} connection the Informix connection + * @param {String} sql the sql + * @return {Object} Informix statement + */ +async function prepare (connection, sql) { + // logger.debug(`Preparing SQL ${sql}`) + const stmt = await connection.prepareAsync(sql) + return Promise.promisifyAll(stmt) +} + +/** + * Set the copilot payment + * @param {Number} challengeLegacyId the legacy challenge ID + * @param {Number} amount the $ amount of the copilot payment + * @param {String} createdBy the create user handle + * @param {String} updatedBy the update user handle + */ +async function setCopilotPayment (challengeLegacyId, amount, createdBy, updatedBy) { + const connection = await helper.getInformixConnection() + try { + // await connection.beginTransactionAsync() + const copilotPayment = await getCopilotPayment(connection, challengeLegacyId) + if (copilotPayment) { + if (!amount) { + await deleteCopilotPayment(connection, challengeLegacyId) + } else if (_.toString(copilotPayment.value) !== _.toString(amount)) { + await updateCopilotPayment(connection, challengeLegacyId, amount, updatedBy) + } + } else { + await createCopilotPayment(connection, challengeLegacyId, amount, createdBy) + } + } catch (e) { + logger.error(`Error in 'setCopilotPayment' ${e}`) + // await connection.rollbackTransactionAsync() + throw e + } finally { + await connection.closeAsync() + } +} + +/** + * Gets the copilot payment for a legacyId + * @param {Object} connection + * @param {Number} challengeLegacyId + */ +async function getCopilotPayment (connection, challengeLegacyId) { + const result = await connection.queryAsync(util.format(QUERY_GET_COPILOT_PAYMENT, challengeLegacyId)) + return _.get(result, '[0]', null) +} + +/** + * Create the copilot payment record + * @param {Object} connection the connection + * @param {Number} challengeLegacyId the legacy challenge id + * @param {Number} amount the $ amount of the copilot payment + * @param {String} createdBy the create user handle + */ +async function createCopilotPayment (connection, challengeLegacyId, amount, createdBy) { + const query = await prepare(connection, QUERY_INSERT_COPILOT_PAYMENT) + return query.executeAsync([challengeLegacyId, amount, createdBy, createdBy]) +} + +/** + * Update the existing copilot payment for a legacyId + * @param {Object} connection + * @param {Number} challengeLegacyId + * @param {*} updatedBy the update user handle + */ +async function updateCopilotPayment (connection, challengeLegacyId, newValue, updatedBy) { + const query = await prepare(connection, QUERY_UPDATE_COPILOT_PAYMENT) + return query.executeAsync([newValue, updatedBy, challengeLegacyId]) +} + +/** + * Delete the existing copilot payment for a legacyId + * @param {Object} connection + * @param {Number} challengeLegacyId + */ +async function deleteCopilotPayment (connection, challengeLegacyId) { + const query = await prepare(connection, QUERY_DELETE_COPILOT_PAYMENT) + return query.executeAsync([challengeLegacyId]) +} + +module.exports = { + setCopilotPayment +}