Skip to content

Commit c66f542

Browse files
authored
Merge pull request topcoder-archive#145 from topcoder-platform/fix/updateDate
fix: sync updated and updatedBy fields
2 parents db80f38 + 91cf54c commit c66f542

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

src/common/helper.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const _ = require('lodash')
66
const config = require('config')
7+
const momentTZ = require('moment-timezone')
78
const ifxnjs = require('ifxnjs')
89
const request = require('superagent')
910
const m2mAuth = require('tc-core-library-js').auth.m2m
@@ -190,6 +191,19 @@ async function getMemberIdByHandle (handle) {
190191
return memberId
191192
}
192193

194+
/**
195+
* Formats a date into a format supported by ifx
196+
* @param {String} dateStr the date in string format
197+
*/
198+
function formatDate (dateStr) {
199+
if (!dateStr) {
200+
return null
201+
}
202+
const date = momentTZ.tz(dateStr, config.TIMEZONE).format('YYYY-MM-DD HH:mm:ss')
203+
logger.info(`Formatting date ${dateStr} New Date ${date}`)
204+
return date
205+
}
206+
193207
module.exports = {
194208
getInformixConnection,
195209
getKafkaOptions,
@@ -200,5 +214,6 @@ module.exports = {
200214
postRequest,
201215
postBusEvent,
202216
forceV4ESFeeder,
203-
getMemberIdByHandle
217+
getMemberIdByHandle,
218+
formatDate
204219
}

src/services/ProcessorService.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ async function processMessage (message) {
760760

761761
const createdByUserHandle = _.get(message, 'payload.createdBy')
762762
const updatedByUserHandle = _.get(message, 'payload.updatedBy')
763+
const updatedAt = _.get(message, 'payload.updated', new Date().toISOString())
763764

764765
const createdByUserId = await helper.getMemberIdByHandle(createdByUserHandle)
765766
let updatedByUserId = createdByUserId
@@ -887,9 +888,12 @@ async function processMessage (message) {
887888
}
888889
if (message.payload.status === constants.challengeStatuses.CancelledClientRequest && challenge.currentStatus !== constants.challengeStatuses.CancelledClientRequest) {
889890
logger.info('Cancelling challenge...')
890-
await legacyChallengeService.cancelChallenge(legacyId, updatedByUserId)
891+
await legacyChallengeService.cancelChallenge(legacyId, updatedByUserId, updatedAt)
891892
needSyncV4ES = true
893+
} else {
894+
legacyChallengeService.updateChallengeAudit(legacyId, updatedByUserId, updatedAt)
892895
}
896+
893897
if (needSyncV4ES) {
894898
try {
895899
logger.info(`Resync V4 ES for the legacy challenge ${legacyId}`)

src/services/legacyChallengeService.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const util = require('util')
77
const helper = require('../common/helper')
88
const { createChallengeStatusesMap } = require('../constants')
99

10-
const QUERY_UPDATE_PROJECT = 'UPDATE project SET project_status_id = ?, modify_user = ? WHERE project_id = %d'
10+
const QUERY_UPDATE_PROJECT = 'UPDATE project SET project_status_id = ?, modify_user = ?, modify_date = ? WHERE project_id = %d'
11+
const QUERY_UPDATE_PROJECT_AUDIT = 'UPDATE project SET modify_user = ?, modify_date = ? WHERE project_id = %d'
1112

1213
/**
1314
* Prepare Informix statement
@@ -24,15 +25,16 @@ async function prepare (connection, sql) {
2425
/**
2526
* Update a challenge in IFX
2627
* @param {Number} challengeLegacyId the legacy challenge ID
27-
* @param {Number} createdBy the creator user ID
28+
* @param {Number} updatedBy the user ID
29+
* @param {String} updatedAt the challenge modified time
2830
*/
29-
async function cancelChallenge (challengeLegacyId, createdBy) {
31+
async function cancelChallenge (challengeLegacyId, updatedBy, updatedAt) {
3032
const connection = await helper.getInformixConnection()
3133
let result = null
3234
try {
3335
await connection.beginTransactionAsync()
3436
const query = await prepare(connection, util.format(QUERY_UPDATE_PROJECT, challengeLegacyId))
35-
result = await query.executeAsync([createChallengeStatusesMap.CancelledClientRequest, createdBy])
37+
result = await query.executeAsync([createChallengeStatusesMap.CancelledClientRequest, updatedBy, helper.formatDate(updatedAt)])
3638
await connection.commitTransactionAsync()
3739
} catch (e) {
3840
logger.error(`Error in 'cancelChallenge' ${e}, rolling back transaction`)
@@ -45,6 +47,29 @@ async function cancelChallenge (challengeLegacyId, createdBy) {
4547
return result
4648
}
4749

50+
/**
51+
* Update a challenge audit fields in IFX
52+
* @param {Number} challengeLegacyId the legacy challenge ID
53+
* @param {Number} updatedBy the user ID
54+
* @param {String} updatedAt the challenge modified time
55+
*/
56+
async function updateChallengeAudit (challengeLegacyId, updatedBy, updatedAt) {
57+
const connection = await helper.getInformixConnection()
58+
let result = null
59+
try {
60+
const query = await prepare(connection, util.format(QUERY_UPDATE_PROJECT_AUDIT, challengeLegacyId))
61+
result = await query.executeAsync([updatedBy, helper.formatDate(updatedAt)])
62+
} catch (e) {
63+
logger.error(`Error in 'updateChallengeAudit' ${e}`)
64+
throw e
65+
} finally {
66+
logger.info(`Challenge audit for ${challengeLegacyId} has been updated`)
67+
await connection.closeAsync()
68+
}
69+
return result
70+
}
71+
4872
module.exports = {
49-
cancelChallenge
73+
cancelChallenge,
74+
updateChallengeAudit
5075
}

src/services/timelineService.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const _ = require('lodash')
77
const logger = require('../common/logger')
88
const util = require('util')
99
const config = require('config')
10-
const momentTZ = require('moment-timezone')
1110
const IDGenerator = require('../common/idGenerator')
1211
const helper = require('../common/helper')
1312

@@ -33,19 +32,6 @@ const QUERY_GET_PROJECT_PHASE_ID = 'SELECT project_phase_id as project_phase_id
3332

3433
const QUERY_INSERT_CHALLENGE_PHASE_SCORECARD_ID = 'INSERT INTO phase_criteria (project_phase_id, phase_criteria_type_id, parameter, create_user, create_date, modify_user, modify_date) VALUES (?, 1, ?, ?, CURRENT, ?, CURRENT)'
3534

36-
/**
37-
* Formats a date into a format supported by ifx
38-
* @param {String} dateStr the date in string format
39-
*/
40-
function formatDate (dateStr) {
41-
if (!dateStr) {
42-
return null
43-
}
44-
const date = momentTZ.tz(dateStr, config.TIMEZONE).format('YYYY-MM-DD HH:mm:ss')
45-
logger.info(`Formatting date ${dateStr} New Date ${date}`)
46-
return date
47-
}
48-
4935
/**
5036
* Prepare Informix statement
5137
* @param {Object} connection the Informix connection
@@ -198,8 +184,8 @@ async function createPhase (challengeLegacyId, phaseTypeId, statusTypeId, schedu
198184
challengeLegacyId,
199185
phaseTypeId,
200186
statusTypeId,
201-
formatDate(scheduledStartDate),
202-
formatDate(scheduledEndDate),
187+
helper.formatDate(scheduledStartDate),
188+
helper.formatDate(scheduledEndDate),
203189
duration,
204190
createdBy,
205191
createdBy
@@ -209,8 +195,8 @@ async function createPhase (challengeLegacyId, phaseTypeId, statusTypeId, schedu
209195
challengeLegacyId,
210196
phaseTypeId,
211197
statusTypeId,
212-
formatDate(scheduledStartDate),
213-
formatDate(scheduledEndDate),
198+
helper.formatDate(scheduledStartDate),
199+
helper.formatDate(scheduledEndDate),
214200
duration,
215201
createdBy,
216202
createdBy
@@ -248,8 +234,8 @@ async function updatePhase (phaseId, challengeLegacyId, fixedStartTime, startTim
248234
await prepare(connection, util.format(QUERY_UPDATE_CHALLENGE_PHASE_WITH_START_TIME, phaseId, challengeLegacyId))
249235

250236
result = actualStartTime == null ?
251-
await query.executeAsync([formatDate(fixedStartTime), formatDate(startTime), formatDate(endTime), duration, statusTypeId]) :
252-
await query.executeAsync([formatDate(fixedStartTime), formatDate(startTime), formatDate(endTime), duration, statusTypeId, formatDate(actualStartTime)])
237+
await query.executeAsync([helper.formatDate(fixedStartTime), helper.formatDate(startTime), helper.formatDate(endTime), duration, statusTypeId]) :
238+
await query.executeAsync([helper.formatDate(fixedStartTime), helper.formatDate(startTime), helper.formatDate(endTime), duration, statusTypeId, helper.formatDate(actualStartTime)])
253239

254240
// await connection.commitTransactionAsync()
255241
} catch (e) {

0 commit comments

Comments
 (0)