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

Set copilot payment to manual before set its value #33

Merged
merged 1 commit into from
Dec 17, 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
2 changes: 2 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ module.exports = {

COPILOT_PAYMENT_TYPE: process.env.COPILOT_PAYMENT_TYPE || 'copilot',

COPILOT_ROLE_ID: process.env.COPILOT_ROLE_ID || 'cfe12b3f-2a24-4639-9d8b-ec86726f76bd',

// V5 Term UUID
SYNC_V5_TERM_UUID: process.env.SYNC_V5_TERM_UUID || '317cd8f9-d66c-4f2a-8774-63c612d99cd4',
SYNC_V5_WRITE_ENABLED: process.env.SYNC_V5_WRITE_ENABLED === 'true' || false
Expand Down
21 changes: 16 additions & 5 deletions src/services/ProcessorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,27 @@ async function associateChallengeTerms (v5Terms, legacyChallengeId, createdBy, u

/**
* Set the copilot payment on legacy
* @param {String} challengeId the V5 challenge ID
* @param {Number|String} legacyChallengeId the legacy challenge ID
* @param {Array} prizeSets the prizeSets array
* @param {String} createdBy the created by handle
* @param {String} updatedBy the updated by handle
* @param {String} m2mToken the m2m token
*/
async function setCopilotPayment (legacyChallengeId, prizeSets = [], createdBy, updatedBy) {
async function setCopilotPayment (challengeId, legacyChallengeId, prizeSets = [], createdBy, updatedBy, m2mToken) {
try {
const copilotPayment = _.get(_.find(prizeSets, p => p.type === config.COPILOT_PAYMENT_TYPE), 'prizes[0].value', null)
logger.debug(`Setting Copilot Payment: ${copilotPayment} for legacyId ${legacyChallengeId}`)
await copilotPaymentService.setCopilotPayment(legacyChallengeId, copilotPayment, createdBy, updatedBy)
if (copilotPayment) {
logger.debug('Fetching challenge copilot...')
const res = await helper.getRequest(`${config.V5_RESOURCES_API_URL}?challengeId=${challengeId}&roleId=${config.COPILOT_ROLE_ID}`, m2mToken)
const [copilotResource] = res.body
if (!copilotResource) {
logger.warn(`Copilot does not exist for challenge ${challengeId} (legacy: ${legacyChallengeId})`)
return
}
logger.debug(`Setting Copilot Payment: ${copilotPayment} for legacyId ${legacyChallengeId} for copilot ${copilotResource.memberId}`)
await copilotPaymentService.setCopilotPayment(legacyChallengeId, copilotPayment, createdBy, updatedBy)
}
} catch (e) {
logger.error('Failed to set the copilot payment!')
logger.debug(e)
Expand Down Expand Up @@ -385,7 +396,7 @@ async function processCreate (message) {
await helper.forceV4ESFeeder(newChallenge.body.result.content.id)
await associateChallengeGroups(saveDraftContestDTO.groupsToBeAdded, saveDraftContestDTO.groupsToBeDeleted, newChallenge.body.result.content.id)
// await associateChallengeTerms(saveDraftContestDTO.termsToBeAdded, saveDraftContestDTO.termsToBeRemoved, newChallenge.body.result.content.id)
await setCopilotPayment(newChallenge.body.result.content.id, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))
await setCopilotPayment(challengeUuid, newChallenge.body.result.content.id, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'), m2mToken)
await helper.patchRequest(`${config.V5_CHALLENGE_API_URL}/${challengeUuid}`, {
legacy: {
...message.payload.legacy,
Expand Down Expand Up @@ -509,7 +520,7 @@ async function processUpdate (message) {
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 associateChallengeTerms(message.payload.terms, message.payload.legacyId, _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))
await setCopilotPayment(message.payload.legacyId, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))
await setCopilotPayment(message.payload.id, message.payload.legacyId, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'), m2mToken)

if (message.payload.status) {
// logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`)
Expand Down
48 changes: 48 additions & 0 deletions src/services/copilotPaymentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const helper = require('../common/helper')

const COPILOT_PAYMENT_PROJECT_INFO_ID = 49
const COPILOT_PAYMENT_RESOURCE_INFO_ID = 7
const COPILOT_PAYMENT_TYPE_ID = 15
const COPILOT_RESOURCE_ROLE_ID = 14

const QUERY_GET_COPILOT_RESOURCE_FOR_CHALLENGE = `SELECT limit 1 resource_id as resourceid FROM resource WHERE project_id = %d AND resource_role_id = ${COPILOT_RESOURCE_ROLE_ID}`
Expand Down Expand Up @@ -32,6 +33,10 @@ const QUERY_DELETE_COPILOT_PAYMENT = `DELETE FROM project_info WHERE project_inf
const QUERY_UPDATE_COPILOT_RESOURCE_PAYMENT = `UPDATE resource_info SET value = ?, modify_user = ?, modify_date = CURRENT WHERE resource_id = ? AND resource_info_type_id = ${COPILOT_PAYMENT_RESOURCE_INFO_ID}`
// const QUERY_DELETE_COPILOT_RESOURCE_PAYMENT = `DELETE FROM resource_info WHERE resource_id = ? AND resource_info_type_id = ${COPILOT_PAYMENT_RESOURCE_INFO_ID}`

const QUERY_SELECT_PAYMENT_TYPE = `SELECT value FROM resource_info WHERE project_info_type_id = ${COPILOT_PAYMENT_TYPE_ID} AND resource_id = %d`
const QUERY_INSERT_PAYMENT_TYPE = `INSERT INTO resource_info (resource_id, resource_info_type_id, value, create_user, create_date, modify_user, modify_date) VALUES (?, ${COPILOT_PAYMENT_TYPE_ID}, ?, ?, CURRENT, ?, CURRENT)`
const QUERY_UPDATE_PAYMENT_TYPE = `UPDATE resource_info SET value = ?, modify_user = ?, modify_date = CURRENT WHERE resource_id = ? AND resource_info_type_id = ${COPILOT_PAYMENT_TYPE_ID}`

/**
* Prepare Informix statement
* @param {Object} connection the Informix connection
Expand All @@ -58,6 +63,13 @@ async function setCopilotPayment (challengeLegacyId, amount, createdBy, updatedB
const copilotResourceId = await getCopilotResourceId(connection, challengeLegacyId)
const copilotPayment = await getCopilotPayment(connection, challengeLegacyId)
if (amount != null && amount >= 0) {
// Make sure the payment type is set to manual
const paymentType = await getCopilotPaymentType(connection, copilotResourceId)
if (!paymentType) {
await createCopilotPaymentType(connection, copilotResourceId, true, updatedBy || createdBy)
} else if (_.toLower(_.toString(paymentType.value)) === 'false') {
await updateCopilotPaymentType(connection, copilotResourceId, true, updatedBy || createdBy)
}
if (copilotPayment) {
logger.debug(`Copilot payment exists, updating: ${challengeLegacyId}`)
return updateCopilotPayment(connection, copilotResourceId, challengeLegacyId, amount, updatedBy)
Expand All @@ -78,6 +90,42 @@ async function setCopilotPayment (challengeLegacyId, amount, createdBy, updatedB
}
}

/**
* Gets the copilot payment type for a legacyId
* @param {Object} connection the connection
* @param {Number} resourceId the resource ID
*/
async function getCopilotPaymentType (connection, resourceId) {
const result = await connection.queryAsync(util.format(QUERY_SELECT_PAYMENT_TYPE, resourceId))
return _.get(result, '[0]', null)
}

/**
* Create the copilot payment type record
* @param {Object} connection the connection
* @param {Number} resourceId the resource ID
* @param {Boolean} value the value
* @param {String} createdBy the create user handle
*/
async function createCopilotPaymentType (connection, resourceId, value, createdBy) {
const query = await prepare(connection, QUERY_INSERT_PAYMENT_TYPE)
logger.debug(`Create Copilot Payment Type Values: ${[resourceId, value, createdBy, createdBy]}`)
await query.executeAsync([resourceId, value, createdBy, createdBy])
}

/**
* Update the copilot payment type record
* @param {Object} connection the connection
* @param {Number} resourceId the resource ID
* @param {Boolean} value the value
* @param {String} createdBy the create user handle
*/
async function updateCopilotPaymentType (connection, resourceId, value, createdBy) {
const query = await prepare(connection, QUERY_UPDATE_PAYMENT_TYPE)
logger.debug(`Update Copilot Payment Type Values: ${[value, createdBy, resourceId]}`)
await query.executeAsync([value, createdBy, resourceId])
}

/**
* Gets the copilot payment for a legacyId
* @param {Object} connection
Expand Down