Skip to content

Commit

Permalink
feat: add diffs to stage resource
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Dec 9, 2021
1 parent 1e15a02 commit 657b34a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as QualificationController from './qualification.controller';
import * as RelatedProjectController from './related-projects.controller';
import * as UnitController from './units.controller';
import * as VintageController from './vintages.controller';
import * as StagingController from './staging.controller';

export {
ProjectController,
Expand All @@ -16,4 +17,5 @@ export {
RelatedProjectController,
UnitController,
VintageController,
StagingController,
};
35 changes: 34 additions & 1 deletion src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
import { Staging, StagingMock } from '../models';
import _ from 'lodash';
import { Staging, StagingMock, Project, Unit } from '../models';

export const findAll = async (req, res) => {
if (req.query.useMock) {
res.json(StagingMock.findAll());
return;
}

res.json(Staging.findAll());
const stagingData = await Staging.findAll();

const response = await Promise.all(
stagingData.map(async (data) => {
const workingData = _.cloneDeep(data.dataValues);
workingData.diff = {};
if (workingData.action === 'INSERT') {
workingData.diff.original = {};
workingData.diff.change = JSON.parse(workingData.data);
}

if (workingData.action === 'UPDATE') {
const Model = workingData.table === 'Projects' ? Project : Unit;
const original = await Model.findOne({
where: { uuid: workingData.uuid },
});
workingData.diff.original = original;
workingData.diff.change = JSON.parse(workingData.data);
}

if (workingData.action === 'DELETE') {
workingData.diff.original = JSON.parse(workingData.data);
workingData.diff.change = {};
}

delete workingData.data;

return workingData;
}),
);

res.json(response);
};

export const destroy = (req, res) => {
Expand Down
2 changes: 2 additions & 0 deletions src/routes/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RatingRouter,
VintageRouter,
RelatedProjectRouter,
StagingRouter,
} from './resources';

V1Router.use('/projects', ProjectRouter);
Expand All @@ -22,5 +23,6 @@ V1Router.use('/qualifications', QualificationRouter);
V1Router.use('/ratings', RatingRouter);
V1Router.use('/vintages', VintageRouter);
V1Router.use('/related-projects', RelatedProjectRouter);
V1Router.use('/staging', StagingRouter);

export { V1Router };
1 change: 1 addition & 0 deletions src/routes/v1/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './ratings';
export * from './vintages';
export * from './related-projects';
export * from './co-benefits';
export * from './stagings';
5 changes: 5 additions & 0 deletions src/routes/v1/resources/stagings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import express from 'express';
import { StagingController } from './staging./../../controllers';
import Joi from 'joi';
import joiExpress from 'express-joi-validation';
import { StagingController } from '../../../controllers';

const validator = joiExpress.createValidator({});
const StagingRouter = express.Router();

StagingRouter.get('/', (req, res) => {
Expand Down

0 comments on commit 657b34a

Please sign in to comment.