Skip to content

Commit

Permalink
fix: dont mutate homeorg info if no homeorg
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Oct 23, 2023
1 parent 3f79378 commit d570f5f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 27 deletions.
30 changes: 17 additions & 13 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,23 @@ app.use(function (req, res, next) {
});

app.use(async function (req, res, next) {
// If the home organization is syncing, then we treat all requests as read-only
const homeOrg = await Organization.getHomeOrg();

if (req.method !== 'GET' && !homeOrg.synced) {
res.status(400).json({
message:
'Your organization data is still resyncing, please try again after it completes',
success: false,
});
} else if (homeOrg.synced) {
res.setHeader(headerKeys.HOME_ORGANIZATION_SYNCED, true);
} else {
res.setHeader(headerKeys.HOME_ORGANIZATION_SYNCED, false);
if (process.env.NODE_ENV !== 'test') {
// If the home organization is syncing, then we treat all requests as read-only
const homeOrg = await Organization.getHomeOrg();

if (homeOrg) {
if (req.method !== 'GET' && !homeOrg.synced) {
res.status(400).json({
message:
'Your organization data is still resyncing, please try again after it completes',
success: false,
});
} else if (homeOrg?.synced) {
res.setHeader(headerKeys.HOME_ORGANIZATION_SYNCED, true);
} else {
res.setHeader(headerKeys.HOME_ORGANIZATION_SYNCED, false);
}
}
}

next();
Expand Down
16 changes: 4 additions & 12 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ import ModelTypes from './organizations.modeltypes.cjs';
class Organization extends Model {
static async getHomeOrg(includeAddress = true) {
const myOrganization = await Organization.findOne({
attributes: [
'orgUid',
'name',
'icon',
'subscribed',
'registryId',
'fileStoreId',
'metadata',
'synced',
],
where: { isHome: true },
raw: true,
});
Expand All @@ -51,14 +41,16 @@ class Organization extends Model {
delete myOrganization.metadata;
}

myOrganization.synced = myOrganization?.synced === 1;

if (myOrganization && includeAddress) {
myOrganization.xchAddress = await datalayer.getPublicAddress();
myOrganization.fileStoreSubscribed = true;
return myOrganization;
}

if (myOrganization) {
myOrganization.synced = myOrganization.synced === 1;
}

return myOrganization;
}

Expand Down
7 changes: 5 additions & 2 deletions src/tasks/sync-audit-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ const syncOrganizationAudit = async (organization) => {
rootHash = lastRootSaved.rootHash;
}

const isSynced = rootHistory[rootHistory.length - 1].root_hash === rootHash;
let isSynced = rootHistory[rootHistory.length - 1].root_hash === rootHash;
if (process.env.NODE_ENV === 'test') {
isSynced = false;
}

const historyIndex = rootHistory.findIndex(
(root) => root.root_hash === rootHash,
Expand All @@ -235,7 +238,7 @@ const syncOrganizationAudit = async (organization) => {
{ where: { orgUid: organization.orgUid } },
);

if (isSynced) {
if (process.env.NODE_ENV !== 'test' && isSynced) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions tests/integration/project.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('Project Resource Integration Tests', function () {
await testFixtures.commitStagingRecords();
await testFixtures.waitForDataLayerSync();
await testFixtures.waitForDataLayerSync();
await testFixtures.waitForDataLayerSync();

// The staging table should be empty after committing
expect(await testFixtures.getLastCreatedStagingRecord()).to.equal(
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('Unit Resource Integration Tests', function () {
expect(createdCommitResult.statusCode).to.equal(200);
expect(createdCommitResult.body).to.deep.equal({
message: 'Staging Table committed to full node',
success: true,
});

// The node simulator runs on an async process, we are importing
Expand Down Expand Up @@ -191,6 +192,7 @@ describe('Unit Resource Integration Tests', function () {

expect(unitRes.body).to.deep.equal({
message: 'Unit split successful',
success: true,
});

expect(unitRes.statusCode).to.equal(200);
Expand Down Expand Up @@ -247,6 +249,7 @@ describe('Unit Resource Integration Tests', function () {
expect(stagingRes.statusCode).to.equal(200);
expect(commitRes.body).to.deep.equal({
message: 'Staging Table committed to full node',
success: true,
});

// After commiting the true flag should be set to this staging record
Expand Down
2 changes: 2 additions & 0 deletions tests/test-fixtures/project-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const updateProject = async (warehouseProjectId, originalRecord) => {

expect(result.body).to.deep.equal({
message: 'Project update added to staging',
success: true,
});
expect(result.statusCode).to.equal(200);

Expand All @@ -49,6 +50,7 @@ export const deleteProject = async (warehouseProjectId) => {
.send({ warehouseProjectId });
expect(result.body).to.deep.equal({
message: 'Project deleted successfully',
success: true,
});
expect(result.statusCode).to.equal(200);
return result;
Expand Down
1 change: 1 addition & 0 deletions tests/test-fixtures/staging-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const commitStagingRecords = async () => {
expect(results.statusCode).to.equal(200);
expect(results.body).to.deep.equal({
message: 'Staging Table committed to full node',
success: true,
});

return results;
Expand Down
2 changes: 2 additions & 0 deletions tests/test-fixtures/unit-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const deleteUnit = async (warehouseUnitId) => {
.send({ warehouseUnitId });
expect(result.body).to.deep.equal({
message: 'Unit deleted successfully',
success: true,
});
expect(result.statusCode).to.equal(200);
return result;
Expand All @@ -50,6 +51,7 @@ export const updateUnit = async (warehouseUnitId, originalRecord) => {

expect(result.body).to.deep.equal({
message: 'Unit update added to staging',
success: true,
});
expect(result.statusCode).to.equal(200);

Expand Down

0 comments on commit d570f5f

Please sign in to comment.