From 75b6b57a247c1c42dc7637b8c5008270f9183edb Mon Sep 17 00:00:00 2001 From: Mike Keen Date: Fri, 28 Jan 2022 23:04:05 -0500 Subject: [PATCH] feat: validation on models during import, and optional exclusion of orguid --- src/controllers/project.controller.js | 2 +- src/controllers/units.controller.js | 2 +- src/models/projects/projects.model.js | 2 ++ src/models/units/units.model.js | 4 +++- src/utils/xls.js | 30 +++++++++++++++++++-------- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index b4399c55..35d8445a 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -129,7 +129,7 @@ export const findAll = async (req, res) => { } else { return sendXls( Project.name, - createXlsFromSequelizeResults(response, Project), + createXlsFromSequelizeResults(response, Project, false, false, true), res, ); } diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index 237bef78..17644b74 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -150,7 +150,7 @@ export const findAll = async (req, res) => { } else { return sendXls( Unit.name, - createXlsFromSequelizeResults(response, Unit), + createXlsFromSequelizeResults(response, Unit, false, false, true), res, ); } diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index 66e9a813..d0ad6e38 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -29,11 +29,13 @@ import { import ModelTypes from './projects.modeltypes.cjs'; import { ProjectMirror } from './projects.model.mirror'; +import { projectsUpdateSchema } from '../../validations/index'; class Project extends Model { static stagingTableName = 'Projects'; static changes = new rxjs.Subject(); static defaultColumns = Object.keys(ModelTypes); + static validateImport = projectsUpdateSchema static getAssociatedModels = () => [ ProjectLocation, Label, diff --git a/src/models/units/units.model.js b/src/models/units/units.model.js index 682ae7ca..a97c3993 100644 --- a/src/models/units/units.model.js +++ b/src/models/units/units.model.js @@ -16,6 +16,7 @@ import { createXlsFromSequelizeResults, transformFullXslsToChangeList, } from '../../utils/xls'; +import {unitsUpdateSchema} from "../../validations/index.js"; const { Model } = Sequelize; @@ -58,7 +59,8 @@ const virtualFields = { class Unit extends Model { static stagingTableName = 'Units'; static changes = new rxjs.Subject(); - + static validateImport = unitsUpdateSchema; + static defaultColumns = Object.keys( Object.assign({}, ModelTypes, virtualFields), ); diff --git a/src/utils/xls.js b/src/utils/xls.js index e246ad0a..c6faf2a0 100644 --- a/src/utils/xls.js +++ b/src/utils/xls.js @@ -49,6 +49,7 @@ export const createXlsFromSequelizeResults = ( model, hex = false, toStructuredCsv = false, + excludeOrgUid = false, ) => { rows = JSON.parse(JSON.stringify(rows)); // Sadly this is the best way to simplify sequelize's return shape @@ -96,7 +97,9 @@ export const createXlsFromSequelizeResults = ( sheets[mainColName + 's'] = { name: mainColName + 's', data: [ - Object.keys(row[mainColName]).concat([ + Object.keys(row[mainColName].filter(colName => { + return !(excludeOrgUid && colName === 'orgUid'); + })).concat([ model.name.split('_').join('') + 'Id', ]), ], @@ -113,7 +116,9 @@ export const createXlsFromSequelizeResults = ( if (row[mainColName] === null) { row[mainColName] = 'null'; } - mainXlsRow.push(encodeValue(row[mainColName], hex)); + if (!(excludeOrgUid && mainColName === 'orgUid')) { + mainXlsRow.push(encodeValue(row[mainColName], hex)); + } } } @@ -235,13 +240,20 @@ export const updateTablesWithData = async (tableData) => { // Assign the newly created record to this home org row.orgUid = orgUid; } - - await Staging.upsert({ - uuid: data[model.primaryKeyAttributes[0]], - action: exists ? 'UPDATE' : 'INSERT', - table: model.tableName, - data: JSON.stringify(row), - }); + + const validation = model.validateImport.validate(row); + + if (!validation.error) { + await Staging.upsert({ + uuid: data[model.primaryKeyAttributes[0]], + action: exists ? 'UPDATE' : 'INSERT', + table: model.tableName, + data: JSON.stringify(row), + }); + } else { + validation.error.message += ' on ' + model.name; + throw validation.error; + } } } });