Skip to content

Commit

Permalink
feat: sync audit table to database
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Feb 23, 2022
1 parent a7fb0fa commit 09c26c0
Show file tree
Hide file tree
Showing 24 changed files with 360 additions and 3 deletions.
13 changes: 13 additions & 0 deletions migrations/20220222204323-create-audit-table.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const modelTypes = require('../src/models/audit/audit.modeltypes.cjs');

module.exports = {
async up(queryInterface) {
await queryInterface.createTable('audit', modelTypes);
},

async down(queryInterface) {
await queryInterface.dropTable('audit');
},
};
14 changes: 14 additions & 0 deletions src/controllers/audit.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Audit } from '../models';

export const findAll = async (req, res) => {
try {
const { orgUid } = req.query;
const auditResults = await Audit.findAll({ where: { orgUid } });
return res.json(auditResults);
} catch (error) {
res.status(400).json({
message: 'Can not retreive issuances',
error: error.message,
});
}
};
1 change: 1 addition & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * as StagingController from './staging.controller';
export * as OrganizationController from './organization.controller';
export * as IssuanceController from './issuance.controller';
export * as LabelController from './label.controller';
export * as AuditController from './audit.controller';
1 change: 1 addition & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export const findAll = async (req, res) => {
);
}
} catch (error) {
console.trace(error);
res.status(400).json({
message: 'Error retrieving units',
error: error.message,
Expand Down
54 changes: 54 additions & 0 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from 'lodash';

import fs from 'fs';
import path from 'path';
import request from 'request-promise';
Expand Down Expand Up @@ -213,3 +215,55 @@ export const subscribeToStoreOnDataLayer = async (storeId, ip, port) => {
return false;
}
};

export const getRootHistory = async (storeId) => {
const options = {
url: `${rpcUrl}/get_root_history`,
body: JSON.stringify({
id: storeId,
}),
};

try {
const response = await request(
Object.assign({}, getBaseOptions(), options),
);

const data = JSON.parse(response);

if (data.success) {
return _.get(data, 'root_history', []);
}

return [];
} catch (error) {
return [];
}
};

export const getRootDiff = async (storeId, root1, root2) => {
const options = {
url: `${rpcUrl}/get_kv_diff`,
body: JSON.stringify({
id: storeId,
hash_1: root1,
hash_2: root2,
}),
};

try {
const response = await request(
Object.assign({}, getBaseOptions(), options),
);

const data = JSON.parse(response);

if (data.success) {
return _.get(data, 'diff', []);
}

return [];
} catch (error) {
return [];
}
};
14 changes: 14 additions & 0 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,25 @@ const getSubscribedStoreData = async (
}, {});
};

const getRootHistory = (storeId) => {
if (process.env.USE_SIMULATOR !== 'true') {
return dataLayer.getRootHistory(storeId);
}
};

const getRootDiff = (storeId, root1, root2) => {
if (process.env.USE_SIMULATOR !== 'true') {
return dataLayer.getRootDiff(storeId, root1, root2);
}
};

export default {
startDataLayerUpdatePolling,
syncDataLayerStoreToClimateWarehouse,
dataLayerWasUpdated,
subscribeToStoreOnDataLayer,
getSubscribedStoreData,
getRootHistory,
getRootDiff,
POLLING_INTERVAL,
};
8 changes: 8 additions & 0 deletions src/models/audit/audit.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import stub from './audit.stub.json';

export const SimulatorMock = {
findAll: () => stub,
findOne: (id) => {
return stub.find((record) => record.id == id);
},
};
35 changes: 35 additions & 0 deletions src/models/audit/audit.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize, safeMirrorDbHandler } from '../database';
import { AuditMirror } from './audit.model.mirror';
import ModelTypes from './audit.modeltypes.cjs';

class Audit extends Model {
static async create(values, options) {
safeMirrorDbHandler(() => AuditMirror.create(values, options));
return super.create(values, options);
}

static async destroy(values, options) {
safeMirrorDbHandler(() => AuditMirror.destroy(values, options));
return super.destroy(values, options);
}

static async upsert(values, options) {
safeMirrorDbHandler(() => AuditMirror.upsert(values, options));
return super.upsert(values, options);
}
}

