Skip to content
Open
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
Expand Up @@ -248,7 +248,7 @@ describe('autoMerge', () => {
"agentAttachments": [],
"agentGeographies": [],
"agentSpecialties": [],
"agentType": 1,
"agentType": 0,
"catalogerOf": "/api/specify/CollectionObject?cataloger=2305",
"collContentContact": null,
"collTechContact": null,
Expand All @@ -257,7 +257,7 @@ describe('autoMerge', () => {
"date1": null,
"date1Precision": null,
"date2": null,
"date2Precision": null,
"date2Precision": 2,
"dateOfBirth": null,
"dateOfBirthPrecision": null,
"dateOfDeath": null,
Expand Down Expand Up @@ -415,7 +415,7 @@ describe('autoMerge', () => {
"agentAttachments": [],
"agentGeographies": [],
"agentSpecialties": [],
"agentType": 1,
"agentType": 0,
"catalogerOf": "/api/specify/CollectionObject?cataloger=2305",
"collContentContact": null,
"collTechContact": null,
Expand All @@ -424,7 +424,7 @@ describe('autoMerge', () => {
"date1": "2020-01-01",
"date1Precision": null,
"date2": null,
"date2Precision": null,
"date2Precision": 2,
"dateOfBirth": null,
"dateOfBirthPrecision": null,
"dateOfDeath": null,
Expand Down Expand Up @@ -582,7 +582,7 @@ describe('autoMerge', () => {
"agentAttachments": [],
"agentGeographies": [],
"agentSpecialties": [],
"agentType": 1,
"agentType": 0,
"catalogerOf": "/api/specify/CollectionObject?cataloger=2305",
"collContentContact": null,
"collTechContact": null,
Expand All @@ -591,7 +591,7 @@ describe('autoMerge', () => {
"date1": null,
"date1Precision": null,
"date2": null,
"date2Precision": null,
"date2Precision": 2,
"dateOfBirth": null,
"dateOfBirthPrecision": null,
"dateOfDeath": null,
Expand Down Expand Up @@ -636,4 +636,37 @@ describe('autoMerge', () => {
}
`);
});

test('prefers longest string values when auto populating', () => {
const merged = autoMerge(
tables.Agent,
[
addMissingFields('Agent', { lastName: '' }),
addMissingFields('Agent', { lastName: 'Longer Value' }),
addMissingFields('Agent', { lastName: 'Mid' }),
] as unknown as RA<SerializedResource<AnySchema>>,
false
);
expect(merged.lastName).toBe('Longer Value');
});

test('fills dependent precision even if newest record lacks it', () => {
const merged = autoMerge(
tables.Agent,
[
addMissingFields('Agent', {
timestampModified: '2024-01-01',
dateOfDeath: '2023-01-01',
dateOfDeathPrecision: null,
}),
addMissingFields('Agent', {
timestampModified: '2023-01-01',
dateOfDeath: '2023-01-01',
dateOfDeathPrecision: 0,
}),
] as unknown as RA<SerializedResource<AnySchema>>,
false
);
expect(merged.dateOfDeathPrecision).toBe(0);
});
});
25 changes: 20 additions & 5 deletions specifyweb/frontend/js_src/lib/components/Merging/autoMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ function mergeField(
resource[field.name],
]);
const values = parentChildValues.map(([_, child]) => child);
const nonFalsyValues = f.unique(values.filter(Boolean));
const nonFalsyValues = f.unique(
values.filter(
(value) =>
value !== null &&
value !== undefined &&
value !== '' &&
// Preserve zeros and false, but drop NaN
value === value
)
);
const firstValue = nonFalsyValues[0] ?? values[0];
if (field.isRelationship)
if (field.isDependent())
Expand Down Expand Up @@ -150,8 +159,9 @@ function mergeField(
// Pick the longest value
return (
Array.from(nonFalsyValues).sort(
sortFunction((string) =>
typeof string === 'string' ? string.length : 0
sortFunction(
(string) => (typeof string === 'string' ? string.length : 0),
true
)
)[0] ?? firstValue
);
Expand Down Expand Up @@ -213,10 +223,15 @@ function mergeDependentField(
sourceValue: ReturnType<typeof mergeField>
): ReturnType<typeof mergeField> {
const sourceField = strictDependentFields()[fieldName];
const sourceResource = resources.find(
const matchingResources = resources.filter(
(resource) => resource[sourceField] === sourceValue
);
return sourceResource?.[fieldName] ?? null;
const preferredResource =
matchingResources.find((resource) => {
const value = resource[fieldName];
return value !== null && value !== undefined;
}) ?? matchingResources[0];
return preferredResource?.[fieldName] ?? null;
}

/**
Expand Down
56 changes: 31 additions & 25 deletions specifyweb/frontend/js_src/lib/components/Merging/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { icons } from '../Atoms/Icons';
import { Link } from '../Atoms/Link';
import { Submit } from '../Atoms/Submit';
import { LoadingContext } from '../Core/Contexts';
import { addMissingFields } from '../DataModel/addMissingFields';
import { runAllFieldChecks } from '../DataModel/businessRules';
import type { AnySchema, SerializedResource } from '../DataModel/helperTypes';
import type { SpecifyResource } from '../DataModel/legacyTypes';
Expand Down Expand Up @@ -210,31 +211,36 @@ function Merging({
const clones = sortedResources.slice(1);

const [merged, setMerged] = useAsyncState(
React.useCallback(
async () =>
records === undefined || initialRecords.current === undefined
? undefined
: postMergeResource(
initialRecords.current,
autoMerge(
table,
initialRecords.current,
userPreferences.get(
'recordMerging',
'behavior',
'autoPopulate'
),
target.id
)
).then(async (merged) => {
const mergedResource = deserializeResource(
merged as SerializedResource<AnySchema>
);
if (merged !== undefined) await runAllFieldChecks(mergedResource);
return mergedResource;
}),
[table, records]
),
React.useCallback(async () => {
if (
records === undefined ||
initialRecords.current === undefined ||
target === undefined
)
return undefined;

const shouldAutoPopulate = userPreferences.get(
'recordMerging',
'behavior',
'autoPopulate'
);

const mergedPayload = shouldAutoPopulate
? await postMergeResource(
initialRecords.current,
autoMerge(table, initialRecords.current, false, target.id)
)
: addMissingFields(
table.name,
{} as Partial<SerializedResource<AnySchema>>
);

const mergedResource = deserializeResource(
mergedPayload as SerializedResource<AnySchema>
);
if (mergedPayload !== undefined) await runAllFieldChecks(mergedResource);
return mergedResource;
}, [table, records, target]),
true
);

Expand Down