Skip to content

Commit

Permalink
feat: stagin table uses upserts
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Jan 3, 2022
1 parent 9ce1fe1 commit d2773bc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
72 changes: 72 additions & 0 deletions src/controllers/units.controller.js.rej
Original file line number Diff line number Diff line change
@@ -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,
+ ),
);
};

5 changes: 4 additions & 1 deletion src/models/staging/staging.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d2773bc

Please sign in to comment.