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

write copilot payments into ifx #26

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
18 changes: 18 additions & 0 deletions src/services/ProcessorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}`)
Expand Down
110 changes: 110 additions & 0 deletions src/services/copilotPaymentService.js
Original file line number Diff line number Diff line change
@@ -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
}