Skip to content

Commit

Permalink
fix: excel download for external projects/units
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius authored and MariusDec committed Mar 3, 2022
1 parent 14d8861 commit 387c612
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const findAll = async (req, res) => {
try {
await assertDataLayerAvailable();
let { page, limit, search, orgUid, columns, xls } = req.query;
let where = orgUid ? { orgUid } : undefined;
let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;

const includes = Project.getAssociatedModels();

Expand Down
10 changes: 7 additions & 3 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const findAll = async (req, res) => {
await assertDataLayerAvailable();

let { page, limit, columns, orgUid, search, xls } = req.query;
let where = orgUid ? { orgUid } : undefined;
let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;

const includes = Unit.getAssociatedModels();

Expand Down Expand Up @@ -383,9 +383,13 @@ export const split = async (req, res) => {
let lastAvailableUnitBlock = unitBlockStart;

const splitRecords = await Promise.all(
req.body.records.map(async (record) => {
req.body.records.map(async (record, index) => {
const newRecord = _.cloneDeep(originalRecord);
newRecord.warehouseUnitId = uuidv4();

if (index > 0) {
newRecord.warehouseUnitId = uuidv4();
}

newRecord.unitCount = record.unitCount;

const newUnitBlockStart = lastAvailableUnitBlock;
Expand Down
18 changes: 10 additions & 8 deletions src/models/staging/staging.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ class Staging extends Model {
static cleanUpCommitedAndInvalidRecords = async () => {
const stagingRecords = await Staging.findAll({ raw: true });

const stagingRecordsToDelete = stagingRecords.filter((record) => {
if (record.commited === 1) {
const { uuid, table, action, data } = record;
const diff = Staging.getDiffObject(uuid, table, action, data);
return diff.original == null;
}
return false;
});
const stagingRecordsToDelete = await Promise.all(
stagingRecords.filter(async (record) => {
if (record.commited === 1) {
const { uuid, table, action, data } = record;
const diff = await Staging.getDiffObject(uuid, table, action, data);
return diff.original == null;
}
return false;
}),
);

await Staging.destroy({
where: { uuid: stagingRecordsToDelete.map((record) => record.uuid) },
Expand Down
32 changes: 31 additions & 1 deletion src/utils/xls.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,41 @@ export function createXlsFromSequelizeResults({
}) {
// TODO MariusD: Test with null values

// Unsure if this is need, therefore just left the semi-deep-clone here. The assumption is that it wants to remove all functions from the prototypes.
const rowsClone = JSON.parse(JSON.stringify(rows)); // Sadly this is the best way to simplify sequelize's return shape

const uniqueColumns = buildColumnMap(rowsClone);

if (!toStructuredCsv) {
// Remove auto-generated columns that don't make sense to the user
const columnsToRemove = ['createdAt', 'updatedAt', 'timeStaged'];
uniqueColumns.columns.forEach((columns, key, map) => {
let indexesToRemove = [];

columnsToRemove.forEach((columnToRemove) => {
indexesToRemove.push(
columns.findIndex((column) => column === columnToRemove),
);
});

indexesToRemove = indexesToRemove.filter((index) => index >= 0);

// Sort the indexes in reverse order
indexesToRemove.sort((first, second) => {
return second - first;
});

if (indexesToRemove.length > 0) {
const newColumns = [...columns];

indexesToRemove.forEach((index) => {
newColumns.splice(index, 1);
});

map.set(key, newColumns);
}
});
}

const columnTransformations = {
[model.name]: {
issuance: 'issuanceId',
Expand Down

0 comments on commit 387c612

Please sign in to comment.