diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index 34f53a47..6633074b 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -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(); diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index b008e7dc..e292a97c 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -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(); @@ -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; diff --git a/src/models/staging/staging.model.js b/src/models/staging/staging.model.js index b23e8670..e362cf68 100644 --- a/src/models/staging/staging.model.js +++ b/src/models/staging/staging.model.js @@ -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) }, diff --git a/src/utils/xls.js b/src/utils/xls.js index 64460bc4..62014af9 100644 --- a/src/utils/xls.js +++ b/src/utils/xls.js @@ -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',