-
Notifications
You must be signed in to change notification settings - Fork 0
69 - Able to update an entity with wrong fields from another entity #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c2779b
invalidate submission when systemId is not found
leoraba 71374de
add error when system id not found
leoraba f0e6356
rename func and add unit tests
leoraba 1b3fd70
change message for invalid value batch error
leoraba 5352589
Merge branch 'main' into fix/69-update-another-entity
leoraba 23f59b5
custom submission errors type
leoraba 44334c1
refactoring function
leoraba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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({ | ||
| fieldName: 'systemId', | ||
| fieldValue: submissionEditData.systemId, | ||
| index, | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| if (_.isEmpty(submissionSchemaErrors)) { | ||
| logger.info(LOG_MODULE, `No error found on data submission`); | ||
| } else { | ||
|
|
@@ -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) { | ||
|
|
@@ -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[] = []; | ||
|
|
||
|
|
@@ -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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. validation added to ensure the |
||
| const changeData = _.omit(record, 'systemId'); | ||
| const diffData = computeDataDiff(foundSubmittedData.data, changeData); | ||
| if (!_.isEmpty(diffData.old) && !_.isEmpty(diffData.new)) { | ||
|
|
@@ -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; | ||
| }); | ||
|
|
||
26 changes: 25 additions & 1 deletion
26
packages/data-provider/src/utils/submissionResponseParser.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/data-provider/test/utils/submission/createUnrecognizedFieldBatchError.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| }); | ||
| }); | ||
| }); |
79 changes: 79 additions & 0 deletions
79
packages/data-provider/test/utils/submission/findEditSubmittedData.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
systemIdis recognized, but the value is not found for this entity. This is an invalid value, not unrecognized field.There was a problem hiding this comment.
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_RESTRICTIONreason 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" } } ], }There was a problem hiding this comment.
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_VALUEerror.Example:
{ "index": 0, "reason": "UNRECOGNIZED_VALUE", "fieldName": "systemId", "fieldValue": "NOOOO" }