|
| 1 | +/** |
| 2 | + * Timeline 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_TIMELINE_NOTIFICATION_SETTINGS = 'SELECT value FROM project_info WHERE project_id = %d and project_info_type_id = %d' |
| 10 | +const QUERY_CREATE_TIMELINE_NOTIFICATIONS = 'INSERT INTO project_info (project_id, project_info_type_id, value, create_user, create_date, modify_user, modify_date) VALUES (?, "11", "On", ?, CURRENT, ?, CURRENT)' |
| 11 | +const QUERY_UPDATE_TIMELINE_NOTIFICATIONS = 'UPDATE project_info SET value = "On", modify_user = ?, modify_date = CURRENT WHERE project_info_type_id = "11" AND project_id = ?' |
| 12 | + |
| 13 | +/** |
| 14 | + * Prepare Informix statement |
| 15 | + * @param {Object} connection the Informix connection |
| 16 | + * @param {String} sql the sql |
| 17 | + * @return {Object} Informix statement |
| 18 | + */ |
| 19 | +async function prepare (connection, sql) { |
| 20 | + // logger.debug(`Preparing SQL ${sql}`) |
| 21 | + const stmt = await connection.prepareAsync(sql) |
| 22 | + return Promise.promisifyAll(stmt) |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Get teh timeline notification settings entry |
| 27 | + * @param {Number} challengeLegacyId the legacy challenge ID |
| 28 | + */ |
| 29 | +async function getTimelineNotifications (challengeLegacyId) { |
| 30 | + // logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`) |
| 31 | + const connection = await helper.getInformixConnection() |
| 32 | + let result = null |
| 33 | + try { |
| 34 | + result = await connection.queryAsync(util.format(QUERY_GET_TIMELINE_NOTIFICATION_SETTINGS, challengeLegacyId, 11)) |
| 35 | + } catch (e) { |
| 36 | + logger.error(`Error in 'getTermsForChallenge' ${e}`) |
| 37 | + throw e |
| 38 | + } finally { |
| 39 | + await connection.closeAsync() |
| 40 | + } |
| 41 | + return result |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Enable timeline notifications |
| 46 | + * @param {Number} challengeLegacyId the legacy challenge ID |
| 47 | + * @param {String} createdBy the created by |
| 48 | + */ |
| 49 | +async function enableTimelineNotifications (challengeLegacyId, createdBy) { |
| 50 | + const connection = await helper.getInformixConnection() |
| 51 | + let result = null |
| 52 | + try { |
| 53 | + // await connection.beginTransactionAsync() |
| 54 | + const [existing] = await getTimelineNotifications(challengeLegacyId) |
| 55 | + if (existing) { |
| 56 | + const query = await prepare(connection, QUERY_UPDATE_TIMELINE_NOTIFICATIONS) |
| 57 | + result = await query.executeAsync([createdBy, challengeLegacyId]) |
| 58 | + } else { |
| 59 | + const query = await prepare(connection, QUERY_CREATE_TIMELINE_NOTIFICATIONS) |
| 60 | + result = await query.executeAsync([challengeLegacyId, createdBy, createdBy]) |
| 61 | + } |
| 62 | + // await connection.commitTransactionAsync() |
| 63 | + logger.info(`Notifications have been enabled for challenge ${challengeLegacyId}`) |
| 64 | + } catch (e) { |
| 65 | + logger.error(`Error in 'enableTimelineNotifications' ${e}, rolling back transaction`) |
| 66 | + await connection.rollbackTransactionAsync() |
| 67 | + throw e |
| 68 | + } finally { |
| 69 | + await connection.closeAsync() |
| 70 | + } |
| 71 | + return result |
| 72 | +} |
| 73 | + |
| 74 | +module.exports = { |
| 75 | + getTimelineNotifications, |
| 76 | + enableTimelineNotifications |
| 77 | +} |
0 commit comments