Skip to content

Commit d1d43ac

Browse files
author
James Cori
committed
Merge branch 'develop'
2 parents 4167db9 + 4e38270 commit d1d43ac

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

config/default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module.exports = {
2323

2424
SUBMITTER_ROLE_ID: process.env.SUBMITTER_ROLE_ID || '732339e7-8e30-49d7-9198-cccf9451e221',
2525

26+
RESOURCE_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS: process.env.RESOURCE_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS ? process.env.RESOURCE_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS.split(',') : ['2a4dc376-a31c-4d00-b173-13934d89e286'],
27+
2628
IS_CREATE_FORUM: process.env.IS_CREATE_FORUM || true,
2729

2830
CREATE_CHALLENGE_RESOURCE_TOPIC: process.env.CREATE_CHALLENGE_RESOURCE_TOPIC || 'challenge.action.resource.create',

src/services/NotificationService.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Notification 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_ENTRY = 'SELECT external_ref_id FROM notification WHERE project_id = %d and external_ref_id = %d and notification_type_id = %d'
10+
const QUERY_CREATE_TIMELINE_NOTIFICATION_ENTRY = 'INSERT INTO notification (project_id, external_ref_id, notification_type_id, create_user, create_date, modify_user, modify_date) VALUES (?, ?, "1", ?, CURRENT, ?, CURRENT)'
11+
const QUERY_DELETE_TIMELINE_NOTIFICATION_ENTRY = 'DELETE FROM notification WHERE project_id = ? and external_ref_id = ? and notification_type_id = "1"'
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, memberId) {
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_ENTRY, challengeLegacyId, memberId, 1))
35+
} catch (e) {
36+
logger.error(`Error in 'getTimelineNotifications' ${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 {Number} memberId the user ID
48+
* @param {String} createdBy the created by
49+
*/
50+
async function enableTimelineNotifications (challengeLegacyId, memberId, createdBy) {
51+
const connection = await helper.getInformixConnection()
52+
let result = null
53+
try {
54+
// await connection.beginTransactionAsync()
55+
const [existing] = await getTimelineNotifications(challengeLegacyId, memberId)
56+
if (!existing) {
57+
const query = await prepare(connection, QUERY_CREATE_TIMELINE_NOTIFICATION_ENTRY)
58+
result = await query.executeAsync([challengeLegacyId, memberId, createdBy, createdBy])
59+
logger.info(`Notifications have been enabled for challenge ${challengeLegacyId} and user ${memberId}`)
60+
}
61+
// await connection.commitTransactionAsync()
62+
} catch (e) {
63+
logger.error(`Error in 'enableTimelineNotifications' ${e}, rolling back transaction`)
64+
await connection.rollbackTransactionAsync()
65+
throw e
66+
} finally {
67+
await connection.closeAsync()
68+
}
69+
return result
70+
}
71+
72+
/**
73+
* Disable timeline notifications
74+
* @param {Number} challengeLegacyId the legacy challenge ID
75+
* @param {Number} memberId the user ID
76+
*/
77+
async function disableTimelineNotifications (challengeLegacyId, memberId) {
78+
const connection = await helper.getInformixConnection()
79+
let result = null
80+
try {
81+
// await connection.beginTransactionAsync()
82+
const [existing] = await getTimelineNotifications(challengeLegacyId, memberId)
83+
if (existing) {
84+
const query = await prepare(connection, QUERY_DELETE_TIMELINE_NOTIFICATION_ENTRY)
85+
result = await query.executeAsync([challengeLegacyId, memberId])
86+
logger.info(`Notifications have been disabled for challenge ${challengeLegacyId} and user ${memberId}`)
87+
}
88+
// await connection.commitTransactionAsync()
89+
} catch (e) {
90+
logger.error(`Error in 'disableTimelineNotifications' ${e}, rolling back transaction`)
91+
await connection.rollbackTransactionAsync()
92+
throw e
93+
} finally {
94+
await connection.closeAsync()
95+
}
96+
return result
97+
}
98+
99+
module.exports = {
100+
getTimelineNotifications,
101+
enableTimelineNotifications,
102+
disableTimelineNotifications
103+
}

src/services/ProcessorService.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const logger = require('../common/logger')
88
const helper = require('../common/helper')
99
const ResourceDirectManager = require('./ResourceDirectManager')
1010
const ProjectServices = require('./ProjectService')
11+
const notificationService = require('./NotificationService')
1112
const {isStudio} = require('../common/utils')
1213

1314
/**
@@ -113,6 +114,13 @@ async function _updateChallengeResource (message, isDelete) {
113114
await ResourceDirectManager.addResource(legacyChallengeID, resourceRoleId, userId, handle, copilotPaymentAmount)
114115
}
115116
}
117+
if (config.RESOURCE_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS.indexOf(resourceRole.id) === -1) {
118+
if (isDelete) {
119+
await notificationService.disableTimelineNotifications(_.get(v5Challenge, 'legacyId'), userId)
120+
} else {
121+
await notificationService.enableTimelineNotifications(_.get(v5Challenge, 'legacyId'), userId, _.get(v5Challenge, 'updatedBy') || _.get(v5Challenge, 'createdBy'))
122+
}
123+
}
116124
}
117125

118126
/**

0 commit comments

Comments
 (0)