Preserve all emails, phones, and links when merging records (#15224)

This commit is contained in:
neo773
2025-10-21 16:16:22 +02:00
committed by GitHub
parent 45473218d3
commit 139b6dd29b
6 changed files with 162 additions and 22 deletions
@@ -83,7 +83,112 @@ describe('mergeFieldValues', () => {
expect(result).toEqual({
primaryEmail: 'priority@example.com',
additionalEmails: ['extra1@example.com', 'extra2@example.com'],
additionalEmails: [
'first@example.com',
'extra1@example.com',
'extra2@example.com',
],
});
});
it('should merge phones for PHONES field', () => {
const phoneRecords = [
{
value: {
primaryPhoneNumber: '+11234567890',
primaryPhoneCountryCode: 'US',
primaryPhoneCallingCode: '+1',
additionalPhones: [
{
number: '+19876543210',
countryCode: 'US',
callingCode: '+1',
},
],
},
recordId: 'record1',
},
{
value: {
primaryPhoneNumber: '+14445556666',
primaryPhoneCountryCode: 'US',
primaryPhoneCallingCode: '+1',
additionalPhones: [
{
number: '+17778889999',
countryCode: 'US',
callingCode: '+1',
},
],
},
recordId: PRIORITY_RECORD_ID,
},
];
const result = mergeFieldValues(
FieldMetadataType.PHONES,
phoneRecords,
PRIORITY_RECORD_ID,
);
expect(result).toEqual({
primaryPhoneNumber: '+14445556666',
primaryPhoneCountryCode: 'US',
primaryPhoneCallingCode: '+1',
additionalPhones: [
{
number: '+11234567890',
countryCode: 'US',
callingCode: '+1',
},
{
number: '+19876543210',
countryCode: 'US',
callingCode: '+1',
},
{
number: '+17778889999',
countryCode: 'US',
callingCode: '+1',
},
],
});
});
it('should merge links for LINKS field', () => {
const linkRecords = [
{
value: {
primaryLinkUrl: 'https://first.com',
primaryLinkLabel: 'First Link',
secondaryLinks: [{ url: 'https://extra1.com', label: 'Extra 1' }],
},
recordId: 'record1',
},
{
value: {
primaryLinkUrl: 'https://priority.com',
primaryLinkLabel: 'Priority Link',
secondaryLinks: [{ url: 'https://extra2.com', label: 'Extra 2' }],
},
recordId: PRIORITY_RECORD_ID,
},
];
const result = mergeFieldValues(
FieldMetadataType.LINKS,
linkRecords,
PRIORITY_RECORD_ID,
);
expect(result).toEqual({
primaryLinkUrl: 'https://priority.com',
primaryLinkLabel: 'Priority Link',
secondaryLinks: [
{ url: 'https://first.com', label: 'First Link' },
{ url: 'https://extra1.com', label: 'Extra 1' },
{ url: 'https://extra2.com', label: 'Extra 2' },
],
});
});
});
@@ -31,23 +31,28 @@ export const mergeEmailsFieldValues = (
primaryEmail = fallbackRecord?.value.primaryEmail || '';
}
const allAdditionalEmails: string[] = [];
const allEmails: string[] = [];
recordsWithValues.forEach((record) => {
if (hasRecordFieldValue(record.value.primaryEmail)) {
allEmails.push(record.value.primaryEmail);
}
const additionalEmails = parseArrayOrJsonStringToArray<string>(
record.value.additionalEmails,
);
allAdditionalEmails.push(
allEmails.push(
...additionalEmails.filter((email) => hasRecordFieldValue(email)),
);
});
const uniqueAdditionalEmails = Array.from(new Set(allAdditionalEmails));
const uniqueEmails = Array.from(new Set(allEmails)).filter(
(email) => email !== primaryEmail,
);
return {
primaryEmail,
additionalEmails:
uniqueAdditionalEmails.length > 0 ? uniqueAdditionalEmails : null,
additionalEmails: uniqueEmails.length > 0 ? uniqueEmails : null,
};
};
@@ -44,25 +44,33 @@ export const mergeLinksFieldValues = (
}
}
const allSecondaryLinks: LinkMetadata[] = [];
const allLinks: LinkMetadata[] = [];
recordsWithValues.forEach((record) => {
if (hasRecordFieldValue(record.value.primaryLinkUrl)) {
allLinks.push({
url: record.value.primaryLinkUrl,
label: record.value.primaryLinkLabel,
});
}
const secondaryLinks = parseArrayOrJsonStringToArray<LinkMetadata>(
record.value.secondaryLinks,
);
allSecondaryLinks.push(
allLinks.push(
...secondaryLinks.filter((link) => hasRecordFieldValue(link.url)),
);
});
const uniqueSecondaryLinks = uniqBy(allSecondaryLinks, 'url');
const uniqueLinks = uniqBy(allLinks, 'url').filter(
(link) => link.url !== primaryLinkUrl,
);
const result = {
primaryLinkLabel,
primaryLinkUrl,
secondaryLinks:
uniqueSecondaryLinks.length > 0 ? uniqueSecondaryLinks : null,
secondaryLinks: uniqueLinks.length > 0 ? uniqueLinks : null,
};
return result;
@@ -47,11 +47,19 @@ export const mergePhonesFieldValues = (
}
}
const allAdditionalPhones: AdditionalPhoneMetadata[] = [];
const allPhones: AdditionalPhoneMetadata[] = [];
recordsWithValues.forEach((record) => {
if (hasRecordFieldValue(record.value.primaryPhoneNumber)) {
allPhones.push({
number: record.value.primaryPhoneNumber,
countryCode: record.value.primaryPhoneCountryCode,
callingCode: record.value.primaryPhoneCallingCode,
});
}
if (Array.isArray(record.value.additionalPhones)) {
allAdditionalPhones.push(
allPhones.push(
...record.value.additionalPhones.filter((phone) =>
hasRecordFieldValue(phone.number),
),
@@ -59,13 +67,14 @@ export const mergePhonesFieldValues = (
}
});
const uniqueAdditionalPhones = uniqBy(allAdditionalPhones, 'number');
const uniquePhones = uniqBy(allPhones, 'number').filter(
(phone) => phone.number !== primaryPhoneNumber,
);
return {
primaryPhoneNumber,
primaryPhoneCountryCode: primaryPhoneCountryCode!,
primaryPhoneCallingCode,
additionalPhones:
uniqueAdditionalPhones.length > 0 ? uniqueAdditionalPhones : null,
additionalPhones: uniquePhones.length > 0 ? uniquePhones : null,
};
};