Skip to content

Commit

Permalink
Merge branch 'develop' into feat/fts
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen authored Dec 15, 2021
2 parents e787862 + f3f81e9 commit fe0b754
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 35 deletions.
38 changes: 22 additions & 16 deletions migrations/20211201194416-create-qualification.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,55 @@ module.exports = {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
},
projectId: {
type: Sequelize.NUMBER
type: Sequelize.NUMBER,
},
qualificationId: {
type: Sequelize.NUMBER,
},
qualificationLink: {
type: Sequelize.STRING,
},
type: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
label: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
creditingPeriodStartDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
creditingPeriodEndDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
validityStartDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
validityEndDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
unitQuantity: {
type: Sequelize.NUMBER
type: Sequelize.NUMBER,
},
owner: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
unitId: {
type: Sequelize.NUMBER
type: Sequelize.NUMBER,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
type: Sequelize.DATE,
},
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Qualifications');
}
};
},
};
29 changes: 16 additions & 13 deletions migrations/20211201194541-create-vintage.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,43 @@ module.exports = {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
},
startDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
endDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
verificationApproach: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
verificationDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
verificationBody: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
owner: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
projectId: {
type: Sequelize.NUMBER
type: Sequelize.NUMBER,
},
UnitId: {
type: Sequelize.NUMBER,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
type: Sequelize.DATE,
},
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Vintages');
}
};
},
};
22 changes: 19 additions & 3 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { uuid as uuidv4 } from 'uuidv4';
import { Staging, ProjectMock, Project } from '../models';
import { sequelize } from "../models/database.js";
import {
Staging,
ProjectMock,
Project,
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
} from '../models';

export const create = async (req, res) => {
// When creating new projects assign a uuid to is so
Expand Down Expand Up @@ -36,7 +44,15 @@ export const findAll = async (req, res) => {
}

} else {
res.json(await Project.findAll());
res.json(await Project.findAll({
include: [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
],
}));
}

};
Expand Down
37 changes: 37 additions & 0 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import * as fullNode from '../fullnode;';
import { Staging, StagingMock, Project, Unit } from '../models';

export const findAll = async (req, res) => {
Expand Down Expand Up @@ -50,6 +51,42 @@ export const findAll = async (req, res) => {
res.json(response);
};

export const commit = async (req, res) => {
const queryResponse = await Staging.findAll();
const stagingRecords = queryResponse.dataValues;
stagingRecords.forEach(async (stagingRecord) => {
const { uuid, table, action, data: rawData } = stagingRecord;
const data = JSON.parse(rawData);

if (table === 'Projects') {
switch (action) {
case 'INSERT':
fullNode.createProjectRecord(uuid, data);
break;
case 'UPDATE':
fullNode.updateProjectRecord(uuid, data);
break;
case 'DELETE':
fullNode.deleteProjectRecord(uuid);
break;
}
} else if (table === 'Unit') {
switch (action) {
case 'INSERT':
fullNode.createUnitRecord(uuid, data);
break;
case 'UPDATE':
fullNode.updateUnitRecord(uuid, data);
break;
case 'DELETE':
fullNode.deleteUnitRecord(uuid);
break;
}
}
});
res.json({ message: 'Not implemented' });
};

export const destroy = (req, res) => {
Staging.destroy({
where: {
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { uuid as uuidv4 } from 'uuidv4';
import { Staging, UnitMock, Unit } from '../models';
import { Staging, UnitMock, Unit, Qualification, Vintage } from '../models';

export const create = (req, res) => {
// When creating new projects assign a uuid to is so
Expand Down Expand Up @@ -31,7 +31,11 @@ export const findAll = async (req, res) => {
return;
}

res.json(await Unit.findAll());
res.json(
await Unit.findAll({
include: [Qualification, Vintage],
}),
);
};

export const findOne = (req, res) => {
Expand Down
25 changes: 25 additions & 0 deletions src/fullnode/fullnode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as simulator from './simulator';

export const updateProjectRecord = async (uuid, record) => {
await simulator.updateProjectRecord(uuid, record);
};

export const createProjectRecord = async (uuid, record) => {
await simulator.createProjectRecord(uuid, record);
};

export const deleteProjectRecord = async (uuid) => {
await simulator.deleteProjectRecord(uuid);
};

export const updateUnitRecord = async (uuid, record) => {
await simulator.updateUnitRecord(uuid, record);
};

export const createUnitRecord = async (uuid, record) => {
await simulator.createUnitRecord(uuid, record);
};

export const deleteUnitRecord = async (uuid) => {
await simulator.deleteUnitRecord(uuid);
};
1 change: 1 addition & 0 deletions src/fullnode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fullnode';
64 changes: 64 additions & 0 deletions src/fullnode/simulator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Project, Unit } from '../models';
const THIRTY_SEC = 30000;

// Simulate 30 seconds passing before commited to node

export const updateProjectRecord = async (uuid, record) => {
await deleteProjectRecord(uuid);
await createProjectRecord(uuid, record);
};

export const createProjectRecord = (uuid, record) => {
return new Promise(async (resolve) => {
setTimeout(() => {
await Project.create({
...record,
warehouseProjectId: uuid,
});
resolve();
}, THIRTY_SEC);
});
};

export const deleteProjectRecord = (uuid) => {
return new Promise(async (resolve) => {
setTimeout(() => {
await Project.destroy({
where: {
warehouseProjectId: uuid,
},
});
resolve();
}, THIRTY_SEC);
});
};

export const updateUnitRecord = async (uuid, record) => {
await deleteUnitRecord(uuid);
await createUnitRecord(uuid, record);
};

export const createUnitRecord = (uuid, record) => {
return new Promise(async (resolve) => {
setTimeout(() => {
await Unit.create({
uuid,
...record,
});
resolve();
}, THIRTY_SEC);
});
};

export const deleteUnitRecord = (uuid) => {
return new Promise(async (resolve) => {
setTimeout(() => {
await Unit.destroy({
where: {
uuid,
},
});
resolve();
}, THIRTY_SEC);
});
};
3 changes: 3 additions & 0 deletions src/models/qualifications/qualifications.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Qualification.init(
primaryKey: true,
type: Sequelize.INTEGER,
},
qualificationId: {
type: Sequelize.NUMBER,
},
qualificationLink: {
type: Sequelize.STRING,
},
Expand Down
4 changes: 3 additions & 1 deletion src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects/index';
import { Project, Qualification, Vintage } from '../../models';

class Unit extends Model {
static associate() {
Unit.belongsTo(Project);
Unit.hasMany(Qualification);
Unit.hasMany(Vintage);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/models/vintages/vintages.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Vintage.init(
projectId: Sequelize.NUMBER,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
UnitId: Sequelize.NUMBER,
},
{
sequelize,
Expand Down
2 changes: 2 additions & 0 deletions src/routes/v1/resources/stagings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ StagingRouter.delete(
StagingController.destroy,
);

StagingRouter.post('/commit', StagingController.commit);

export { StagingRouter };

0 comments on commit fe0b754

Please sign in to comment.