Skip to content

Commit

Permalink
Merge pull request #265 from Chia-Network/feature/organization-subscribe
Browse files Browse the repository at this point in the history
refactor: datalayer improvements
  • Loading branch information
MichaelTaylor3D authored Feb 3, 2022
2 parents f75cf18 + 178b68b commit c62c092
Show file tree
Hide file tree
Showing 28 changed files with 330 additions and 273 deletions.
19 changes: 11 additions & 8 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
assertProjectRecordExists,
assertCsvFileInRequest,
assertHomeOrgExists,
assetNoPendingCommits,
} from '../utils/data-assertions';

import { createProjectRecordsFromCsv } from '../utils/csv-utils';
Expand All @@ -41,6 +42,7 @@ import {
export const create = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const newRecord = _.cloneDeep(req.body);
// When creating new projects assign a uuid to is so
Expand Down Expand Up @@ -74,14 +76,6 @@ export const create = async (req, res) => {
}
});

// The new project is getting created in this registry
newRecord.currentRegistry = orgUid;

// Unless we are overriding, a new project originates in this org
if (!newRecord.registryOfOrigin) {
newRecord.registryOfOrigin = orgUid;
}

await Staging.create({
uuid,
action: 'INSERT',
Expand Down Expand Up @@ -178,6 +172,7 @@ export const findOne = async (req, res) => {
export const updateFromXLS = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const { files } = req;

Expand Down Expand Up @@ -206,6 +201,7 @@ export const updateFromXLS = async (req, res) => {
export const update = async (req, res) => {
try {
await assertHomeOrgExists();
// await assetNoPendingCommits();

const originalRecord = await assertProjectRecordExists(
req.body.warehouseProjectId,
Expand Down Expand Up @@ -241,6 +237,10 @@ export const update = async (req, res) => {
childRecord.warehouseProjectId = newRecord.warehouseProjectId;
}

if (childRecordKey === 'labels' && childRecord.labelUnits) {
childRecord.labelUnits.orgUid = orgUid;
}

return childRecord;
});
}
Expand Down Expand Up @@ -273,12 +273,14 @@ export const update = async (req, res) => {
message: 'Error adding update to stage',
error: err.message,
});
console.log(err);
}
};

export const destroy = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const originalRecord = await assertProjectRecordExists(
req.body.warehouseProjectId,
Expand Down Expand Up @@ -308,6 +310,7 @@ export const destroy = async (req, res) => {
export const batchUpload = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const csvFile = assertCsvFileInRequest(req);
await createProjectRecordsFromCsv(csvFile);
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Staging } from '../models';
import {
assertStagingRecordExists,
assertHomeOrgExists,
assetNoPendingCommits,
} from '../utils/data-assertions';

export const findAll = async (req, res) => {
Expand Down Expand Up @@ -39,6 +40,7 @@ export const findAll = async (req, res) => {
export const commit = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

await Staging.pushToDataLayer();
res.json({ message: 'Staging Table committed to full node' });
Expand Down
6 changes: 6 additions & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
assertSumOfSplitUnitsIsValid,
assertCsvFileInRequest,
assertHomeOrgExists,
assetNoPendingCommits,
} from '../utils/data-assertions';

import { createUnitRecordsFromCsv } from '../utils/csv-utils';
Expand Down Expand Up @@ -188,6 +189,7 @@ export const findOne = async (req, res) => {
export const updateFromXLS = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const { files } = req;

Expand All @@ -213,6 +215,7 @@ export const updateFromXLS = async (req, res) => {
export const update = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const originalRecord = await assertUnitRecordExists(
req.body.warehouseUnitId,
Expand Down Expand Up @@ -293,6 +296,7 @@ export const update = async (req, res) => {
export const destroy = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const originalRecord = await assertUnitRecordExists(
req.body.warehouseUnitId,
Expand Down Expand Up @@ -321,6 +325,7 @@ export const destroy = async (req, res) => {
export const split = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const originalRecord = await assertUnitRecordExists(
req.body.warehouseUnitId,
Expand Down Expand Up @@ -386,6 +391,7 @@ export const split = async (req, res) => {
export const batchUpload = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();

const csvFile = assertCsvFileInRequest(req);
await createUnitRecordsFromCsv(csvFile);
Expand Down
56 changes: 39 additions & 17 deletions src/fullnode/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export const createDataLayerStore = async () => {

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

if (response.body.id) {
return response.body.id;
const data = JSON.parse(response);

if (data.success) {
return data.id;
}

throw new Error('Error creating new datalayer store');
Expand All @@ -47,13 +49,15 @@ export const pushChangeListToDataLayer = async (storeId, changelist) => {
}),
};

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

const data = JSON.parse(response);

if (response.body) {
return response.body;
if (data.success) {
return data;
}

throw new Error('Error updating datalayer store');
console.log(options, data);
};

export const getRoots = async (storeIds) => {
Expand All @@ -64,13 +68,19 @@ export const getRoots = async (storeIds) => {
}),
};

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

if (response.body) {
return response.body;
}
try {
const data = JSON.parse(response);

if (data.success) {
return data;
}

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

export const getRoot = async (storeId) => {
Expand All @@ -81,10 +91,18 @@ export const getRoot = async (storeId) => {
}),
};

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

try {
const data = JSON.parse(response);

if (response.body && response.body.keys_values) {
return response.body.keys_values;
if (data.success) {
return data;
}

return false;
} catch (error) {
return false;
}
};

Expand All @@ -97,10 +115,14 @@ export const getStoreData = async (storeId) => {
}),
};

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

const data = JSON.parse(response);

if (response.body && response.body.keys_values) {
return response.body.keys_values;
if (data.success) {
return data;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/fullnode/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const pushChangeListToDataLayer = async (storeId, changeList) => {
export const getStoreData = async (storeId) => {
if (storeId) {
const results = await Simulator.findAll({
attributes: ['key', 'value'],
where: {
key: { [Op.like]: `${storeId}%` },
},
Expand Down
Loading

0 comments on commit c62c092

Please sign in to comment.