From d2773bcade031f7ca5e38f053e7581fade4b64a3 Mon Sep 17 00:00:00 2001 From: Frantz Arty Date: Mon, 3 Jan 2022 08:16:28 -0500 Subject: [PATCH] feat: stagin table uses upserts --- src/controllers/project.controller.js | 2 +- src/controllers/units.controller.js | 2 +- src/controllers/units.controller.js.rej | 72 +++++++++++++++++++++++ src/models/staging/staging.modeltypes.cjs | 5 +- 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/controllers/units.controller.js.rej diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index b3cfc17e..38191140 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -149,7 +149,7 @@ export const update = async (req, res) => { data: JSON.stringify(Array.isArray(req.body) ? req.body : [req.body]), }; - await Staging.create(stagedData); + await Staging.upsert(stagedData); res.json({ message: 'Project update added to staging', diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index 6170ca04..6b87dcb7 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -109,7 +109,7 @@ export const update = async (req, res) => { data: JSON.stringify(Array.isArray(req.body) ? req.body : [req.body]), }; - await Staging.create(stagedData); + await Staging.upsert(stagedData); res.json({ message: 'Unit updated successfully', diff --git a/src/controllers/units.controller.js.rej b/src/controllers/units.controller.js.rej new file mode 100644 index 00000000..c3cb17ae --- /dev/null +++ b/src/controllers/units.controller.js.rej @@ -0,0 +1,72 @@ +diff a/src/controllers/units.controller.js b/src/controllers/units.controller.js (rejected hunks) +@@ -2,7 +2,7 @@ + + import { uuid as uuidv4 } from 'uuidv4'; + import { Staging, UnitMock, Unit, Qualification, Vintage } from '../models'; +-import { optionallyPaginatedResponse, paginationParams } from "./helpers"; ++import { optionallyPaginatedResponse, paginationParams } from './helpers'; + + export const create = async (req, res) => { + try { +@@ -37,30 +37,40 @@ export const findAll = async (req, res) => { + } + + if (req.query.onlyEssentialColumns) { +- return res.json(optionallyPaginatedResponse(await Unit.findAndCountAll({ +- attributes: [ +- 'orgUid', +- 'unitLink', +- 'registry', +- 'unitType', +- 'unitCount', +- 'unitStatus', +- 'unitStatusDate', +- ], +- }), page, limit)); ++ return res.json( ++ optionallyPaginatedResponse( ++ await Unit.findAndCountAll({ ++ attributes: [ ++ 'orgUid', ++ 'unitLink', ++ 'registry', ++ 'unitType', ++ 'unitCount', ++ 'unitStatus', ++ 'unitStatusDate', ++ ], ++ }), ++ page, ++ limit, ++ ), ++ ); + } + + res.json( +- optionallyPaginatedResponse(await Unit.findAndCountAll({ +- include: [ +- { +- model: Qualification, +- as: 'qualification', +- }, +- Vintage, +- ], +- ...paginationParams(page, limit), +- }), page, limit), ++ optionallyPaginatedResponse( ++ await Unit.findAndCountAll({ ++ include: [ ++ { ++ model: Qualification, ++ as: 'qualification', ++ }, ++ Vintage, ++ ], ++ ...paginationParams(page, limit), ++ }), ++ page, ++ limit, ++ ), + ); + }; + diff --git a/src/models/staging/staging.modeltypes.cjs b/src/models/staging/staging.modeltypes.cjs index 649bc18c..b454fe13 100644 --- a/src/models/staging/staging.modeltypes.cjs +++ b/src/models/staging/staging.modeltypes.cjs @@ -6,7 +6,10 @@ module.exports = { primaryKey: true, autoIncrement: true, }, - uuid: Sequelize.STRING, + uuid: { + type: Sequelize.STRING, + unique: true, + }, table: Sequelize.STRING, action: Sequelize.STRING, data: Sequelize.STRING,