Skip to content

Commit

Permalink
feat: fuly resolved changelist
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 24, 2022
1 parent 68bf5ba commit 1796ba1
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 250 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"joi": "^17.5.0",
"lodash": "^4.17.21",
"mysql2": "^2.3.3",
"node-xlsx": "^0.21.0",
"random-hash": "^4.0.1",
"request-promise": "^4.2.6",
"node-xlsx": "^0.21.0",
"rxjs": "^7.5.1",
"sequelize": "^6.12.0-alpha.1",
"sequelize-mock": "^0.10.2",
Expand Down
4 changes: 2 additions & 2 deletions src/fullnode/simulatorV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export const pushChangeListToDataLayer = async (storeId, changeList) => {
changeList.map(async (change) => {
if (change.action === 'insert') {
await Simulator.upsert({
key: `simulator_${storeId}_${change.key}`,
key: `${storeId}_${change.key}`,
value: change.value,
});
} else if (change.action === 'delete') {
await Simulator.destroy({
where: { key: `simulator_${storeId}_${change.key}` },
where: { key: `${storeId}_${change.key}` },
});
}
}),
Expand Down
121 changes: 65 additions & 56 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import {
Qualification,
ProjectLocation,
CoBenefit,
Rating,
Staging,
} from '../';

import { changeListFactory } from '../../fullnode/data-layer-utils';
import {
createXlsFromSequelizeResults,
transformFullXslsToChangeList,
} from '../../utils/xls';

import ModelTypes from './projects.modeltypes.cjs';
import { ProjectMirror } from './projects.model.mirror';
Expand Down Expand Up @@ -223,66 +226,72 @@ class Project extends Model {
};
}

static async generateChangeListFromStagedData(
action,
warehouseProjectId,
stagedData,
) {
const foreignKeys = [
'projectLocations',
'qualifications',
'vintages',
'coBenefits',
'relatedProjects',
];

return Promise.resolve(
changeListFactory(
action,
warehouseProjectId,
_.omit(stagedData, foreignKeys),
),
);
}

static async generateFullProjectModelChangeListFromStagedRecord(data) {
const promises = [Project.generateChangeListFromStagedData(data)];
static generateChangeListFromStagedData(stagedData) {
const [insertRecords, updateRecords, deleteChangeList] =
Staging.seperateStagingDataIntoActionGroups(stagedData, 'Projects');

if (data.projectLocations) {
promises.push(
ProjectLocation.generateChangeListFromStagedData(data.projectLocations),
);
}

if (data.qualifications) {
promises.push(
Qualification.generateChangeListFromStagedData(data.qualifications),
);
}
const insertXslsSheets = createXlsFromSequelizeResults(
insertRecords,
Project,
false,
true,
);

if (data.relatedProjects) {
promises.push(
Rating.generateChangeListFromStagedData(data.relatedProjects),
);
}
const updateXslsSheets = createXlsFromSequelizeResults(
updateRecords,
Project,
false,
true,
);

if (data.coBenefits) {
promises.push(
CoBenefit.generateChangeListFromStagedData(data.coBenefits),
);
}
const primaryKeyMap = {
projects: 'warehouseProjectId',
projectLocations: 'id',
qualifications: 'id',
vintages: 'id',
coBenifets: 'id',
relatedProjects: 'id',
};

if (data.vintages) {
promises.push(Vintage.generateChangeListFromStagedData(data.vintages));
}
const insertChangeList = transformFullXslsToChangeList(
insertXslsSheets,
'insert',
primaryKeyMap,
);

if (data.relatedProjects) {
promises.push(
RelatedProject.generateChangeListFromStagedData(data.relatedProjects),
);
}
const updateChangeList = transformFullXslsToChangeList(
updateXslsSheets,
'update',
primaryKeyMap,
);

return Promise.all(promises);
return {
projects: [
..._.get(insertChangeList, 'project', []),
..._.get(updateChangeList, 'project', []),
...deleteChangeList,
],
qualifications: [
..._.get(insertChangeList, 'qualifications', []),
..._.get(updateChangeList, 'qualifications', []),
],
projectLocations: [
..._.get(insertChangeList, 'projectLocations', []),
..._.get(updateChangeList, 'projectLocations', []),
],
vintages: [
..._.get(insertChangeList, 'vintages', []),
..._.get(updateChangeList, 'vintages', []),
],
coBenifets: [
..._.get(insertChangeList, 'coBenifets', []),
..._.get(updateChangeList, 'coBenifets', []),
],
relatedProjects: [
..._.get(insertChangeList, 'relatedProjects', []),
..._.get(updateChangeList, 'relatedProjects', []),
],
};
}
}

Expand Down
12 changes: 1 addition & 11 deletions src/models/qualifications/qualifications.model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize, safeMirrorDbHandler } from '../database';
Expand Down Expand Up @@ -47,17 +48,6 @@ class Qualification extends Model {
safeMirrorDbHandler(() => QualificationMirror.destroy(values));
return super.destroy(values);
}

static async generateChangeListFromStagedData(
// eslint-disable-next-line
action,
// eslint-disable-next-line
id,
// eslint-disable-next-line
stagedData,
) {
return {};
}
}

Qualification.init(ModelTypes, {
Expand Down
Loading

0 comments on commit 1796ba1

Please sign in to comment.