Skip to content

Commit

Permalink
feat: validation on models during import, and optional exclusion of o…
Browse files Browse the repository at this point in the history
…rguid
  • Loading branch information
mkeen committed Jan 29, 2022
1 parent fefd304 commit 75b6b57
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
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 @@ -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,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
createXlsFromSequelizeResults,
transformFullXslsToChangeList,
} from '../../utils/xls';
import {unitsUpdateSchema} from "../../validations/index.js";

const { Model } = Sequelize;

Expand Down Expand Up @@ -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),
);
Expand Down
30 changes: 21 additions & 9 deletions src/utils/xls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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',
]),
],
Expand All @@ -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));
}
}
}

Expand Down Expand Up @@ -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;
}
}
}
});
Expand Down

0 comments on commit 75b6b57

Please sign in to comment.