From f7763d7472be422e3c182245ea6f72ae38a61a3e Mon Sep 17 00:00:00 2001 From: Thomas Kranitsas Date: Wed, 9 Dec 2020 19:55:16 +0200 Subject: [PATCH 1/3] enable timeline notifications --- src/services/ProcessorService.js | 2 ++ src/services/timelineService.js | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/services/timelineService.js diff --git a/src/services/ProcessorService.js b/src/services/ProcessorService.js index 5dbe7c2..1040863 100644 --- a/src/services/ProcessorService.js +++ b/src/services/ProcessorService.js @@ -13,6 +13,7 @@ const constants = require('../constants') const groupService = require('./groupsService') const termsService = require('./termsService') const copilotPaymentService = require('./copilotPaymentService') +const timelineService = require('./timelineService') /** * Get group information by V5 UUID @@ -376,6 +377,7 @@ async function processCreate (message) { for (const resource of (challengeResourcesResponse.body || [])) { await helper.postBusEvent(config.RESOURCE_CREATE_TOPIC, _.pick(resource, ['id', 'challengeId', 'memberId', 'memberHandle', 'roleId', 'created', 'createdBy', 'updated', 'updatedBy', 'legacyId'])) } + await timelineService.enableTimelineNotifications(newChallenge.body.result.content.id, _.get(message, 'payload.createdBy')) logger.debug('End of processCreate') } catch (e) { logger.error('processCreate Catch', e) diff --git a/src/services/timelineService.js b/src/services/timelineService.js new file mode 100644 index 0000000..72510b7 --- /dev/null +++ b/src/services/timelineService.js @@ -0,0 +1,48 @@ +/** + * Timeline Service + * Interacts with InformixDB + */ +const logger = require('../common/logger') +const helper = require('../common/helper') + +const QUERY_ENABLE_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)' + +/** + * 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) +} + +/** + * Enable timeline notifications + * @param {Number} challengeLegacyId the legacy challenge ID + * @param {String} createdBy the created by + */ +async function enableTimelineNotifications (challengeLegacyId, createdBy) { + const connection = await helper.getInformixConnection() + let result = null + try { + // await connection.beginTransactionAsync() + const query = await prepare(connection, QUERY_ENABLE_TIMELINE_NOTIFICATIONS) + result = await query.executeAsync([challengeLegacyId, createdBy, createdBy]) + // await connection.commitTransactionAsync() + } catch (e) { + logger.error(`Error in 'enableTimelineNotifications' ${e}, rolling back transaction`) + await connection.rollbackTransactionAsync() + throw e + } finally { + logger.info(`Notifications have been enabled for challenge ${challengeLegacyId}`) + await connection.closeAsync() + } + return result +} + +module.exports = { + enableTimelineNotifications +} From a6280a3f9dc98dc821d6a516a7db9962126de96a Mon Sep 17 00:00:00 2001 From: Thomas Kranitsas Date: Wed, 9 Dec 2020 20:37:32 +0200 Subject: [PATCH 2/3] check if timeline notification entry exists before create --- src/services/timelineService.js | 37 +++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/services/timelineService.js b/src/services/timelineService.js index 72510b7..cb6804e 100644 --- a/src/services/timelineService.js +++ b/src/services/timelineService.js @@ -2,10 +2,13 @@ * Timeline Service * Interacts with InformixDB */ +const util = require('util') const logger = require('../common/logger') const helper = require('../common/helper') -const QUERY_ENABLE_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)' +const QUERY_GET_TIMELINE_NOTIFICATION_SETTINGS = 'SELECT value FROM project_info WHERE project_id = %d and project_info_type_id = %d' +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)' +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 = ?' /** * Prepare Informix statement @@ -19,6 +22,25 @@ async function prepare (connection, sql) { return Promise.promisifyAll(stmt) } +/** + * Get teh timeline notification settings entry + * @param {Number} challengeLegacyId the legacy challenge ID + */ +async function getTimelineNotifications (challengeLegacyId) { + // logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`) + const connection = await helper.getInformixConnection() + let result = null + try { + result = await connection.queryAsync(util.format(QUERY_GET_TIMELINE_NOTIFICATION_SETTINGS, challengeLegacyId, 11)) + } catch (e) { + logger.error(`Error in 'getTermsForChallenge' ${e}`) + throw e + } finally { + await connection.closeAsync() + } + return result +} + /** * Enable timeline notifications * @param {Number} challengeLegacyId the legacy challenge ID @@ -29,20 +51,27 @@ async function enableTimelineNotifications (challengeLegacyId, createdBy) { let result = null try { // await connection.beginTransactionAsync() - const query = await prepare(connection, QUERY_ENABLE_TIMELINE_NOTIFICATIONS) - result = await query.executeAsync([challengeLegacyId, createdBy, createdBy]) + const [existing] = await getTimelineNotifications(challengeLegacyId) + if (existing) { + const query = await prepare(connection, QUERY_UPDATE_TIMELINE_NOTIFICATIONS) + result = await query.executeAsync([createdBy, challengeLegacyId]) + } else { + const query = await prepare(connection, QUERY_CREATE_TIMELINE_NOTIFICATIONS) + result = await query.executeAsync([challengeLegacyId, createdBy, createdBy]) + } // await connection.commitTransactionAsync() + logger.info(`Notifications have been enabled for challenge ${challengeLegacyId}`) } catch (e) { logger.error(`Error in 'enableTimelineNotifications' ${e}, rolling back transaction`) await connection.rollbackTransactionAsync() throw e } finally { - logger.info(`Notifications have been enabled for challenge ${challengeLegacyId}`) await connection.closeAsync() } return result } module.exports = { + getTimelineNotifications, enableTimelineNotifications } From 179d797cfb99c894d948d62a15eef52aa31d91d9 Mon Sep 17 00:00:00 2001 From: James Cori Date: Wed, 9 Dec 2020 17:23:08 -0500 Subject: [PATCH 3/3] Turning the create listener back on So tools can create draft challenges off the bat and get created in legacy --- src/app.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app.js b/src/app.js index eb9514d..78be71e 100644 --- a/src/app.js +++ b/src/app.js @@ -64,8 +64,7 @@ const dataHandler = (messageSet, topic, partition) => Promise.each(messageSet, a try { if (topic === config.CREATE_CHALLENGE_TOPIC) { - // create shut off. Only works with challenges of status draft, which happens on update - // await ProcessorService.processCreate(messageJSON) + await ProcessorService.processCreate(messageJSON) } else { await ProcessorService.processUpdate(messageJSON) }