Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit ff311ac

Browse files
authored
Merge pull request #30 from topcoder-platform/fix-timline-notif
check if timeline notification entry exists before create
2 parents 0b50a0b + a6280a3 commit ff311ac

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/services/timelineService.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
* Timeline Service
33
* Interacts with InformixDB
44
*/
5+
const util = require('util')
56
const logger = require('../common/logger')
67
const helper = require('../common/helper')
78

8-
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)'
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 = ?'
912

1013
/**
1114
* Prepare Informix statement
@@ -19,6 +22,25 @@ async function prepare (connection, sql) {
1922
return Promise.promisifyAll(stmt)
2023
}
2124

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+
2244
/**
2345
* Enable timeline notifications
2446
* @param {Number} challengeLegacyId the legacy challenge ID
@@ -29,20 +51,27 @@ async function enableTimelineNotifications (challengeLegacyId, createdBy) {
2951
let result = null
3052
try {
3153
// await connection.beginTransactionAsync()
32-
const query = await prepare(connection, QUERY_ENABLE_TIMELINE_NOTIFICATIONS)
33-
result = await query.executeAsync([challengeLegacyId, createdBy, createdBy])
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+
}
3462
// await connection.commitTransactionAsync()
63+
logger.info(`Notifications have been enabled for challenge ${challengeLegacyId}`)
3564
} catch (e) {
3665
logger.error(`Error in 'enableTimelineNotifications' ${e}, rolling back transaction`)
3766
await connection.rollbackTransactionAsync()
3867
throw e
3968
} finally {
40-
logger.info(`Notifications have been enabled for challenge ${challengeLegacyId}`)
4169
await connection.closeAsync()
4270
}
4371
return result
4472
}
4573

4674
module.exports = {
75+
getTimelineNotifications,
4776
enableTimelineNotifications
4877
}

0 commit comments

Comments
 (0)