Skip to content

Commit

Permalink
feat: datalayer organization setup
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 24, 2022
1 parent 413ea37 commit 6150001
Show file tree
Hide file tree
Showing 39 changed files with 854 additions and 357 deletions.
13 changes: 13 additions & 0 deletions migrations/20220122141750-create-simulator-table.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

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

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

async down(queryInterface) {
await queryInterface.dropTable('simulator');
},
};
13 changes: 13 additions & 0 deletions migrations/20220122164836-create-organizatin-tablec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

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

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

async down(queryInterface) {
await queryInterface.dropTable('organizations');
},
};
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"joi": "^17.5.0",
"lodash": "^4.17.21",
"mysql2": "^2.3.3",
"random-hash": "^4.0.1",
"request-promise": "^4.2.6",
"rxjs": "^7.5.1",
"sequelize": "^6.12.0-alpha.1",
Expand Down
26 changes: 26 additions & 0 deletions seeders/20220121232631-add-test-organization.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const dotenv = require('dotenv');
dotenv.config();

const metaStub = [
{
metaKey: 'organizationId',
metaValue: 'f1c54511-865e-4611-976c-7c3c1f704662',
},
{
metaKey: 'organizationName',
metaValue: 'Demo Org',
},
];

module.exports = {
up: async (queryInterface) => {
if (process.env.USE_SIMULATOR === 'true') {
await queryInterface.bulkInsert('meta', metaStub, {});
}
},

down: async (queryInterface) => {
await queryInterface.bulkDelete('meta');
},
};
9 changes: 6 additions & 3 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Organization } from '../models/organizations';
import { updateOrganization } from '../fullnode/dataLayerService';

export const findAll = async (req, res) => {
return res.json(await Organization.getOrgsMap());
};

export const create = async (req, res) => {
const { name, icon, website } = req.body;
const { name, icon } = req.body;
return res.json({
message: 'New organization created successfully.',
orgId: await updateOrganization(name, icon, website),
orgId: await Organization.createHomeOrganization(name, icon, 'v1'),
});
};

// eslint-disable-next-line
export const importOrg = async (req, res) => {};
199 changes: 4 additions & 195 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import _ from 'lodash';
import * as fullNode from '../fullnode';

import { Staging, Project, Unit } from '../models';
import {
assertStagingRecordExists,
assertUnitRecordExists,
assertProjectRecordExists,
} from '../utils/data-assertions';
import { assertStagingRecordExists } from '../utils/data-assertions';

export const findAll = async (req, res) => {
try {
Expand Down Expand Up @@ -76,69 +71,15 @@ export const findAll = async (req, res) => {

export const commit = async (req, res) => {
try {
const queryResponses = await Staging.findAll();

await Promise.all(
queryResponses.map(async (queryResponse) => {
const stagingRecord = queryResponse.dataValues;

const {
id: stagingRecordId,
uuid,
table,
action,
commited,
data: rawData,
} = stagingRecord;
let data = JSON.parse(rawData);

if (table === 'Projects' && !commited) {
const customAssertionMessage = `The project record for the warehouseProjectId: ${uuid} does not exist. Please remove ${uuid} from the staging table and try to commit again.`;
switch (action) {
case 'INSERT':
data.warehouseUnitId = uuid;
fullNode.createProjectRecord(uuid, data, stagingRecordId);
break;
case 'UPDATE':
await assertProjectRecordExists(uuid, customAssertionMessage);
fullNode.updateProjectRecord(uuid, data, stagingRecordId);
break;
case 'DELETE':
await assertProjectRecordExists(uuid, customAssertionMessage);
fullNode.deleteProjectRecord(uuid, stagingRecordId);
break;
}
} else if (table === 'Units' && !commited) {
const customAssertionMessage = `The unit record for the warehouseUnitId: ${uuid} does not exist. Please remove ${uuid} from the staging table and try to commit again.`;
switch (action) {
case 'INSERT':
fullNode.createUnitRecord(uuid, data, stagingRecordId);
break;
case 'UPDATE':
await assertUnitRecordExists(uuid, customAssertionMessage);
fullNode.updateUnitRecord(uuid, data, stagingRecordId);
break;
case 'DELETE':
await assertUnitRecordExists(uuid, customAssertionMessage);
fullNode.deleteUnitRecord(uuid, stagingRecordId);
break;
}
}

// set the commited flag to true
await Staging.update(
{ commited: true },
{ where: { id: stagingRecordId } },
);
}),
);

await Staging.pushToDataLayer();
res.json({ message: 'Staging Table committed to full node' });
} catch (error) {
res.status(400).json({
message: 'Error commiting staging table',
error: error.message,
});

console.trace(error);
}
};

Expand Down Expand Up @@ -176,135 +117,3 @@ export const clean = async (req, res) => {
});
}
};

