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

update metadata in ifx #32

Merged
merged 1 commit into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
25 changes: 25 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 @@ -306,6 +307,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 @@ -486,6 +502,15 @@ async function processUpdate (message) {
await associateChallengeTerms(message.payload.terms, message.payload.legacyId, _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))
await setCopilotPayment(message.payload.legacyId, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy'))

// 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) {
await metadataService.createOrUpdateMetadata(message.payload.legacyId, constants.supportedMetadata[metadataKey], entry.value, _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))
}
}
}
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
80 changes: 80 additions & 0 deletions src/services/metadataService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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 = ?'

/**
* 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, 11))
} 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) {
const query = await prepare(connection, QUERY_UPDATE)
result = await query.executeAsync([value, createdBy, typeId, challengeLegacyId])
} else {
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
}