Skip to content

Commit

Permalink
feat: sync the orgUid back to cw
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 25, 2022
1 parent 3aa019e commit 4a9cd0b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const create = async (req, res) => {
newRecord.warehouseProjectId = uuid;

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

// The new project is getting created in this registry
Expand Down
79 changes: 47 additions & 32 deletions src/fullnode/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const startDataLayerUpdatePolling = () => {
updateInterval = setInterval(async () => {
const tablesToUpdate = await dataLayerWasUpdated();
_.keys(tablesToUpdate).forEach((storeId) => {
if (tablesToUpdate[storeId]) {
syncDataLayerStore(storeId);
}
// if (tablesToUpdate[storeId]) {
syncDataLayerStore(storeId);
// }
});
}, 10000);
};
Expand All @@ -42,45 +42,60 @@ export const syncDataLayerStore = async (storeId) => {
storeData = await dataLayer.getStoreData(storeId);
}

await Promise.all([
Unit.destroy({ where: {}, truncate: true }),
Project.destroy({ where: {}, truncate: true }),
RelatedProject.destroy({ where: {}, truncate: true }),
QualificationUnit.destroy({ where: {}, truncate: true }),
CoBenefit.destroy({ where: {}, truncate: true }),
Vintage.destroy({ where: {}, truncate: true }),
ProjectLocation.destroy({ where: {}, truncate: true }),
Qualification.destroy({ where: {}, truncate: true }),
]);
// Extract out the orgUid thats being updated so we can
// clean that orgs tables and repopulate without truncating
// the entire db
const { decodedStoreData, orgUid } = storeData.keys_values.reduce(
(accum, kv) => {
const decodedRecord = {
key: new Buffer(kv.key.replace(`${storeId}_`, ''), 'hex').toString(),
value: JSON.parse(new Buffer(kv.value, 'hex').toString()),
};

await Promise.all(
storeData.keys_values.map(async (kv) => {
const key = new Buffer(
kv.key.replace(`${storeId}_`, ''),
'hex',
).toString();
const value = new Buffer(kv.value, 'hex').toString();
accum.decodedStoreData.push(decodedRecord);
if (_.get(decodedRecord, 'value.orgUid')) {
accum.orgUid = _.get(decodedRecord, 'value.orgUid');
}

return accum;
},
{
decodedStoreData: [],
orgUid: null,
},
);

if (orgUid) {
await Promise.all([
// the child table records should cascade delete so we only need to
// truncate the primary tables
Unit.destroy({ where: { orgUid } }),
Project.destroy({ where: { orgUid } }),
QualificationUnit.destroy({ where: { orgUid } }),
]);
}

await Promise.all(
decodedStoreData.map(async (kv) => {
const { key, value } = kv;
if (key.includes('unit')) {
const data = JSON.parse(value);
await Unit.upsert(data);
await Staging.destroy({ where: { uuid: data.warehouseUnitId } });
await Unit.upsert(value);
await Staging.destroy({ where: { uuid: value.warehouseUnitId } });
} else if (key.includes('project')) {
const data = JSON.parse(value);
await Project.upsert(data);
await Staging.destroy({ where: { uuid: data.warehouseProjectId } });
await Project.upsert(value);
await Staging.destroy({ where: { uuid: value.warehouseProjectId } });
} else if (key.includes('relatedProjects')) {
await RelatedProject.upsert(JSON.parse(value));
await RelatedProject.upsert(value);
} else if (key.includes('qualification_units')) {
await QualificationUnit.upsert(JSON.parse(value));
await QualificationUnit.upsert(value);
} else if (key.includes('coBenefits')) {
await CoBenefit.upsert(JSON.stringify(value));
await CoBenefit.upsert(value);
} else if (key.includes('vintages')) {
await Vintage.upsert(JSON.parse(value));
await Vintage.upsert(value);
} else if (key.includes('projectLocations')) {
await ProjectLocation.upsert(JSON.parse(value));
await ProjectLocation.upsert(value);
} else if (key.includes('qualifications')) {
await Qualification.upsert(JSON.parse(value));
await Qualification.upsert(value);
}
}),
);
Expand Down
7 changes: 2 additions & 5 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ class Organization extends Model {
});

return organizations.reduce((map, current) => {
if (map) {
map = {};
}

map[current.orgUid] = current;
});
return map;
}, {});
}

static async createHomeOrganization(name, icon, dataVersion = 'v1') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const Sequelize = require('sequelize');

module.exports = {
orgUid: {
type: Sequelize.STRING,
required: true,
},
warehouseUnitId: Sequelize.STRING,
qualificationId: Sequelize.STRING,
createdAt: {
Expand Down
6 changes: 4 additions & 2 deletions src/models/qualificationUnits/qualificationUnits.stub.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[
{
"warehouseUnitId": "5c960ac1-a180-45a4-9850-be177e26d2fb",
"qualificationId": "702cafbb-c624-4273-9cdc-c617ad5675df"
"qualificationId": "702cafbb-c624-4273-9cdc-c617ad5675df",
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662"
},
{
"warehouseUnitId": "5c960ac1-a180-45a4-9850-be177e26d2fb",
"qualificationId": "76903895-840e-406c-b2a0-f90244acf02d"
"qualificationId": "76903895-840e-406c-b2a0-f90244acf02d",
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662"
}
]

0 comments on commit 4a9cd0b

Please sign in to comment.