Skip to content

Commit

Permalink
Merge pull request #5100 from specify/issue-5088b
Browse files Browse the repository at this point in the history
MOTs business logic: Clear CO form when CO Type changes
  • Loading branch information
sharadsw authored Aug 1, 2024
2 parents 067fd09 + ad8a08f commit 84642bc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ describe('Collection Object business rules', () => {
} as SerializedResource<Determination>,
],
resource_uri: collectionObjectUrl,
description: 'Base collection object',
catalogNumber: '123',
});

const orginalEmbeddedCollectingEvent = schema.embeddedCollectingEvent;
Expand Down Expand Up @@ -133,6 +135,39 @@ describe('Collection Object business rules', () => {
'Taxon does not belong to the same tree as this Object Type',
]);
});

const otherCollectionObjectTypeUrl = getResourceApiUrl(
'CollectionObjectType',
2
);
const otherCollectionObjectType: Partial<
SerializedResource<CollectionObjectType>
> = {
id: 2,
name: 'Fossil',
taxonTreeDef: getResourceApiUrl('Taxon', 2),
resource_uri: otherCollectionObjectTypeUrl,
};
overrideAjax(otherCollectionObjectTypeUrl, otherCollectionObjectType);

test('CollectionObject determinations clear when CollectionObjectType changes', async () => {
const collectionObject = getBaseCollectionObject();
collectionObject.set('collectionObjectType', otherCollectionObjectTypeUrl);

const determinations =
collectionObject.getDependentResource('determinations');

expect(determinations?.models.length).toBe(0);
});

test('CollectionObject simple fields clear when CollectionObjectType changes', async () => {
const collectionObject = getBaseCollectionObject();
collectionObject.set('collectionObjectType', otherCollectionObjectTypeUrl);

// Catalog Number must get ignored when clearing
expect(collectionObject.get('catalogNumber')).toBeDefined();
expect(collectionObject.get('description')).toBeNull();
});
});

describe('DNASequence business rules', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { formsText } from '../../localization/forms';
import { resourcesText } from '../../localization/resources';
import { f } from '../../utils/functools';
import { relationshipIsToMany } from '../WbPlanView/mappingHelpers';
import type { BusinessRuleResult } from './businessRules';
import type { AnySchema, TableFields } from './helperTypes';
import {
Expand Down Expand Up @@ -150,6 +152,44 @@ export const businessRuleDefs: MappedBusinessRuleDefs = {
);
}
},
fieldChecks: {
collectionObjectType: (resource) => {
// Fields ignored for better UX/backend restrictions
const fieldsToIgnore = [
'cataloger',
'catalogNumber',
'collection',
'collectionObjectType',
'version',
];

// Clear all fields of CO except to-many dependents
resource.specifyTable.fields
.filter((field) => !f.includes(fieldsToIgnore, field.name))
.map((field) => {
const fieldName = field.name as keyof (CollectionObject['fields'] &
CollectionObject['toManyDependent'] &
CollectionObject['toManyIndependent'] &
CollectionObject['toOneDependent'] &
CollectionObject['toOneIndependent']);
if (
!field.isRelationship ||
!relationshipIsToMany(field) ||
!field.isDependent()
)
resource.set(fieldName, null);
});

// Delete all determinations
const determinations = resource.getDependentResource('determinations');
while (
determinations !== undefined &&
determinations.models.length > 0
) {
determinations.remove(determinations.models[0]);
}
},
},
},

Determination: {
Expand Down

0 comments on commit 84642bc

Please sign in to comment.