|
| 1 | +/** |
| 2 | + * Legacy Challenge Service |
| 3 | + * Interacts with InformixDB |
| 4 | + */ |
| 5 | +const logger = require('../common/logger') |
| 6 | +const util = require('util') |
| 7 | +const helper = require('../common/helper') |
| 8 | +const { createChallengeStatusesMap } = require('../constants') |
| 9 | + |
| 10 | +const QUERY_UPDATE_PROJECT = 'UPDATE project SET project_status_id = ?, modify_user = ? WHERE project_id = %d' |
| 11 | + |
| 12 | +/** |
| 13 | + * Prepare Informix statement |
| 14 | + * @param {Object} connection the Informix connection |
| 15 | + * @param {String} sql the sql |
| 16 | + * @return {Object} Informix statement |
| 17 | + */ |
| 18 | +async function prepare (connection, sql) { |
| 19 | + // logger.debug(`Preparing SQL ${sql}`) |
| 20 | + const stmt = await connection.prepareAsync(sql) |
| 21 | + return Promise.promisifyAll(stmt) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Update a challenge in IFX |
| 26 | + * @param {Number} challengeLegacyId the legacy challenge ID |
| 27 | + * @param {Number} createdBy the creator user ID |
| 28 | + */ |
| 29 | +async function cancelChallenge (challengeLegacyId, createdBy) { |
| 30 | + const connection = await helper.getInformixConnection() |
| 31 | + let result = null |
| 32 | + try { |
| 33 | + await connection.beginTransactionAsync() |
| 34 | + const query = await prepare(connection, util.format(QUERY_UPDATE_PROJECT, challengeLegacyId)) |
| 35 | + result = await query.executeAsync([createChallengeStatusesMap.CancelledClientRequest, createdBy]) |
| 36 | + await connection.commitTransactionAsync() |
| 37 | + } catch (e) { |
| 38 | + logger.error(`Error in 'cancelChallenge' ${e}, rolling back transaction`) |
| 39 | + await connection.rollbackTransactionAsync() |
| 40 | + throw e |
| 41 | + } finally { |
| 42 | + logger.info(`Challenge ${challengeLegacyId} has been cancelled`) |
| 43 | + await connection.closeAsync() |
| 44 | + } |
| 45 | + return result |
| 46 | +} |
| 47 | + |
| 48 | +module.exports = { |
| 49 | + cancelChallenge |
| 50 | +} |
0 commit comments