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

Fix wm 974 #34

Merged
merged 14 commits into from
Dec 17, 2020
Merged
12 changes: 11 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,20 @@ const challengeStatuses = {
CancelledZeroRegistrations: 'Cancelled - Zero Registrations'
}

const supportedMetadata = {
allowStockArt: 52,
drPoints: 30,
submissionViewable: 53,
submissionLimit: 51,
codeRepo: 85,
environment: 84
}

module.exports = {
prizeSetTypes,
EVENT_ORIGINATOR,
EVENT_MIME_TYPE,
createChallengeStatusesMap,
challengeStatuses
challengeStatuses,
supportedMetadata
}
42 changes: 42 additions & 0 deletions src/services/ProcessorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const groupService = require('./groupsService')
const termsService = require('./termsService')
const copilotPaymentService = require('./copilotPaymentService')
const timelineService = require('./timelineService')
const metadataService = require('./metadataService')

/**
* Get group information by V5 UUID
Expand Down Expand Up @@ -336,6 +337,21 @@ async function parsePayload (payload, m2mToken, isCreated = true, informixGroupI
data.groupsToBeDeleted = _.map(informixGroupIds, g => _.toString(g))
}

if (payload.metadata && payload.metadata.length > 0) {
const fileTypes = _.find(payload.metadata, meta => meta.name === 'fileTypes')
if (fileTypes) {
if (_.isArray(fileTypes.value)) {
data.fileTypes = fileTypes.value
} else {
try {
data.fileTypes = JSON.parse(fileTypes.value)
} catch (e) {
data.fileTypes = []
}
}
}
}

return data
} catch (err) {
// Debugging
Expand Down Expand Up @@ -522,6 +538,32 @@ async function processUpdate (message) {
await associateChallengeTerms(message.payload.terms, message.payload.legacyId, _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))
await setCopilotPayment(message.payload.id, message.payload.legacyId, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'), m2mToken)

// Update metadata in IFX
if (message.payload.metadata && message.payload.metadata.length > 0) {
for (const metadataKey of _.keys(constants.supportedMetadata)) {
const entry = _.find(message.payload.metadata, meta => meta.name === metadataKey)
if (entry) {
if (metadataKey === 'submissionLimit') {
// data here is JSON stringified
try {
const parsedEntryValue = JSON.parse(entry.value)
if (parsedEntryValue.limit) {
entry.value = parsedEntryValue.count
} else {
entry.value = null
}
} catch (e) {
entry.value = null
}
}
try {
await metadataService.createOrUpdateMetadata(message.payload.legacyId, constants.supportedMetadata[metadataKey], entry.value, _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))
} catch (e) {
logger.warn(`Failed to set ${metadataKey} (${constants.supportedMetadata[metadataKey]})`)
}
}
}
}
if (message.payload.status) {
// logger.info(`The status has changed from ${challenge.currentStatus} to ${message.payload.status}`)
if (message.payload.status === constants.challengeStatuses.Active && challenge.currentStatus !== constants.challengeStatuses.Active) {
Expand Down
89 changes: 89 additions & 0 deletions src/services/metadataService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Metadata Service
* Interacts with InformixDB
*/
const util = require('util')
const logger = require('../common/logger')
const helper = require('../common/helper')

const QUERY_GET_ENTRY = 'SELECT value FROM project_info WHERE project_id = %d and project_info_type_id = %d'
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)'
const QUERY_UPDATE = 'UPDATE project_info SET value = ?, modify_user = ?, modify_date = CURRENT WHERE project_info_type_id = ? AND project_id = ?'
const QUERY_DELETE = 'DELETE FROM project_info WHERE project_id = ? and project_info_type_id = ?'

/**
* Prepare Informix statement
* @param {Object} connection the Informix connection
* @param {String} sql the sql
* @return {Object} Informix statement
*/
async function prepare (connection, sql) {
// logger.debug(`Preparing SQL ${sql}`)
const stmt = await connection.prepareAsync(sql)
return Promise.promisifyAll(stmt)
}

/**
* Get project info entry entry
* @param {Number} challengeLegacyId the legacy challenge ID
* @param {Number} typeId the type ID
*/
async function getMetadataEntry (challengeLegacyId, typeId) {
// logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`)
const connection = await helper.getInformixConnection()
let result = null
try {
result = await connection.queryAsync(util.format(QUERY_GET_ENTRY, challengeLegacyId, typeId))
} catch (e) {
logger.error(`Error in 'getMetadataEntry' ${e}`)
throw e
} finally {
await connection.closeAsync()
}
return result
}

/**
* Enable timeline notifications
* @param {Number} challengeLegacyId the legacy challenge ID
* @param {Number} typeId the type ID
* @param {Any} value the value
* @param {String} createdBy the created by
*/
async function createOrUpdateMetadata (challengeLegacyId, typeId, value, createdBy) {
const connection = await helper.getInformixConnection()
let result = null
try {
// await connection.beginTransactionAsync()
const [existing] = await getMetadataEntry(challengeLegacyId, typeId)
if (existing) {
if (value) {
logger.info(`Metadata ${typeId} exists. Will update`)
const query = await prepare(connection, QUERY_UPDATE)
result = await query.executeAsync([value, createdBy, typeId, challengeLegacyId])
} else {
logger.info(`Metadata ${typeId} exists. Will delete`)
const query = await prepare(connection, QUERY_DELETE)
result = await query.executeAsync([challengeLegacyId, typeId])
}
} else {
logger.info(`Metadata ${typeId} does not exist. Will create`)
const query = await prepare(connection, QUERY_CREATE)
result = await query.executeAsync([challengeLegacyId, typeId, value, createdBy, createdBy])
}
// await connection.commitTransactionAsync()
logger.info(`Metadata with typeId ${typeId} has been enabled for challenge ${challengeLegacyId}`)
} catch (e) {
logger.error(`Error in 'createOrUpdateMetadata' ${e}, rolling back transaction`)
await connection.rollbackTransactionAsync()
throw e
} finally {
await connection.closeAsync()
}
return result
}

module.exports = {
getMetadataEntry,
createOrUpdateMetadata
}