Skip to content

Commit

Permalink
Update transformation logic to better handle partial records and pare…
Browse files Browse the repository at this point in the history
…nt ids
  • Loading branch information
NickPhura committed Dec 21, 2021
1 parent 414ba8f commit 52f1f5a
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 80 deletions.
3 changes: 2 additions & 1 deletion api/src/json-schema/transformation-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ describe('example submission transformation schema', () => {
{
condition: {
if: {
columns: ['Lone Cows']
columns: ['Lone Cows'],
not: true
}
},
transformations: [
Expand Down
4 changes: 4 additions & 0 deletions api/src/json-schema/transformation-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ export const submissionTransformationSchema = {
properties: {
if: {
type: 'object',
required: ['columns'],
properties: {
columns: {
type: 'array',
items: {
type: 'string'
}
},
not: {
type: 'boolean'
}
},
additionalProperties: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type TransformationFieldsSchema = {
export type Condition = {
if: {
columns: string[];
not?: boolean;
};
};

Expand Down
30 changes: 15 additions & 15 deletions api/src/utils/media/xlsx/transformation/xlsx-transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,17 +506,24 @@ export class XLSXTransformation {
const columnValueParts = this._getColumnValueParts(rowObject, condition.if.columns);

if (!columnValueParts || !columnValueParts.length) {
if (condition.if.not) {
// return true if no condition column values are defined, when condition is inverted
return true;
}

// return false if no condition column values are defined
return false;
}

for (const columnValuePart in columnValueParts) {
if (columnValuePart === undefined || columnValuePart === null || columnValuePart === '') {
return false;
}
if (condition.if.not) {
// return false if any condition column values are defined, when condition is inverted
return false;
}

// all conditions met
return true;
// return true if all condition columns are defined
return !columnValueParts.every(
(columnValuePart) => columnValuePart === undefined || columnValuePart === null || columnValuePart === ''
);
}

/**
Expand Down Expand Up @@ -565,15 +572,8 @@ export class XLSXTransformation {
}
}

const newRowObjectKeys = Object.keys(newRowObject);

if (
parseSchema?.condition &&
!parseSchema?.condition?.if?.columns.every((conditionalFields) =>
newRowObjectKeys.includes(conditionalFields)
)
) {
// If the `newRowObject` is missing any of the conditional fields, skip this record
if (!Object.keys(newRowObject).length) {
// row object is empty, skip
return;
}

Expand Down
Loading

0 comments on commit 52f1f5a

Please sign in to comment.