Skip to content

Commit

Permalink
feat: single step xls upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jun 14, 2022
1 parent 1e40b00 commit a055e76
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
9 changes: 4 additions & 5 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
sendXls,
updateTableWithData,
collapseTablesData,
transformMetaUid,
} from '../utils/xls';
import { formatModelAssociationName } from '../utils/model-utils.js';

Expand Down Expand Up @@ -219,13 +220,11 @@ export const updateFromXLS = async (req, res) => {
throw new Error('File Not Received');
}

const xlsxParsed = xlsx.parse(files.xlsx.data);
const xlsxParsed = transformMetaUid(xlsx.parse(files.xlsx.data));
const stagedDataItems = tableDataFromXlsx(xlsxParsed, Project);
const collapsedData = collapseTablesData(stagedDataItems, Project);

await updateTableWithData(
collapseTablesData(stagedDataItems, Project),
Project,
);
await updateTableWithData(collapsedData, Project);

res.json({
message: 'Updates from xlsx added to staging',
Expand Down
23 changes: 13 additions & 10 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import _ from 'lodash';
import { uuid as uuidv4 } from 'uuidv4';
import { Sequelize } from 'sequelize';
import xlsx from 'node-xlsx';

import { Staging, Unit, Label, Issuance, Organization } from '../models';

Expand All @@ -29,8 +30,9 @@ import {
sendXls,
tableDataFromXlsx,
updateTableWithData,
transformMetaUid,
} from '../utils/xls';
import xlsx from 'node-xlsx';

import { formatModelAssociationName } from '../utils/model-utils.js';

export const create = async (req, res) => {
Expand Down Expand Up @@ -165,13 +167,12 @@ export const findAll = async (req, res) => {
}

const results = await Unit.findAndCountAll({
where,
distinct: true,
order: resultOrder,
...columnsToInclude(columns, includes),
...paginationParams(page, limit),
});

where,
distinct: true,
order: resultOrder,
...columnsToInclude(columns, includes),
...paginationParams(page, limit),
});

const response = optionallyPaginatedResponse(results, page, limit);

Expand Down Expand Up @@ -233,9 +234,11 @@ export const updateFromXLS = async (req, res) => {
throw new Error('File Not Received');
}

const xlsxParsed = xlsx.parse(files.xlsx.data);
const xlsxParsed = transformMetaUid(xlsx.parse(files.xlsx.data));
const stagedDataItems = tableDataFromXlsx(xlsxParsed, Unit);
await updateTableWithData(collapseTablesData(stagedDataItems, Unit), Unit);
const collapsedData = collapseTablesData(stagedDataItems, Unit);

await updateTableWithData(collapsedData, Unit);

res.json({
message: 'Updates from xlsx added to staging',
Expand Down
70 changes: 50 additions & 20 deletions src/utils/xls.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,31 +536,44 @@ export const collapseTablesData = (tableData, model) => {
}

const dataKey = formatModelAssociationName(association);
data[dataKey] = tableData[association.model.name]?.data?.find((row) => {
let found = false;
const foundRecord = tableData[association.model.name]?.data?.find(
(row) => {
let found = false;

if (association.model.name === 'issuance' && !association.pluralize) {
if (
row[model.name + 'Id'] === data[association.model.name + 'Id']
) {
found = true;
delete row[model.name + 'Id'];
}
} else {
let comparedToData = null;
const primaryKey =
tableData[model.name]?.model?.primaryKeyAttributes[0];

if (association.model.name === 'issuance' && !association.pluralize) {
if (row[model.name + 'Id'] === data[association.model.name + 'Id']) {
found = true;
delete row[model.name + 'Id'];
}
} else {
let comparedToData = null;
const primaryKey =
tableData[model.name]?.model?.primaryKeyAttributes[0];
if (tableRowData != null && primaryKey != null) {
comparedToData = tableRowData[primaryKey];
}

if (tableRowData != null && primaryKey != null) {
comparedToData = tableRowData[primaryKey];
}
if (row[model.name + 'Id'] === comparedToData) {
found = true;
delete row[model.name + 'Id'];
}

if (row[model.name + 'Id'] === comparedToData) {
found = true;
delete row[model.name + 'Id'];
if (
row['warehouseProjectId'] === comparedToData ||
row['warehouseUnitId'] === comparedToData
) {
found = true;
delete row[model.name + 'Id'];
}
}
}

return found;
});
return found;
},
);
data[dataKey] = foundRecord;
});
});

Expand Down Expand Up @@ -951,3 +964,20 @@ function getExcludedColumns(items) {

return excludedColumns;
}

// Converts the metaUids to actual Uids
export const transformMetaUid = (xlsxParsed) => {
let xlsxParseSerialized = JSON.stringify(xlsxParsed, 2, 2);
const metaUids = _.uniq(
[...xlsxParseSerialized.matchAll(/(NEW-[0-9])/g)].map((item) => item[0]),
);
metaUids.forEach((metaId) => {
const uuid = uuidv4();
xlsxParseSerialized = xlsxParseSerialized.replace(
new RegExp(metaId, 'g'),
uuid,
);
});

return JSON.parse(xlsxParseSerialized);
};

0 comments on commit a055e76

Please sign in to comment.