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

Support cancelling a challenge #83

Merged
merged 1 commit into from
Jun 21, 2022
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
7 changes: 6 additions & 1 deletion src/services/ProcessorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const metadataService = require('./metadataService')
const paymentService = require('./paymentService')
const { createOrSetNumberOfReviewers } = require('./selfServiceReviewerService')
const { disableTimelineNotifications } = require('./selfServiceNotificationService')
const legacyChallengeService = require('./legacyChallengeService')

/**
* Drop and recreate phases in ifx
Expand Down Expand Up @@ -656,7 +657,6 @@ async function processMessage (message) {
if (_.get(message, 'payload.legacy.selfService')) {
await disableTimelineNotifications(legacyId, createdByUserId) // disable
}

}

logger.debug('Result from parsePayload:')
Expand Down Expand Up @@ -734,6 +734,11 @@ async function processMessage (message) {
} else {
logger.info('Will skip syncing phases as the challenge is a task...')
}
if (message.payload.status === constants.challengeStatuses.CancelledClientRequest && challenge.currentStatus !== constants.challengeStatuses.CancelledClientRequest) {
logger.info('Cancelling challenge...')
await legacyChallengeService.cancelChallenge(legacyId, updatedByUserId)
needSyncV4ES = true
}
if (needSyncV4ES) {
try {
logger.info(`Resync V4 ES for the legacy challenge ${legacyId}`)
Expand Down
50 changes: 50 additions & 0 deletions src/services/legacyChallengeService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Legacy Challenge Service
* Interacts with InformixDB
*/
const logger = require('../common/logger')
const util = require('util')
const helper = require('../common/helper')
const { createChallengeStatusesMap } = require('../constants')

const QUERY_UPDATE_PROJECT = 'UPDATE project SET project_status_id = ?, modify_user = ? WHERE project_id = %d'

/**
* 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)
}

/**
* Update a challenge in IFX
* @param {Number} challengeLegacyId the legacy challenge ID
* @param {Number} createdBy the creator user ID
*/
async function cancelChallenge (challengeLegacyId, createdBy) {
const connection = await helper.getInformixConnection()
let result = null
try {
await connection.beginTransactionAsync()
const query = await prepare(connection, util.format(QUERY_UPDATE_PROJECT, challengeLegacyId))
result = await query.executeAsync([createChallengeStatusesMap.CancelledClientRequest, createdBy])
await connection.commitTransactionAsync()
} catch (e) {
logger.error(`Error in 'cancelChallenge' ${e}, rolling back transaction`)
await connection.rollbackTransactionAsync()
throw e
} finally {
logger.info(`Challenge ${challengeLegacyId} has been cancelled`)
await connection.closeAsync()
}
return result
}

module.exports = {
cancelChallenge
}