Skip to content

Commit

Permalink
Merge pull request #187 from Chia-Network/refactor/assocationed-table…
Browse files Browse the repository at this point in the history
…-data

Merge pull request #185 from Chia-Network/fix/assertions
  • Loading branch information
MichaelTaylor3D authored Jan 18, 2022
2 parents 7fe6215 + b39630d commit 82bc2f3
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 27 deletions.
17 changes: 17 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 @@ -36,6 +36,7 @@
"socket.io": "^4.4.0",
"socket.io-client": "^4.4.0",
"sqlite3": "4.1.1",
"updeep": "^1.2.1",
"uuidv4": "^6.2.12"
},
"devDependencies": {
Expand Down
12 changes: 4 additions & 8 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ export const findAll = async (req, res) => {
let { page, limit, search, orgUid, columns } = req.query;
let where = orgUid ? { orgUid } : undefined;

const includes = [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
];
const includes = Project.getAssociatedModels();

if (columns) {
// Remove any unsupported columns
Expand Down Expand Up @@ -145,7 +139,9 @@ export const update = async (req, res) => {

// merge the new record into the old record
let stagedRecord = Array.isArray(req.body) ? req.body : [req.body];
stagedRecord.map((record) => Object.assign({}, originalRecord, record));
stagedRecord = stagedRecord.map((record) =>
Object.assign({}, originalRecord, record),
);

const stagedData = {
uuid: req.body.warehouseProjectId,
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 @@ -26,12 +26,14 @@ export const findAll = async (req, res) => {
if (workingData.table === 'Projects') {
original = await Project.findOne({
where: { warehouseProjectId: workingData.uuid },
include: Project.getAssociatedModels(),
});
}

if (workingData.table === 'Units') {
original = await Unit.findOne({
where: { warehouseUnitId: workingData.uuid },
include: Unit.getAssociatedModels(),
});
}

Expand Down
14 changes: 4 additions & 10 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

import _ from 'lodash';

import { uuid as uuidv4 } from 'uuidv4';

import { Staging, Unit, Qualification, Vintage, Organization } from '../models';
Expand Down Expand Up @@ -118,7 +117,6 @@ export const findAll = async (req, res) => {
if (columns.includes('vintages')) {
results.rows = await Promise.all(
results.rows.map(async (result) => {
console.log(result.dataValues.vintageId);
result.dataValues.vintage = await Vintage.findByPk(
result.dataValues.vintageId,
);
Expand All @@ -144,13 +142,7 @@ export const findOne = async (req, res) => {
console.info('req.query', req.query);
res.json(
await Unit.findByPk(req.query.warehouseUnitId, {
include: [
{
model: Qualification,
as: 'qualifications',
},
Vintage,
],
include: Unit.getAssociatedModels(),
}),
);
};
Expand All @@ -169,7 +161,9 @@ export const update = async (req, res) => {

// merge the new record into the old record
let stagedRecord = Array.isArray(req.body) ? req.body : [req.body];
stagedRecord.map((record) => Object.assign({}, originalRecord, record));
stagedRecord = stagedRecord.map((record) =>
Object.assign({}, originalRecord, record),
);

const stagedData = {
uuid: req.body.warehouseUnitId,
Expand Down
4 changes: 2 additions & 2 deletions src/models/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dotenv from 'dotenv';
dotenv.config();

// possible values: local, test
export const sequelize = new Sequelize(config['test']);
export const sequelize = new Sequelize(config[process.env.NODE_ENV]);

const mirrorConfig = process.env.NODE_ENV === 'local' ? 'mirror' : 'mirrorTest';
export const sequelizeMirror = new Sequelize(config[mirrorConfig]);
Expand All @@ -16,6 +16,6 @@ export const safeMirrorDbHandler = (callback) => {
callback();
})
.catch(() => {
// console.log('Mirror DB not connected');
console.log('Mirror DB not connected');
});
};
7 changes: 7 additions & 0 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Project extends Model {
static stagingTableName = 'Projects';
static changes = new rxjs.Subject();
static defaultColumns = Object.keys(ModelTypes);
static getAssociatedModels = () => [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
];

static associate() {
Project.hasMany(ProjectLocation, { foreignKey: 'warehouseProjectId' });
Expand Down
9 changes: 9 additions & 0 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ const virtualFields = {
class Unit extends Model {
static stagingTableName = 'Units';
static changes = new rxjs.Subject();

static defaultColumns = Object.keys(
Object.assign({}, ModelTypes, virtualFields),
);

static getAssociatedModels = () => [
{
model: Qualification,
as: 'qualifications',
},
Vintage,
];

static associate() {
Unit.belongsTo(Vintage, {
sourceKey: 'vintageId',
Expand Down
9 changes: 7 additions & 2 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export const assertUnitRecordExists = async (
warehouseUnitId,
customMessage,
) => {
const record = await Unit.findByPk(warehouseUnitId);
const record = await Unit.findByPk(warehouseUnitId, {
include: Unit.getAssociatedModels(),
});
if (!record) {
throw new Error(
customMessage ||
Expand All @@ -65,7 +67,10 @@ export const assertProjectRecordExists = async (
warehouseProjectId,
customMessage,
) => {
const record = await Project.findByPk(warehouseProjectId);
const record = await Project.findByPk(warehouseProjectId, {
include: Project.getAssociatedModels(),
});

if (!record) {
throw new Error(
customMessage ||
Expand Down
6 changes: 1 addition & 5 deletions tests/integration/unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,7 @@ describe('Create Unit Integration', function () {
const originalRecord = stagingRecord.diff.original;

// The orginal record should be the original unit before the split
// merge the unitRecord into the originalRecord because the original includes associated tables
// that we are not testing right now
expect(Object.assign({}, originalRecord, unitRecord)).to.deep.equal(
unitRecord,
);
expect(originalRecord).to.deep.equal(unitRecord);

// Check that the 2 split records have set up their data correctly
const splitRecord1 = stagingRecord.diff.change[0];
Expand Down

0 comments on commit 82bc2f3

Please sign in to comment.