Skip to content

Commit

Permalink
feat: add author data to audit transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2sw committed Jul 11, 2022
1 parent 33b56a5 commit 6540926
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', []),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -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');
}),
);
},
};
5 changes: 5 additions & 0 deletions src/database/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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',
}
];
3 changes: 3 additions & 0 deletions src/models/audit/audit.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module.exports = {
required: true,
allowNull: false,
},
author: {
type: Sequelize.STRING,
},
comment: {
type: Sequelize.STRING,
},
Expand Down
13 changes: 12 additions & 1 deletion src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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', []),
Expand Down Expand Up @@ -406,6 +416,7 @@ class Project extends Model {
..._.get(deletedAssociationsChangeList, 'projectRatings', []),
],
comment: commentChangeList,
author: authorChangeList,
};
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/models/staging/staging.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand Down
13 changes: 12 additions & 1 deletion src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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', []),
Expand All @@ -354,6 +364,7 @@ class Unit extends Model {
..._.get(deletedAssociationsChangeList, 'label_units', []),
],
comment: commentChangeList,
author: authorChangeList,
};
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/tasks/sync-audit-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -131,6 +137,11 @@ const syncOrganizationAudit = async (organization) => {
'comment',
'',
),
author: _.get(
JSON.parse(decodeHex(_.get(author, '[0].value', '7b7d'))),
'author',
'',
),
});
}
}),
Expand Down
1 change: 1 addition & 0 deletions src/validations/staging.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
Expand Down

0 comments on commit 6540926

Please sign in to comment.