|
| 1 | +/** |
| 2 | + * Metadata 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 value FROM project_info WHERE project_id = %d and project_info_type_id = %d' |
| 10 | +const QUERY_CREATE = 'INSERT INTO project_info (project_id, project_info_type_id, value, create_user, create_date, modify_user, modify_date) VALUES (?, ?, ?, ?, CURRENT, ?, CURRENT)' |
| 11 | +const QUERY_UPDATE = 'UPDATE project_info SET value = ?, modify_user = ?, modify_date = CURRENT WHERE project_info_type_id = ? AND project_id = ?' |
| 12 | +const QUERY_DELETE = 'DELETE FROM project_info WHERE project_id = ? and project_info_type_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 project info entry entry |
| 28 | + * @param {Number} challengeLegacyId the legacy challenge ID |
| 29 | + * @param {Number} typeId the type ID |
| 30 | + */ |
| 31 | +async function getMetadataEntry (challengeLegacyId, typeId) { |
| 32 | + // logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`) |
| 33 | + const connection = await helper.getInformixConnection() |
| 34 | + let result = null |
| 35 | + try { |
| 36 | + result = await connection.queryAsync(util.format(QUERY_GET_ENTRY, challengeLegacyId, typeId)) |
| 37 | + } catch (e) { |
| 38 | + logger.error(`Error in 'getMetadataEntry' ${e}`) |
| 39 | + throw e |
| 40 | + } finally { |
| 41 | + await connection.closeAsync() |
| 42 | + } |
| 43 | + return result |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Enable timeline notifications |
| 48 | + * @param {Number} challengeLegacyId the legacy challenge ID |
| 49 | + * @param {Number} typeId the type ID |
| 50 | + * @param {Any} value the value |
| 51 | + * @param {String} createdBy the created by |
| 52 | + */ |
| 53 | +async function createOrUpdateMetadata (challengeLegacyId, typeId, value, createdBy) { |
| 54 | + const connection = await helper.getInformixConnection() |
| 55 | + let result = null |
| 56 | + try { |
| 57 | + // await connection.beginTransactionAsync() |
| 58 | + const [existing] = await getMetadataEntry(challengeLegacyId, typeId) |
| 59 | + if (existing) { |
| 60 | + if (value) { |
| 61 | + logger.info(`Metadata ${typeId} exists. Will update`) |
| 62 | + const query = await prepare(connection, QUERY_UPDATE) |
| 63 | + result = await query.executeAsync([value, createdBy, typeId, challengeLegacyId]) |
| 64 | + } else { |
| 65 | + logger.info(`Metadata ${typeId} exists. Will delete`) |
| 66 | + const query = await prepare(connection, QUERY_DELETE) |
| 67 | + result = await query.executeAsync([challengeLegacyId, typeId]) |
| 68 | + } |
| 69 | + } else { |
| 70 | + logger.info(`Metadata ${typeId} does not exist. Will create`) |
| 71 | + const query = await prepare(connection, QUERY_CREATE) |
| 72 | + result = await query.executeAsync([challengeLegacyId, typeId, value, createdBy, createdBy]) |
| 73 | + } |
| 74 | + // await connection.commitTransactionAsync() |
| 75 | + logger.info(`Metadata with typeId ${typeId} has been enabled for challenge ${challengeLegacyId}`) |
| 76 | + } catch (e) { |
| 77 | + logger.error(`Error in 'createOrUpdateMetadata' ${e}, rolling back transaction`) |
| 78 | + await connection.rollbackTransactionAsync() |
| 79 | + throw e |
| 80 | + } finally { |
| 81 | + await connection.closeAsync() |
| 82 | + } |
| 83 | + return result |
| 84 | +} |
| 85 | + |
| 86 | +module.exports = { |
| 87 | + getMetadataEntry, |
| 88 | + createOrUpdateMetadata |
| 89 | +} |
0 commit comments