Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions packages/data-provider/src/services/submission/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import { getDictionarySchemaRelations, type SchemaChildNode } from '../../utils/
import { BadRequest } from '../../utils/errors.js';
import { convertRecordToString } from '../../utils/formatUtils.js';
import { parseRecordsToInsert } from '../../utils/recordsParser.js';
import { createUnrecognizedFieldBatchError } from '../../utils/submissionResponseParser.js';
import {
extractSchemaDataFromMergedDataRecords,
filterDeletesFromUpdates,
filterRelationsForPrimaryIdUpdate,
findEditSubmittedData,
findInvalidRecordErrorsBySchemaName,
groupSchemaErrorsByEntity,
mapGroupedUpdateSubmissionData,
Expand Down Expand Up @@ -421,6 +423,39 @@ const processor = (dependencies: BaseDependencies) => {
dataValidated: dataMergedByEntityName,
});

// Check for records to be updated that its systemId was not found in the Submitted Data collection.
// Any error found will cause the submission to be marked as 'invalid'
Object.entries(submissionData.updates ?? {}).forEach(([entityName, recordsToUpdate]) => {
recordsToUpdate.forEach((submissionEditData, index) => {
const found = findEditSubmittedData(entityName, submissionEditData.systemId, dataMergedByEntityName);

if (found) {
return;
}

logger.error(
LOG_MODULE,
`Record with systemId '${submissionEditData.systemId}' not found in entity '${entityName}'`,
);

if (!submissionSchemaErrors.updates) {
submissionSchemaErrors.updates = {};
}

if (!submissionSchemaErrors.updates[entityName]) {
submissionSchemaErrors.updates[entityName] = [];
}

submissionSchemaErrors.updates[entityName].push(
createUnrecognizedFieldBatchError({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the correct error name? The field systemId is recognized, but the value is not found for this entity. This is an invalid value, not unrecognized field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated code to return a INVALID_BY_RESTRICTION reason and an error object because Lectern does not provide a reason for 'INVALID_VALUE'.
Example:

{
  "fieldName": "systemId",
  "fieldValue": "ABC12456465",
  "reason": "INVALID_BY_RESTRICTION",
  "index": 0,
  "errors": [
    {
      "message": "Value does not match any existing record.",
      "restriction": {
        "rule": true,
        "type": "required"
      }
    }
  ],
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated code to return a UNRECOGNIZED_VALUE error.
Example:

{
          "index": 0,
          "reason": "UNRECOGNIZED_VALUE",
          "fieldName": "systemId",
          "fieldValue": "NOOOO"
        }

fieldName: 'systemId',
fieldValue: submissionEditData.systemId,
index,
}),
);
});
});

if (_.isEmpty(submissionSchemaErrors)) {
logger.info(LOG_MODULE, `No error found on data submission`);
} else {
Expand Down Expand Up @@ -474,7 +509,7 @@ const processor = (dependencies: BaseDependencies) => {
// Parse file data
const recordsParsed = records.map(convertRecordToString).map(parseToSchema(schema));

const filesDataProcessed = await compareUpdatedData(recordsParsed);
const filesDataProcessed = await compareUpdatedData(recordsParsed, schema.name);

const currentDictionary = await getDictionaryById(submission.dictionaryId);
if (!currentDictionary) {
Expand Down Expand Up @@ -560,11 +595,12 @@ const processor = (dependencies: BaseDependencies) => {
/**
* Processes a list of data records and compares them with previously submitted data.
* @param {DataRecord[]} records An array of data records to be processed
* @param {string} schemaName The name of the schema associated with the records
* @returns {Promise<SubmissionUpdateData[]>} An array of `SubmissionUpdateData` objects. Each object
* contains the `systemId`, `old` data, and `new` data representing the differences
* between the previously submitted data and the updated record.
*/
const compareUpdatedData = async (records: DataRecord[]): Promise<SubmissionUpdateData[]> => {
const compareUpdatedData = async (records: DataRecord[], schemaName: string): Promise<SubmissionUpdateData[]> => {
const { getSubmittedDataBySystemId } = submittedRepository(dependencies);
const results: SubmissionUpdateData[] = [];

Expand All @@ -576,6 +612,18 @@ const processor = (dependencies: BaseDependencies) => {

const foundSubmittedData = await getSubmittedDataBySystemId(systemId);
if (foundSubmittedData?.data) {
if (foundSubmittedData.entityName !== schemaName) {
logger.error(
LOG_MODULE,
`Entity name mismatch for system ID '${systemId}': expected '${schemaName}', found '${foundSubmittedData.entityName}'`,
);
results.push({
systemId: systemId,
old: {},
new: {},
});
return;
}
Comment on lines +618 to +629
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validation added to ensure the systemId belongs to the specified entityName.

const changeData = _.omit(record, 'systemId');
const diffData = computeDataDiff(foundSubmittedData.data, changeData);
if (!_.isEmpty(diffData.old) && !_.isEmpty(diffData.new)) {
Expand All @@ -585,6 +633,13 @@ const processor = (dependencies: BaseDependencies) => {
new: diffData.new,
});
}
} else {
logger.error(LOG_MODULE, `No submitted data found for system ID '${systemId}'`);
results.push({
systemId: systemId,
old: {},
new: {},
});
}
return;
});
Expand Down
26 changes: 25 additions & 1 deletion packages/data-provider/src/utils/submissionResponseParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import type { DataRecord } from '@overture-stack/lectern-client';
import type { DataRecord, DictionaryValidationRecordErrorDetails } from '@overture-stack/lectern-client';
import type { SubmissionInsertData } from '@overture-stack/lyric-data-model/models';

/**
* Creates an error object for unrecognized fields in a batch submission.
*/
export const createUnrecognizedFieldBatchError = ({
fieldName,
fieldValue,
index,
}: {
fieldName: string;
fieldValue: string;
index: number;
}) => {
const errorDetails: DictionaryValidationRecordErrorDetails = {
fieldName,
fieldValue,
reason: 'UNRECOGNIZED_FIELD',
};

return {
index,
...errorDetails,
};
};

export const createBatchResponse = (schemaName: string, records: DataRecord[]): SubmissionInsertData => {
return { batchName: schemaName, records };
};
18 changes: 18 additions & 0 deletions packages/data-provider/src/utils/submissionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ export const extractSchemaDataFromMergedDataRecords = (
return _.mapValues(mergeDataRecordsByEntityName, (mappingArray) => mappingArray.map((o) => o.dataRecord));
};

/**
* Checks whether a record exists within a collection of submitted data records marked for update.
* The lookup is performed by matching the given 'entityName' and 'systemId'.
*
* @Returns true if found, false otherwise
*/
export const findEditSubmittedData = (
entityName: string,
systemId: string,
dataByEntityName: Record<string, DataRecordReference[]>,
) => {
return (
dataByEntityName[entityName]?.some(
(data) =>
data.reference.type === MERGE_REFERENCE_TYPE.EDIT_SUBMITTED_DATA && data.reference.systemId === systemId,
) ?? false
);
};
/**
* Finds and returns a list of invalid records based on a provided schema name.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { createUnrecognizedFieldBatchError } from '../../../src/utils/submissionResponseParser.js';

describe('Submission Utils - Create Unrecognized Field Batch Error', () => {
it('should create an error object with correct structure and values', () => {
const result = createUnrecognizedFieldBatchError({
fieldName: 'systemId',
fieldValue: 'DOESNOTEXIST',
index: 3,
});

expect(result).to.eql({
index: 3,
fieldName: 'systemId',
fieldValue: 'DOESNOTEXIST',
reason: 'UNRECOGNIZED_FIELD',
});
});

it('should handle zero index correctly', () => {
const result = createUnrecognizedFieldBatchError({
fieldName: '',
fieldValue: '',
index: 0,
});

expect(result).to.eql({
index: 0,
fieldName: '',
fieldValue: '',
reason: 'UNRECOGNIZED_FIELD',
});
});

it('should handle empty values correctly', () => {
const result = createUnrecognizedFieldBatchError({
fieldName: '',
fieldValue: '',
index: 2,
});

expect(result).to.eql({
index: 2,
fieldName: '',
fieldValue: '',
reason: 'UNRECOGNIZED_FIELD',
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { findEditSubmittedData } from '../../../index.js';
import { type DataRecordReference, MERGE_REFERENCE_TYPE } from '../../../src/utils/types.js';

const recordsByEntityName: Record<string, DataRecordReference[]> = {
animals: [
{
dataRecord: { name: 'Bird', color: 'blue' },
reference: {
systemId: 'BB4546',
submissionId: 2,
index: 0,
type: MERGE_REFERENCE_TYPE.EDIT_SUBMITTED_DATA,
},
},
{
dataRecord: { name: 'Dinosaur', color: 'red' },
reference: {
systemId: 'DINO8912',
submissionId: 2,
index: 1,
type: MERGE_REFERENCE_TYPE.EDIT_SUBMITTED_DATA,
},
},
],
teams: [
{
dataRecord: { title: 'Raptors' },
reference: {
systemId: 'RPT5678',
submissionId: 2,
index: 3,
type: MERGE_REFERENCE_TYPE.EDIT_SUBMITTED_DATA,
},
},
{
dataRecord: { tile: 'Blue Jays' },
reference: {
systemId: 'BJ1425',
submittedDataId: 45,
type: MERGE_REFERENCE_TYPE.SUBMITTED_DATA,
},
},
],
};

describe('Submission Utils - Find Edited Submitted Data by systemId', () => {
it('should return true when matching entityName and systemId and marked to edit', () => {
const result = findEditSubmittedData('animals', 'BB4546', recordsByEntityName);
expect(result).to.be.true;
});

it('should return false when matching systemId but not entityName', () => {
const result = findEditSubmittedData('teams', 'BB4546', recordsByEntityName);
expect(result).to.be.false;
});

it('should return false when matching systemId and entityName but not marked to edit', () => {
const result = findEditSubmittedData('teams', 'BJ1425', recordsByEntityName);
expect(result).to.be.false;
});

it('should return false when systemId not found', () => {
const result = findEditSubmittedData('animals', 'DOESNOTEXISTS', recordsByEntityName);
expect(result).to.be.false;
});

it('should return false when entityName does not exist', () => {
const result = findEditSubmittedData('incorrect', 'DINO8912', recordsByEntityName);
expect(result).to.be.false;
});

it('should return false when there are no records to find', () => {
const result = findEditSubmittedData('animals', 'DINO8912', {});
expect(result).to.be.false;
});
});