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

Commit 06522fb

Browse files
Merge pull request #34 from topcoder-platform/fix-wm-974
Fix wm 974
2 parents 4c4dbc3 + 097bde0 commit 06522fb

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-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: 42 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
@@ -336,6 +337,21 @@ async function parsePayload (payload, m2mToken, isCreated = true, informixGroupI
336337
data.groupsToBeDeleted = _.map(informixGroupIds, g => _.toString(g))
337338
}
338339

340+
if (payload.metadata && payload.metadata.length > 0) {
341+
const fileTypes = _.find(payload.metadata, meta => meta.name === 'fileTypes')
342+
if (fileTypes) {
343+
if (_.isArray(fileTypes.value)) {
344+
data.fileTypes = fileTypes.value
345+
} else {
346+
try {
347+
data.fileTypes = JSON.parse(fileTypes.value)
348+
} catch (e) {
349+
data.fileTypes = []
350+
}
351+
}
352+
}
353+
}
354+
339355
return data
340356
} catch (err) {
341357
// Debugging
@@ -522,6 +538,32 @@ async function processUpdate (message) {
522538
await associateChallengeTerms(message.payload.terms, message.payload.legacyId, _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))
523539
await setCopilotPayment(message.payload.id, message.payload.legacyId, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'), m2mToken)
524540

541+
// Update metadata in IFX
542+
if (message.payload.metadata && message.payload.metadata.length > 0) {
543+
for (const metadataKey of _.keys(constants.supportedMetadata)) {
544+
const entry = _.find(message.payload.metadata, meta => meta.name === metadataKey)
545+
if (entry) {
546+
if (metadataKey === 'submissionLimit') {
547+
// data here is JSON stringified
548+
try {
549+
const parsedEntryValue = JSON.parse(entry.value)
550+
if (parsedEntryValue.limit) {
551+
entry.value = parsedEntryValue.count
552+
} else {
553+
entry.value = null
554+
}
555+
} catch (e) {
556+
entry.value = null
557+
}
558+
}
559+
try {
560+
await metadataService.createOrUpdateMetadata(message.payload.legacyId, constants.supportedMetadata[metadataKey], entry.value, _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))
561+
} catch (e) {
562+
logger.warn(`Failed to set ${metadataKey} (${constants.supportedMetadata[metadataKey]})`)
563+
}
564+
}
565+
}
566+
}
525567
if (message.payload.status) {
526568
// logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`)
527569
if (message.payload.status === constants.challengeStatuses.Active && challenge.currentStatus !== constants.challengeStatuses.Active) {

src/services/metadataService.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
const QUERY_DELETE = 'DELETE FROM project_info WHERE project_id = ? and project_info_type_id = ?'
13+
14+
/**
15+
* Prepare Informix statement
16+
* @param {Object} connection the Informix connection
17+
* @param {String} sql the sql
18+
* @return {Object} Informix statement
19+
*/
20+
async function prepare (connection, sql) {
21+
// logger.debug(`Preparing SQL ${sql}`)
22+
const stmt = await connection.prepareAsync(sql)
23+
return Promise.promisifyAll(stmt)
24+
}
25+
26+
/**
27+
* Get project info entry entry
28+
* @param {Number} challengeLegacyId the legacy challenge ID
29+
* @param {Number} typeId the type ID
30+
*/
31+
async function getMetadataEntry (challengeLegacyId, typeId) {
32+
// logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`)
33+
const connection = await helper.getInformixConnection()
34+
let result = null
35+
try {
36+
result = await connection.queryAsync(util.format(QUERY_GET_ENTRY, challengeLegacyId, typeId))
37+
} catch (e) {
38+
logger.error(`Error in 'getMetadataEntry' ${e}`)
39+
throw e
40+
} finally {
41+
await connection.closeAsync()
42+
}
43+
return result
44+
}
45+
46+
/**
47+
* Enable timeline notifications
48+
* @param {Number} challengeLegacyId the legacy challenge ID
49+
* @param {Number} typeId the type ID
50+
* @param {Any} value the value
51+
* @param {String} createdBy the created by
52+
*/
53+
async function createOrUpdateMetadata (challengeLegacyId, typeId, value, createdBy) {
54+
const connection = await helper.getInformixConnection()
55+
let result = null
56+
try {
57+
// await connection.beginTransactionAsync()
58+
const [existing] = await getMetadataEntry(challengeLegacyId, typeId)
59+
if (existing) {
60+
if (value) {
61+
logger.info(`Metadata ${typeId} exists. Will update`)
62+
const query = await prepare(connection, QUERY_UPDATE)
63+
result = await query.executeAsync([value, createdBy, typeId, challengeLegacyId])
64+
} else {
65+
logger.info(`Metadata ${typeId} exists. Will delete`)
66+
const query = await prepare(connection, QUERY_DELETE)
67+
result = await query.executeAsync([challengeLegacyId, typeId])
68+
}
69+
} else {
70+
logger.info(`Metadata ${typeId} does not exist. Will create`)
71+
const query = await prepare(connection, QUERY_CREATE)
72+
result = await query.executeAsync([challengeLegacyId, typeId, value, createdBy, createdBy])
73+
}
74+
// await connection.commitTransactionAsync()
75+
logger.info(`Metadata with typeId ${typeId} has been enabled for challenge ${challengeLegacyId}`)
76+
} catch (e) {
77+
logger.error(`Error in 'createOrUpdateMetadata' ${e}, rolling back transaction`)
78+
await connection.rollbackTransactionAsync()
79+
throw e
80+
} finally {
81+
await connection.closeAsync()
82+
}
83+
return result
84+
}
85+
86+
module.exports = {
87+
getMetadataEntry,
88+
createOrUpdateMetadata
89+
}

0 commit comments

Comments
 (0)