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

Commit b67d1d4

Browse files
authored
Merge pull request #32 from topcoder-platform/fix-wm-974
update metadata in ifx
2 parents 179d797 + bce1070 commit b67d1d4

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

src/constants.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,20 @@ const challengeStatuses = {
4545
CancelledZeroRegistrations: 'Cancelled - Zero Registrations'
4646
}
4747

48+
const supportedMetadata = {
49+
allowStockArt: 52,
50+
drPoints: 30,
51+
submissionViewable: 53,
52+
submissionLimit: 51,
53+
codeRepo: 85,
54+
environment: 84
55+
}
56+
4857
module.exports = {
4958
prizeSetTypes,
5059
EVENT_ORIGINATOR,
5160
EVENT_MIME_TYPE,
5261
createChallengeStatusesMap,
53-
challengeStatuses
62+
challengeStatuses,
63+
supportedMetadata
5464
}

src/services/ProcessorService.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const groupService = require('./groupsService')
1414
const termsService = require('./termsService')
1515
const copilotPaymentService = require('./copilotPaymentService')
1616
const timelineService = require('./timelineService')
17+
const metadataService = require('./metadataService')
1718

1819
/**
1920
* Get group information by V5 UUID
@@ -306,6 +307,21 @@ async function parsePayload (payload, m2mToken, isCreated = true, informixGroupI
306307
data.groupsToBeDeleted = _.map(informixGroupIds, g => _.toString(g))
307308
}
308309

310+
if (payload.metadata && payload.metadata.length > 0) {
311+
const fileTypes = _.find(payload.metadata, meta => meta.name === 'fileTypes')
312+
if (fileTypes) {
313+
if (_.isArray(fileTypes.value)) {
314+
data.fileTypes = fileTypes.value
315+
} else {
316+
try {
317+
data.fileTypes = JSON.parse(fileTypes.value)
318+
} catch (e) {
319+
data.fileTypes = []
320+
}
321+
}
322+
}
323+
}
324+
309325
return data
310326
} catch (err) {
311327
// Debugging
@@ -486,6 +502,15 @@ async function processUpdate (message) {
486502
await associateChallengeTerms(message.payload.terms, message.payload.legacyId, _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))
487503
await setCopilotPayment(message.payload.legacyId, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))
488504

505+
// Update metadata in IFX
506+
if (message.payload.metadata && message.payload.metadata.length > 0) {
507+
for (const metadataKey of _.keys(constants.supportedMetadata)) {
508+
const entry = _.find(message.payload.metadata, meta => meta.name === metadataKey)
509+
if (entry) {
510+
await metadataService.createOrUpdateMetadata(message.payload.legacyId, constants.supportedMetadata[metadataKey], entry.value, _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))
511+
}
512+
}
513+
}
489514
if (message.payload.status) {
490515
// logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`)
491516
if (message.payload.status === constants.challengeStatuses.Active && challenge.currentStatus !== constants.challengeStatuses.Active) {

src/services/metadataService.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Metadata 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_ENTRY = 'SELECT value FROM project_info WHERE project_id = %d and project_info_type_id = %d'
10+
const QUERY_CREATE = 'INSERT INTO project_info (project_id, project_info_type_id, value, create_user, create_date, modify_user, modify_date) VALUES (?, ?, ?, ?, CURRENT, ?, CURRENT)'
11+
const QUERY_UPDATE = 'UPDATE project_info SET value = ?, modify_user = ?, modify_date = CURRENT WHERE project_info_type_id = ? 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 project info entry entry
27+
* @param {Number} challengeLegacyId the legacy challenge ID
28+
* @param {Number} typeId the type ID
29+
*/
30+
async function getMetadataEntry (challengeLegacyId, typeId) {
31+
// logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`)
32+
const connection = await helper.getInformixConnection()
33+
let result = null
34+
try {
35+
result = await connection.queryAsync(util.format(QUERY_GET_ENTRY, challengeLegacyId, 11))
36+
} catch (e) {
37+
logger.error(`Error in 'getMetadataEntry' ${e}`)
38+
throw e
39+
} finally {
40+
await connection.closeAsync()
41+
}
42+
return result
43+
}
44+
45+
/**
46+
* Enable timeline notifications
47+
* @param {Number} challengeLegacyId the legacy challenge ID
48+
* @param {Number} typeId the type ID
49+
* @param {Any} value the value
50+
* @param {String} createdBy the created by
51+
*/
52+
async function createOrUpdateMetadata (challengeLegacyId, typeId, value, createdBy) {
53+
const connection = await helper.getInformixConnection()
54+
let result = null
55+
try {
56+
// await connection.beginTransactionAsync()
57+
const [existing] = await getMetadataEntry(challengeLegacyId, typeId)
58+
if (existing) {
59+
const query = await prepare(connection, QUERY_UPDATE)
60+
result = await query.executeAsync([value, createdBy, typeId, challengeLegacyId])
61+
} else {
62+
const query = await prepare(connection, QUERY_CREATE)
63+
result = await query.executeAsync([challengeLegacyId, typeId, value, createdBy, createdBy])
64+
}
65+
// await connection.commitTransactionAsync()
66+
logger.info(`Metadata with typeId ${typeId} has been enabled for challenge ${challengeLegacyId}`)
67+
} catch (e) {
68+
logger.error(`Error in 'createOrUpdateMetadata' ${e}, rolling back transaction`)
69+
await connection.rollbackTransactionAsync()
70+
throw e
71+
} finally {
72+
await connection.closeAsync()
73+
}
74+
return result
75+
}
76+
77+
module.exports = {
78+
getMetadataEntry,
79+
createOrUpdateMetadata
80+
}

0 commit comments

Comments
 (0)