Skip to content
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

Add business rule to validate Determination taxon with COType #5127

Merged
merged 16 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook } from '@testing-library/react';
import { act, renderHook } from '@testing-library/react';

import { overrideAjax } from '../../../tests/ajax';
import { mockTime, requireContext } from '../../../tests/helpers';
Expand All @@ -9,7 +9,12 @@ import { getResourceApiUrl } from '../resource';
import { useSaveBlockers } from '../saveBlockers';
import { schema } from '../schema';
import { tables } from '../tables';
import type { Taxon, TaxonTreeDefItem } from '../types';
import type {
CollectionObjectType,
Determination,
Taxon,
TaxonTreeDefItem,
} from '../types';

mockTime();
requireContext();
Expand Down Expand Up @@ -49,6 +54,28 @@ describe('Borrow Material business rules', () => {
});

describe('Collection Object business rules', () => {
const collectionObjectTypeUrl = getResourceApiUrl('CollectionObjectType', 1);
const collectionObjectType: Partial<
SerializedResource<CollectionObjectType>
> = {
id: 1,
name: 'Entomology',
taxonTreeDef: getResourceApiUrl('Taxon', 1),
resource_uri: collectionObjectTypeUrl,
};
overrideAjax(collectionObjectTypeUrl, collectionObjectType);

const otherTaxonId = 1;
const otherTaxon: Partial<SerializedResource<Taxon>> = {
id: otherTaxonId,
isAccepted: true,
rankId: 10,
definition: getResourceApiUrl('TaxonTreeDef', 2),
resource_uri: getResourceApiUrl('Taxon', otherTaxonId),
};

overrideAjax(getResourceApiUrl('Taxon', otherTaxonId), otherTaxon);

const collectionObjectlId = 2;
const collectionObjectUrl = getResourceApiUrl(
'CollectionObject',
Expand All @@ -58,6 +85,13 @@ describe('Collection Object business rules', () => {
const getBaseCollectionObject = () =>
new tables.CollectionObject.Resource({
id: collectionObjectlId,
collectionobjecttype: collectionObjectTypeUrl,
determinations: [
{
taxon: getResourceApiUrl('Taxon', otherTaxonId),
preferredTaxon: getResourceApiUrl('Taxon', otherTaxonId),
} as SerializedResource<Determination>,
],
resource_uri: collectionObjectUrl,
});

Expand All @@ -80,6 +114,25 @@ describe('Collection Object business rules', () => {

expect(collectionObject.get('collectingEvent')).toBeDefined();
});

test('Save blocked when CollectionObjectType of a CollectionObject does not have same tree definition as its associated Determination -> taxon', async () => {
const collectionObject = getBaseCollectionObject();

const determination =
collectionObject.getDependentResource('determinations')?.models[0];

const { result } = renderHook(() =>
useSaveBlockers(determination, tables.Determination.getField('taxon'))
);

await act(async () => {
await determination?.businessRuleManager?.checkField('taxon');
});

expect(result.current[0]).toStrictEqual([
'Taxon does not belong to the same tree as this Object Type',
]);
});
});

describe('DNASequence business rules', () => {
Expand Down
sharadsw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { formsText } from '../../localization/forms';
import { resourcesText } from '../../localization/resources';
import type { BusinessRuleResult } from './businessRules';
import type { AnySchema, TableFields } from './helperTypes';
Expand All @@ -17,6 +18,7 @@ import type {
Address,
BorrowMaterial,
CollectionObject,
CollectionObjectType,
Determination,
DNASequence,
LoanPreparation,
Expand Down Expand Up @@ -47,6 +49,7 @@ type MappedBusinessRuleDefs = {
};

const CURRENT_DETERMINATION_KEY = 'determination-isCurrent';
const DETERMINATION_TAXON_KEY = 'determination-taxon';

export const businessRuleDefs: MappedBusinessRuleDefs = {
Address: {
Expand Down Expand Up @@ -153,7 +156,7 @@ export const businessRuleDefs: MappedBusinessRuleDefs = {
fieldChecks: {
taxon: async (
determination: SpecifyResource<Determination>
): Promise<BusinessRuleResult> =>
): Promise<BusinessRuleResult | undefined> =>
determination
.rgetPromise('taxon', true)
.then((taxon: SpecifyResource<Taxon> | null) => {
Expand All @@ -165,6 +168,40 @@ export const businessRuleDefs: MappedBusinessRuleDefs = {
.then(async (accepted) =>
accepted === null ? taxon : getLastAccepted(accepted)
);

const collectionObject = determination.collection?.related;
if (
collectionObject !== undefined &&
collectionObject.specifyTable.name === 'CollectionObject'
)
(collectionObject as SpecifyResource<CollectionObject>)
.rgetPromise('collectionObjectType', true)
.then((coType: SpecifyResource<CollectionObjectType>) => {
/*
* Have to set save blockers directly here to get this working.
* Since following code has to wait for above rgetPromise to resolve, returning a Promise<BusinessRuleResult> for validation here is too slow and
* does not get captured by business rules.
*/
if (
coType.get('taxonTreeDef') ===
(taxon?.get('definition') ?? '')
) {
setSaveBlockers(
determination,
determination.specifyTable.field.taxon,
[],
DETERMINATION_TAXON_KEY
);
} else {
setSaveBlockers(
determination,
determination.specifyTable.field.taxon,
[formsText.invalidTree()],
DETERMINATION_TAXON_KEY
);
}
});

return taxon === null
? {
isValid: true,
Expand Down
3 changes: 3 additions & 0 deletions specifyweb/frontend/js_src/lib/localization/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,4 +1159,7 @@ export const formsText = createDictionary({
'ru-ru': 'Номер по каталогу Числовой',
'uk-ua': 'Каталожний номер Числовий',
},
invalidTree: {
'en-us': 'Taxon does not belong to the same tree as this Object Type',
},
} as const);
Loading