diff --git a/src/controllers/staging.controller.js b/src/controllers/staging.controller.js index 1f3aba7c..52c87234 100644 --- a/src/controllers/staging.controller.js +++ b/src/controllers/staging.controller.js @@ -82,6 +82,7 @@ export const commit = async (req, res) => { await Staging.pushToDataLayer( _.get(req, 'query.table', null), _.get(req, 'body.comment', ''), + _.get(req, 'body.author', ''), _.get(req, 'body.ids', []), ); diff --git a/src/database/migrations/20220708210357-adding-author-column-to-audit-table.js b/src/database/migrations/20220708210357-adding-author-column-to-audit-table.js new file mode 100644 index 00000000..0cb4cb71 --- /dev/null +++ b/src/database/migrations/20220708210357-adding-author-column-to-audit-table.js @@ -0,0 +1,22 @@ +'use strict'; + +export default { + async up(queryInterface, Sequelize) { + await Promise.all( + ['audit'].map((table) => { + queryInterface.addColumn(table, 'author', { + type: Sequelize.STRING, + allowNull: true, + }); + }), + ); + }, + + async down(queryInterface) { + await Promise.all( + ['audit'].map((table) => { + queryInterface.removeColumn(table, 'author'); + }), + ); + }, +}; diff --git a/src/database/migrations/index.js b/src/database/migrations/index.js index 38c26ed8..efa49732 100644 --- a/src/database/migrations/index.js +++ b/src/database/migrations/index.js @@ -22,6 +22,7 @@ import AddCommentColumnToDataModelTables from './20220428144558-add-comment-colu import AddSerialNumberFields from './20220504180739-add-serial-number-fields'; import AddDescriptionFieldToProjects from './20220509125335-add-description-field-to-projects'; import RepopulateVirtualTables from './20220515223227-re-populate-virtual-tables'; +import AddAuthorColumnToAuditTable from './20220708210357-adding-author-column-to-audit-table'; export const migrations = [ { @@ -124,4 +125,8 @@ export const migrations = [ migration: RepopulateVirtualTables, name: '20220515223227-re-populate-virtual-tables', }, + { + migration: AddAuthorColumnToAuditTable, + name: '20220708210357-adding-author-column-to-audit-table', + } ]; diff --git a/src/models/audit/audit.modeltypes.cjs b/src/models/audit/audit.modeltypes.cjs index 8c25db4c..fbd0518d 100644 --- a/src/models/audit/audit.modeltypes.cjs +++ b/src/models/audit/audit.modeltypes.cjs @@ -41,6 +41,9 @@ module.exports = { required: true, allowNull: false, }, + author: { + type: Sequelize.STRING, + }, comment: { type: Sequelize.STRING, }, diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index 32971ef5..d1b4d080 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -294,7 +294,7 @@ class Project extends Model { }; } - static async generateChangeListFromStagedData(stagedData, comment) { + static async generateChangeListFromStagedData(stagedData, comment, author) { const [insertRecords, updateRecords, deleteChangeList] = Staging.seperateStagingDataIntoActionGroups(stagedData, 'Projects'); @@ -364,6 +364,16 @@ class Project extends Model { isUpdateComment, ); + const currentAuthor = currentDataLayer.filter( + (kv) => kv.key === 'author', + ); + const isUpdateAuthor = currentAuthor.length > 0; + const authorChangeList = keyValueToChangeList( + 'author', + `{"author": "${author}"}`, + isUpdateAuthor, + ); + return { projects: [ ..._.get(insertChangeList, 'project', []), @@ -406,6 +416,7 @@ class Project extends Model { ..._.get(deletedAssociationsChangeList, 'projectRatings', []), ], comment: commentChangeList, + author: authorChangeList, }; } } diff --git a/src/models/staging/staging.model.js b/src/models/staging/staging.model.js index 1ef4a717..a28a3411 100644 --- a/src/models/staging/staging.model.js +++ b/src/models/staging/staging.model.js @@ -205,7 +205,7 @@ class Staging extends Model { return [insertRecords, updateRecords, deleteChangeList]; }; - static async pushToDataLayer(tableToPush, comment, ids = []) { + static async pushToDataLayer(tableToPush, comment, author, ids = []) { let stagedRecords; if (tableToPush) { @@ -246,11 +246,13 @@ class Staging extends Model { const unitsChangeList = await Unit.generateChangeListFromStagedData( stagedRecords, comment, + author, ); const projectsChangeList = await Project.generateChangeListFromStagedData( stagedRecords, comment, + author, ); const unifiedChangeList = { diff --git a/src/models/units/units.model.js b/src/models/units/units.model.js index 473c3ab7..e0180ba9 100644 --- a/src/models/units/units.model.js +++ b/src/models/units/units.model.js @@ -244,7 +244,7 @@ class Unit extends Model { }; } - static async generateChangeListFromStagedData(stagedData, comment) { + static async generateChangeListFromStagedData(stagedData, comment, author) { const [insertRecords, updateRecords, deleteChangeList] = Staging.seperateStagingDataIntoActionGroups(stagedData, 'Units'); @@ -332,6 +332,16 @@ class Unit extends Model { isUpdateComment, ); + const currentAuthor = currentDataLayer.filter( + (kv) => kv.key === 'author', + ); + const isUpdateAuthor = currentAuthor.length > 0; + const authorChangeList = keyValueToChangeList( + 'author', + `{"author": "${author}"}`, + isUpdateAuthor, + ); + return { units: [ ..._.get(insertChangeList, 'unit', []), @@ -354,6 +364,7 @@ class Unit extends Model { ..._.get(deletedAssociationsChangeList, 'label_units', []), ], comment: commentChangeList, + author: authorChangeList, }; } } diff --git a/src/tasks/sync-audit-table.js b/src/tasks/sync-audit-table.js index 374703e9..dc5d75df 100644 --- a/src/tasks/sync-audit-table.js +++ b/src/tasks/sync-audit-table.js @@ -113,6 +113,12 @@ const syncOrganizationAudit = async (organization) => { diff.key === '636f6d6d656e74' || diff.key === '0x636f6d6d656e74', ); + // 0x617574686F72 is hex for 'author' + const author = kvDiff.filter( + (diff) => + diff.key === '617574686F72' || diff.key === '0x617574686F72', + ); + await Promise.all( kvDiff.map(async (diff) => { const key = decodeHex(diff.key); @@ -131,6 +137,11 @@ const syncOrganizationAudit = async (organization) => { 'comment', '', ), + author: _.get( + JSON.parse(decodeHex(_.get(author, '[0].value', '7b7d'))), + 'author', + '', + ), }); } }), diff --git a/src/validations/staging.validations.js b/src/validations/staging.validations.js index 85d07587..8145f37a 100644 --- a/src/validations/staging.validations.js +++ b/src/validations/staging.validations.js @@ -9,6 +9,7 @@ export const stagingRetrySchema = Joi.object({ }); export const commitStagingSchema = Joi.object({ + author: Joi.string().optional(), comment: Joi.string().optional(), ids: Joi.array().items(Joi.string()).optional(), });