Audit.init(ModelTypes, {
sequelize,
modelName: 'audit',
freezeTableName: true,
timestamps: true,
createdAt: true,
updatedAt: true,
});

export { Audit };
22 changes: 22 additions & 0 deletions src/models/audit/audit.model.mirror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

import Sequelize from 'sequelize';
const { Model } = Sequelize;

import { sequelizeMirror, safeMirrorDbHandler } from '../database';
import ModelTypes from './audit.modeltypes.cjs';

class AuditMirror extends Model {}

safeMirrorDbHandler(() => {
AuditMirror.init(ModelTypes, {
sequelize: sequelizeMirror,
modelName: 'audit',
freezeTableName: true,
timestamps: true,
createdAt: true,
updatedAt: true,
});
});

export { AuditMirror };
52 changes: 52 additions & 0 deletions src/models/audit/audit.modeltypes.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
orgUid: {
type: Sequelize.STRING,
required: true,
allowNull: false,
},
registryId: {
type: Sequelize.STRING,
required: true,
allowNull: false,
},
rootHash: {
type: Sequelize.STRING,
required: true,
allowNull: false,
},
type: {
type: Sequelize.STRING,
required: true,
allowNull: false,
},
change: {
type: Sequelize.STRING,
required: true,
allowNull: true,
},
table: {
type: Sequelize.STRING,
required: true,
allowNull: true,
},
onchainConfirmationTimeStamp: {
type: 'TIMESTAMP',
required: true,
allowNull: false,
},
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
};
1 change: 1 addition & 0 deletions src/models/audit/audit.stub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
2 changes: 2 additions & 0 deletions src/models/audit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './audit.model.js';
export * from './audit.mock.js';
1 change: 1 addition & 0 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export * from './meta';
export * from './simulator';
export * from './labelUnits';
export * from './estimations';
export * from './audit';

export const ModelKeys = {
unit: Unit,
Expand Down
2 changes: 2 additions & 0 deletions src/routes/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
OrganizationRouter,
IssuanceRouter,
LabelRouter,
AuditRouter,
} from './resources';

V1Router.use('/projects', ProjectRouter);
Expand All @@ -18,5 +19,6 @@ V1Router.use('/staging', StagingRouter);
V1Router.use('/organizations', OrganizationRouter);
V1Router.use('/issuances', IssuanceRouter);
V1Router.use('/labels', LabelRouter);
V1Router.use('/audit', AuditRouter);

export { V1Router };
16 changes: 16 additions & 0 deletions src/routes/v1/resources/audit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

import express from 'express';
import joiExpress from 'express-joi-validation';

import { AuditController } from '../../../controllers';
import { auditGetSchema } from '../../../validations';

const validator = joiExpress.createValidator({ passError: true });
const AuditRouter = express.Router();

AuditRouter.get('/', validator.query(auditGetSchema), (req, res) => {
return AuditController.findAll(req, res);
});

export { AuditRouter };
1 change: 1 addition & 0 deletions src/routes/v1/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './staging';
export * from './organization';
export * from './issuances';
export * from './labels';
export * from './audit';
8 changes: 7 additions & 1 deletion src/tasks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ToadScheduler } from 'toad-scheduler';
import syncDataLayer from './sync-datalayer';
import syncOrganizations from './sync-organizations';
import syncPickLists from './sync-picklists';
import syncAudit from './sync-audit-table';

const scheduler = new ToadScheduler();

Expand All @@ -15,7 +16,12 @@ const addJobToScheduler = (job) => {

const start = () => {
// add default jobs
const defaultJobs = [syncDataLayer, syncOrganizations, syncPickLists];
const defaultJobs = [
syncDataLayer,
syncOrganizations,
syncPickLists,
syncAudit,
];
defaultJobs.forEach((defaultJob) => {
jobRegistry[defaultJob.id] = defaultJob;
scheduler.addSimpleIntervalJob(defaultJob);
Expand Down
Loading

0 comments on commit 09c26c0

Please sign in to comment.