|
| 1 | +/** |
| 2 | + * Number of reviewers Service |
| 3 | + * Interacts with InformixDB |
| 4 | + */ |
| 5 | +const util = require('util') |
| 6 | +const logger = require('../common/logger') |
| 7 | +const helper = require('../common/helper') |
| 8 | + |
| 9 | +const QUERY_GET_ENTRY = 'SELECT parameter FROM phase_criteria WHERE project_phase_id = %d' |
| 10 | +const QUERY_CREATE = 'INSERT INTO phase_criteria (project_phase_id, phase_criteria_type_id, parameter, create_user, create_date, modify_user, modify_date) VALUES (?, 6, ?, ?, CURRENT, ?, CURRENT)' |
| 11 | +const QUERY_UPDATE = 'UPDATE phase_criteria SET parameter = ?, modify_user = ?, modify_date = CURRENT WHERE project_phase_id = ?' |
| 12 | +const QUERY_DELETE = 'DELETE FROM phase_criteria WHERE project_phase_id = ?' |
| 13 | + |
| 14 | +/** |
| 15 | + * Prepare Informix statement |
| 16 | + * @param {Object} connection the Informix connection |
| 17 | + * @param {String} sql the sql |
| 18 | + * @return {Object} Informix statement |
| 19 | + */ |
| 20 | +async function prepare (connection, sql) { |
| 21 | + // logger.debug(`Preparing SQL ${sql}`) |
| 22 | + const stmt = await connection.prepareAsync(sql) |
| 23 | + return Promise.promisifyAll(stmt) |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Get entry |
| 28 | + * @param {Number} phaseId the phase ID |
| 29 | + */ |
| 30 | +async function getEntry (phaseId) { |
| 31 | + // logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`) |
| 32 | + const connection = await helper.getInformixConnection() |
| 33 | + let result = null |
| 34 | + try { |
| 35 | + result = await connection.queryAsync(util.format(QUERY_GET_ENTRY, phaseId)) |
| 36 | + } catch (e) { |
| 37 | + logger.error(`Error in 'getEntry' ${e}`) |
| 38 | + throw e |
| 39 | + } finally { |
| 40 | + await connection.closeAsync() |
| 41 | + } |
| 42 | + return result |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Enable timeline notifications |
| 47 | + * @param {Number} phaseId the legacy challenge ID |
| 48 | + * @param {Number} typeId the type ID |
| 49 | + * @param {Any} value the value |
| 50 | + * @param {String} createdBy the created by |
| 51 | + */ |
| 52 | +async function createOrSetNumberOfReviewers (phaseId, value, createdBy) { |
| 53 | + const connection = await helper.getInformixConnection() |
| 54 | + let result = null |
| 55 | + try { |
| 56 | + await connection.beginTransactionAsync() |
| 57 | + const [existing] = await getEntry(phaseId) |
| 58 | + if (existing) { |
| 59 | + if (value) { |
| 60 | + const query = await prepare(connection, QUERY_UPDATE) |
| 61 | + result = await query.executeAsync([value, createdBy, phaseId]) |
| 62 | + } else { |
| 63 | + const query = await prepare(connection, QUERY_DELETE) |
| 64 | + result = await query.executeAsync([phaseId, value]) |
| 65 | + } |
| 66 | + } else { |
| 67 | + const query = await prepare(connection, QUERY_CREATE) |
| 68 | + result = await query.executeAsync([phaseId, value, createdBy, createdBy]) |
| 69 | + } |
| 70 | + await connection.commitTransactionAsync() |
| 71 | + } catch (e) { |
| 72 | + logger.error(`Error in 'createOrSetNumberOfReviewers' ${e}, rolling back transaction`) |
| 73 | + await connection.rollbackTransactionAsync() |
| 74 | + throw e |
| 75 | + } finally { |
| 76 | + await connection.closeAsync() |
| 77 | + } |
| 78 | + return result |
| 79 | +} |
| 80 | + |
| 81 | +module.exports = { |
| 82 | + getEntry, |
| 83 | + createOrSetNumberOfReviewers |
| 84 | +} |
0 commit comments