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

COG business rules: isPrimary and isSubstrate #5275

Draft
wants to merge 19 commits into
base: production
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 16 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
Expand Up @@ -1293,7 +1293,7 @@ exports[`tableScoping 1`] = `
"CollectionObjectAttribute": undefined,
"CollectionObjectCitation": "collectionObject",
"CollectionObjectGroup": "collection",
"CollectionObjectGroupJoin": "parentcog > collection",
"CollectionObjectGroupJoin": "parentCog > collection",
"CollectionObjectGroupType": "collection",
"CollectionObjectProperty": "collectionObject",
"CollectionObjectType": "collection",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,48 @@ describe('Collection Object business rules', () => {
});
});

describe('CollectionObjectGroup business rules', () => {
const getBaseCOG = () => {
sharadsw marked this conversation as resolved.
Show resolved Hide resolved
const cog = new tables.CollectionObjectGroup.Resource({
id: 1,
cogType: getResourceApiUrl('CollectionObjectGroupType', 1),
resource_uri: getResourceApiUrl('CollectionObjectGroup', 1),
});

const cojo1 = new tables.CollectionObjectGroupJoin.Resource({
isPrimary: false,
isSubstrate: true,
childCo: getResourceApiUrl('CollectionObject', 1),
parentCog: getResourceApiUrl('CollectionObjectGroup', 1),
});
const cojo2 = new tables.CollectionObjectGroupJoin.Resource({
isPrimary: true,
isSubstrate: false,
childCo: getResourceApiUrl('CollectionObject', 2),
parentCog: getResourceApiUrl('CollectionObjectGroup', 1),
});

cog.set('parentCojos', [cojo1, cojo2]);
return { cog, cojo1, cojo2 };
};

test('Only one CO COJO can be primary', () => {
const { cojo1, cojo2 } = getBaseCOG();
cojo1.set('isPrimary', true);

expect(cojo1.get('isPrimary')).toBe(true);
expect(cojo2.get('isPrimary')).toBe(false);
});

test('Only one CO COJO can be substrate', () => {
const { cojo1, cojo2 } = getBaseCOG();
cojo2.set('isSubstrate', true);

expect(cojo1.get('isSubstrate')).toBe(false);
expect(cojo2.get('isSubstrate')).toBe(true);
});
});

describe('DNASequence business rules', () => {
test('fieldCheck geneSequence', async () => {
const dNASequence = new tables.DNASequence.Resource({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { formsText } from '../../localization/forms';
import { resourcesText } from '../../localization/resources';
import { f } from '../../utils/functools';
import type { BusinessRuleResult } from './businessRules';
import {
CURRENT_DETERMINATION_KEY,
DETERMINATION_TAXON_KEY,
ensureSingleCollectionObjectCheck,
hasNoCurrentDetermination,
} from './businessRuleUtils';
import { cogTypes } from './helpers';
import type { AnySchema, TableFields } from './helperTypes';
import {
checkPrepAvailability,
Expand All @@ -20,6 +27,7 @@ import type {
Address,
BorrowMaterial,
CollectionObject,
CollectionObjectGroupJoin,
Determination,
DNASequence,
LoanPreparation,
Expand Down Expand Up @@ -49,15 +57,6 @@ type MappedBusinessRuleDefs = {
readonly [TABLE in keyof Tables]?: BusinessRuleDefs<Tables[TABLE]>;
};

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

const hasNoCurrentDetermination = (collection: Collection<Determination>) =>
collection.models.length > 0 &&
!collection.models.some((determination: SpecifyResource<Determination>) =>
determination.get('isCurrent')
);

export const businessRuleDefs: MappedBusinessRuleDefs = {
Address: {
customInit: (address) => {
Expand Down Expand Up @@ -202,6 +201,42 @@ export const businessRuleDefs: MappedBusinessRuleDefs = {
},
},

CollectionObjectGroup: {
fieldChecks: {
cogType: (cog): void => {
// The first COJO CO will automatically have isPrimary set to True when the COG type is 'consolidated'
sharadsw marked this conversation as resolved.
Show resolved Hide resolved
cog.rgetPromise('cogType').then((cogtype) => {
if (cogtype.get('type') === cogTypes.CONSOLIDATED) {
const cojos = cog.getDependentResource('parentCojos');
// Set first CO in COG to primary
cojos?.models
.find((cojo) => cojo.get('childCo') !== null)
?.set('isPrimary', true);
}
});
},
},
},

CollectionObjectGroupJoin: {
fieldChecks: {
/*
* Only a single CO in a COG can be set as primary.
* When checking a CO as primary, other COs in that COG will get unchecked.
*/
isPrimary: (cojo: SpecifyResource<CollectionObjectGroupJoin>): void => {
ensureSingleCollectionObjectCheck(cojo, 'isPrimary');
},
/*
* Only a single CO in a COG can be set as substrate.
* When checking a CO as substrate, other COs in that COG will get unchecked.
*/
isSubstrate: (cojo: SpecifyResource<CollectionObjectGroupJoin>): void => {
ensureSingleCollectionObjectCheck(cojo, 'isSubstrate');
},
},
},

Determination: {
fieldChecks: {
taxon: async (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { SpecifyResource } from './legacyTypes';
import type { Collection } from './specifyTable';
import type { CollectionObjectGroupJoin, Determination } from './types';

// Save blocker keys used in businessRuleDefs.ts
export const CURRENT_DETERMINATION_KEY = 'determination-isCurrent';
export const DETERMINATION_TAXON_KEY = 'determination-taxon';

/**
*
* Calculates whether a collection of determinations has any current determinations or not
* Used in CO -> Determination -> isCurrent business rule
*/
export const hasNoCurrentDetermination = (
collection: Collection<Determination>
) =>
collection.models.length > 0 &&
!collection.models.some((determination: SpecifyResource<Determination>) =>
determination.get('isCurrent')
);

/**
*
* Ensures only one CO in a COG can be checked as isPrimary or isSubstrate
* Used in COG business rules: https://github.com/specify/specify7/issues/5246
*/
export const ensureSingleCollectionObjectCheck = (
cojo: SpecifyResource<CollectionObjectGroupJoin>,
field: 'isPrimary' | 'isSubstrate'
) => {
if (cojo.get(field) && cojo.collection !== undefined) {
cojo.collection.models
.filter((resource) => resource.get('childCo') !== null)
.forEach((other: SpecifyResource<CollectionObjectGroupJoin>) => {
if (other.cid !== cojo.cid) {
other.set(field, false);
}
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,10 @@ export async function fetchDistantRelated(
field,
};
}

// Cog types: Discrete, Consolidated, Drill Core
export const cogTypes = {
DISCRETE: 'Discrete',
CONSOLIDATED: 'Consolidated',
DRILL_CORE: 'Drill Core',
};
Loading