From 657b34a33f6e14afe224104bc1f18377860b4942 Mon Sep 17 00:00:00 2001 From: Frantz Arty Date: Thu, 9 Dec 2021 15:44:36 -0500 Subject: [PATCH] feat: add diffs to stage resource --- src/controllers/index.js | 2 ++ src/controllers/staging.controller.js | 35 ++++++++++++++++++++++++++- src/routes/v1/index.js | 2 ++ src/routes/v1/resources/index.js | 1 + src/routes/v1/resources/stagings.js | 5 ++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/controllers/index.js b/src/controllers/index.js index cc444420..99c6f9bc 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -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, @@ -16,4 +17,5 @@ export { RelatedProjectController, UnitController, VintageController, + StagingController, }; diff --git a/src/controllers/staging.controller.js b/src/controllers/staging.controller.js index 0fa0a33a..f7158be0 100644 --- a/src/controllers/staging.controller.js +++ b/src/controllers/staging.controller.js @@ -1,4 +1,6 @@ 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) { @@ -6,7 +8,38 @@ export const findAll = async (req, res) => { 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) => { diff --git a/src/routes/v1/index.js b/src/routes/v1/index.js index 348d2cd9..bf192899 100644 --- a/src/routes/v1/index.js +++ b/src/routes/v1/index.js @@ -12,6 +12,7 @@ import { RatingRouter, VintageRouter, RelatedProjectRouter, + StagingRouter, } from './resources'; V1Router.use('/projects', ProjectRouter); @@ -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 }; diff --git a/src/routes/v1/resources/index.js b/src/routes/v1/resources/index.js index 683eea9a..d3678e25 100644 --- a/src/routes/v1/resources/index.js +++ b/src/routes/v1/resources/index.js @@ -6,3 +6,4 @@ export * from './ratings'; export * from './vintages'; export * from './related-projects'; export * from './co-benefits'; +export * from './stagings'; diff --git a/src/routes/v1/resources/stagings.js b/src/routes/v1/resources/stagings.js index 6bc3ffe9..5a01375a 100644 --- a/src/routes/v1/resources/stagings.js +++ b/src/routes/v1/resources/stagings.js @@ -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) => {