export const commitV2 = async (req, res) => {
try {
const queryResponses = await Staging.findAll();

const changeList = [];

await Promise.all(
queryResponses.map(async (queryResponse) => {
const stagingRecord = queryResponse.dataValues;

const {
id: stagingRecordId,
uuid,
table,
action,
commited,
data: rawData,
} = stagingRecord;
let data = JSON.parse(rawData);

if (table === 'Projects' && !commited) {
const customAssertionMessage = `The project record for the warehouseProjectId: ${uuid} does not exist. Please remove ${uuid} from the staging table and try to commit again.`;
const projectRecord = _.omit(data, 'qualifications', 'vintages');
// const qualificationsRecords = data.qualifications;
// const vintageRecords = data.vintages;

console.log('!@@');

switch (action) {
case 'INSERT':
data.warehouseProjectId = uuid;

changeList.push([
{
action: 'delete',
key: new Buffer(`project_${uuid}`).toString('hex'),
},
{
action: 'insert',
key: new Buffer(`project_${uuid}`).toString('hex'),
value: new Buffer(JSON.stringify(projectRecord)).toString(
'hex',
),
},
]);
break;
case 'UPDATE':
await assertProjectRecordExists(uuid, customAssertionMessage);
changeList.push([
{
action: 'delete',
key: new Buffer(`project_${uuid}`).toString('hex'),
},
{
action: 'insert',
key: new Buffer(`project_${uuid}`).toString('hex'),
value: new Buffer(JSON.stringify(data)).toString('hex'),
},
]);
break;
case 'DELETE':
await assertProjectRecordExists(uuid, customAssertionMessage);
changeList.push([
{
action: 'delete',
key: new Buffer(`project_${uuid}`).toString('hex'),
},
]);
break;
}
} else if (table === 'Units' && !commited) {
const customAssertionMessage = `The unit record for the warehouseUnitId: ${uuid} does not exist. Please remove ${uuid} from the staging table and try to commit again.`;
const unitRecord = _.omit(data, 'qualifications', 'vintage');
switch (action) {
case 'INSERT':
changeList.push({
action: 'delete',
key: new Buffer(`unit_${uuid}`).toString('hex'),
});
changeList.push({
action: 'insert',
key: new Buffer(`unit_${uuid}`).toString('hex'),
value: new Buffer(JSON.stringify(unitRecord)).toString('hex'),
});

break;
case 'UPDATE':
await assertUnitRecordExists(uuid, customAssertionMessage);
changeList.push([
{
action: 'delete',
key: new Buffer(`unit_${uuid}`).toString('hex'),
},
{
action: 'insert',
key: new Buffer(`unit_${uuid}`).toString('hex'),
value: new Buffer(JSON.stringify(unitRecord)).toString('hex'),
},
]);
break;
case 'DELETE':
await assertUnitRecordExists(uuid, customAssertionMessage);
changeList.push([
{
action: 'delete',
key: new Buffer(`unit_${uuid}`).toString('hex'),
},
]);
break;
}
}

// set the commited flag to true
await Staging.update(
{ commited: true },
{ where: { id: stagingRecordId } },
);
}),
);

console.log(changeList);
await fullNode.pushChangeListToRegistryTable('units', changeList);

res.json({ message: 'Staging Table committed to full node' });
} catch (error) {
res.status(400).json({
message: 'Error commiting staging table',
error: error.message,
});
}
};
2 changes: 1 addition & 1 deletion src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const create = async (req, res) => {
newRecord.warehouseUnitId = uuid;

// All new units are assigned to the home orgUid
const orgUid = _.head(Object.keys(await Organization.getHomeOrg()));
const { orgUid } = await Organization.getHomeOrg();
newRecord.orgUid = orgUid;
newRecord.unitOwnerOrgUid = orgUid;

Expand Down
28 changes: 28 additions & 0 deletions src/fullnode/data-layer-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const changeListFactory = (action, id, record) => {
console.log({ action, id, record });
switch (action) {
case 'INSERT':
return {
action: 'insert',
key: Buffer.from(id).toString('hex'),
value: Buffer.from(JSON.stringify(record)).toString('hex'),
};
case 'UPDATE':
return [
{
action: 'delete',
key: Buffer.from(id).toString('hex'),
},
{
action: 'insert',
key: Buffer.from(id).toString('hex'),
value: Buffer.from(JSON.stringify(record)).toString('hex'),
},
];
case 'DELETE':
return {
action: 'delete',
key: Buffer.from(id).toString('hex'),
};
}
};
Loading

0 comments on commit 6150001

Please sign in to comment.