Skip to content

Commit

Permalink
Only remove determination when different from COType tree def
Browse files Browse the repository at this point in the history
  • Loading branch information
CarolineDenis committed Aug 9, 2024
1 parent 77ded18 commit 6dcf055
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
updateLoanPrep,
} from './interactionBusinessRules';
import type { SpecifyResource } from './legacyTypes';
import { fetchResource, idFromUrl } from './resource';
import { setSaveBlockers } from './saveBlockers';
import type { Collection } from './specifyTable';
import { tables } from './tables';
Expand Down Expand Up @@ -160,12 +161,35 @@ export const businessRuleDefs: MappedBusinessRuleDefs = {
collectionObjectType: (resource) => {
// Delete all determinations
const determinations = resource.getDependentResource('determinations');
while (
determinations !== undefined &&
determinations.models.length > 0
) {
determinations.remove(determinations.models[0]);
}
const taxon = determinations?.models[0].get('taxon');
const taxonId = idFromUrl(taxon ?? '');
const COType = resource.get('collectionObjectType');
const COTypeId = idFromUrl(COType);

const fetchTaxon = fetchResource('Taxon', taxonId ?? 0);
const fetchCOType = fetchResource(
'CollectionObjectType',
COTypeId ?? 0
);

Promise.all([fetchTaxon, fetchCOType])
.then(([taxonResource, COTypeResource]) => {
const taxonTreeDefinition = taxonResource.definition;
const COTypeTreeDefinition = COTypeResource.taxonTreeDef;

// Remove all models from determinations
if (
taxonTreeDefinition !== COTypeTreeDefinition &&
determinations
) {
while (determinations.models.length > 0) {
determinations.remove(determinations.models[0]);
}
}
})
.catch((error) => {
console.error('Error fetching resources:', error);
});
},
},
},
Expand Down

0 comments on commit 6dcf055

Please sign in to comment.