From bce10706bdc44024424f7bf3b8eb88c5f8573c96 Mon Sep 17 00:00:00 2001 From: Thomas Kranitsas Date: Tue, 15 Dec 2020 22:23:13 +0200 Subject: [PATCH] update metadata in ifx --- src/constants.js | 12 ++++- src/services/ProcessorService.js | 25 ++++++++++ src/services/metadataService.js | 80 ++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/services/metadataService.js diff --git a/src/constants.js b/src/constants.js index 3a237bc..8c386bb 100644 --- a/src/constants.js +++ b/src/constants.js @@ -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 } diff --git a/src/services/ProcessorService.js b/src/services/ProcessorService.js index 1040863..05b540e 100644 --- a/src/services/ProcessorService.js +++ b/src/services/ProcessorService.js @@ -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 @@ -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 @@ -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) { diff --git a/src/services/metadataService.js b/src/services/metadataService.js new file mode 100644 index 0000000..51a3701 --- /dev/null +++ b/src/services/metadataService.js @@ -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 +}