@aglyn/plugins-crm 1.29.1 → 1.29.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -14
- package/src/lib/transfer/aliases.js +8 -2
- package/src/lib/transfer/aliases.js.map +1 -1
- package/src/lib/transfer/contact-import-steps.d.ts +36 -0
- package/src/lib/transfer/contact-import-steps.js +81 -0
- package/src/lib/transfer/contact-import-steps.js.map +1 -0
- package/src/lib/transfer/contacts.js +119 -17
- package/src/lib/transfer/contacts.js.map +1 -1
- package/src/lib/transfer/customer-consent-plan.d.ts +90 -0
- package/src/lib/transfer/customer-consent-plan.js +258 -0
- package/src/lib/transfer/customer-consent-plan.js.map +1 -0
- package/src/lib/transfer/customer-sources.d.ts +68 -0
- package/src/lib/transfer/customer-sources.js +420 -0
- package/src/lib/transfer/customer-sources.js.map +1 -0
- package/src/lib/transfer/fields.js +7 -3
- package/src/lib/transfer/fields.js.map +1 -1
- package/src/lib/transfer/imported-consent.server.d.ts +74 -0
- package/src/lib/transfer/imported-consent.server.js +241 -0
- package/src/lib/transfer/imported-consent.server.js.map +1 -0
- package/src/lib/transfer/ui.d.ts +5 -1
- package/src/lib/transfer/ui.js +21 -2
- package/src/lib/transfer/ui.js.map +1 -1
|
@@ -38,11 +38,21 @@ import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
|
38
38
|
*
|
|
39
39
|
* ## What a file may not do
|
|
40
40
|
*
|
|
41
|
-
* The locked rules: an existing contact's email is who they are;
|
|
42
|
-
* never
|
|
41
|
+
* The locked rules: an existing contact's email is who they are; the
|
|
42
|
+
* Marketing consent column is never read as consent; a stage moves forward only; Do not call is
|
|
43
43
|
* turned on by a file and never off; the full name follows the first and
|
|
44
44
|
* last names once either is set. The plan holds each one back by row and
|
|
45
45
|
* names it, so the person acknowledges it before anything is written.
|
|
46
|
+
*
|
|
47
|
+
* ## A store's marketing choices (AGL-4245)
|
|
48
|
+
*
|
|
49
|
+
* A Shopify, Wix or Squarespace customer file records whether each person
|
|
50
|
+
* agreed to marketing email. Its column is read into an import-only field and
|
|
51
|
+
* carried by `customer-consent-plan.ts` (the plan) and
|
|
52
|
+
* `imported-consent.server.ts` (the write and its undo): an opt-in only on
|
|
53
|
+
* the Permission step's statement, an opt-out always, and never an opt-in
|
|
54
|
+
* over somebody who unsubscribed here. Undo takes back the opt-ins and never
|
|
55
|
+
* the opt-outs: those are the person's own choice, and outlive the import.
|
|
46
56
|
*=========================================*/ import { composeContactName, crmDefaultScopeOf, ORG_CONTACT_HOLDER_ID, contactDisplayName, contactFacetPath, CRM_COLLECTIONS, interactionsForGroup, isContactLifecycleStage, normalizeContactBirthdate, normalizePhone, planContactDetach, readContactFacet, readMarketingBasis } from "@aglyn/aglyn/server";
|
|
47
57
|
import { CONTACT_LIFECYCLE_STAGE_LABELS } from "@aglyn/aglyn/app-utils/crm";
|
|
48
58
|
import { CONTACT_EXTRA_PHONE_FIELDS as PHONE_FIELDS } from "@aglyn/aglyn/app-utils/contacts";
|
|
@@ -57,6 +67,9 @@ import { cachedContactReader, clearReportsToOf, CONTACT_REPORTS_TO_LOOP_REFUSAL,
|
|
|
57
67
|
import { contactPatch, settleCompanyCounts } from "../server/contact-update.js";
|
|
58
68
|
import { sweepDealContactRoles } from "../server/deal-contact-roles.js";
|
|
59
69
|
import { CONTACT_ALIASES } from "./aliases.js";
|
|
70
|
+
import { consentOptInEmails, importedConsentChange, planContactConsent, withoutCustomerSourceFields } from "./customer-consent-plan.js";
|
|
71
|
+
import { CUSTOMER_FIELD, customerFileSource, MARKETING_CONSENT_FIELD_ID, readContactConsentAnswer } from "./customer-sources.js";
|
|
72
|
+
import { readImportedConsentUndo, readSuppressedAddresses, revertImportedConsent, writeImportedConsent } from "./imported-consent.server.js";
|
|
60
73
|
import { addCrmPicklistValues, addressFromValues, addressValues, countCrmExport, crmCustomDefinitions, crmMemberEmails, crmMembersTarget, crmPicklistLists, crmRecordsRoom, crmTransferEnv, customTransferValues, customWriteValues, eachWithinBudget, failureMessage, holdBackChanges, IN_LIMIT, isoOf, lookupByField, lookupById, lookupResult, pickValues, plannedValues, readCrmExportPage, requireCrmSuite, requireSite, textOf, TransferEngineError, visibleIn } from "./common.js";
|
|
61
74
|
import { companyForName } from "./companies.js";
|
|
62
75
|
import { addContactByHand } from "../server/add-contact-by-hand.js";
|
|
@@ -415,6 +428,40 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
415
428
|
skipped
|
|
416
429
|
};
|
|
417
430
|
}
|
|
431
|
+
/** Why a row's marketing choice was not written, when the write says. */ function consentMessage(outcome) {
|
|
432
|
+
return outcome && 'message' in outcome ? outcome.message : undefined;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* The fields undo may take back: every changed one, but a refusal the file
|
|
436
|
+
* carried only stays. An unsubscribe is the person's own choice (AGL-4245).
|
|
437
|
+
*/ function undoableFields(changed, consent) {
|
|
438
|
+
return consent && 'value' in consent && consent.value === false ? changed.filter((fieldId)=>fieldId !== MARKETING_CONSENT_FIELD_ID) : [
|
|
439
|
+
...changed
|
|
440
|
+
];
|
|
441
|
+
}
|
|
442
|
+
/** What undo needs to take back an opt-in the import wrote. */ function consentUndoExtras(consent) {
|
|
443
|
+
return consent && 'undo' in consent && consent.undo ? {
|
|
444
|
+
extras: {
|
|
445
|
+
consent: consent.undo
|
|
446
|
+
}
|
|
447
|
+
} : {};
|
|
448
|
+
}
|
|
449
|
+
/** Two result sentences as one. */ function joinMessages(...parts) {
|
|
450
|
+
const text = parts.filter(Boolean).join(' ');
|
|
451
|
+
return text || undefined;
|
|
452
|
+
}
|
|
453
|
+
/** Writes a row's marketing choice through the import's site (AGL-4245). */ async function writeConsent(run, contactId, detail) {
|
|
454
|
+
return writeImportedConsent({
|
|
455
|
+
firestore: run.env.firestore,
|
|
456
|
+
contactRef: contactsOf(run.env).doc(contactId),
|
|
457
|
+
hostId: run.hostId,
|
|
458
|
+
group: run.group,
|
|
459
|
+
detail,
|
|
460
|
+
attestedNow: run.attested,
|
|
461
|
+
fallbackBy: run.actorUid,
|
|
462
|
+
atMs: Date.now()
|
|
463
|
+
});
|
|
464
|
+
}
|
|
418
465
|
/** The `crm.contacts` hooks. */ export function contactsTransferResource() {
|
|
419
466
|
return {
|
|
420
467
|
async fields (ctx) {
|
|
@@ -530,7 +577,7 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
530
577
|
},
|
|
531
578
|
{
|
|
532
579
|
fieldId: 'marketingConsent',
|
|
533
|
-
reason: '
|
|
580
|
+
reason: 'This column is never read as consent: a person gives it on a form, or a team member records how it was given on the contact’s page. A store’s customer file carries its marketing choices through Email marketing choice instead.',
|
|
534
581
|
refuseValues: true
|
|
535
582
|
},
|
|
536
583
|
{
|
|
@@ -550,14 +597,31 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
550
597
|
{
|
|
551
598
|
fieldId: 'name',
|
|
552
599
|
reason: 'The full name is built from the first and last names once either is set.'
|
|
553
|
-
}
|
|
600
|
+
},
|
|
601
|
+
// A store's customer file (AGL-4245): read under their own rules, never as a field value.
|
|
602
|
+
{
|
|
603
|
+
fieldId: CUSTOMER_FIELD.emailMarketing,
|
|
604
|
+
reason: 'An unsubscribe is always kept. An opt-in comes in only when you confirm on the Permission step, and never for somebody who unsubscribed here.'
|
|
605
|
+
},
|
|
606
|
+
...[
|
|
607
|
+
CUSTOMER_FIELD.smsMarketing,
|
|
608
|
+
CUSTOMER_FIELD.totalSpent,
|
|
609
|
+
CUSTOMER_FIELD.orderCount,
|
|
610
|
+
CUSTOMER_FIELD.customerId
|
|
611
|
+
].map((fieldId)=>({
|
|
612
|
+
fieldId,
|
|
613
|
+
reason: 'Read from your old store’s file and not kept. The dry run says so.'
|
|
614
|
+
}))
|
|
554
615
|
],
|
|
555
616
|
async plan (ctx, input) {
|
|
617
|
+
var _ctx_jobId;
|
|
556
618
|
const env = await crmTransferEnv(ctx);
|
|
557
619
|
requireCrmSuite(env, SUITE_ACT);
|
|
558
|
-
contactImportSite(env);
|
|
559
|
-
|
|
560
|
-
|
|
620
|
+
const site = contactImportSite(env);
|
|
621
|
+
// The store file's own columns are read below, never by the core (AGL-4245).
|
|
622
|
+
const plan = holdBackChanges(buildTransferPlan(_extends({}, input, {
|
|
623
|
+
rows: withoutCustomerSourceFields(input.rows)
|
|
624
|
+
})), (row, change)=>{
|
|
561
625
|
const before = row.recordId ? input.existing.get(row.recordId) : undefined;
|
|
562
626
|
if (change.fieldId === 'lifecycleStage' && before) {
|
|
563
627
|
const from = crmLifecycleStageRank(before['lifecycleStage']);
|
|
@@ -578,6 +642,18 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
578
642
|
}
|
|
579
643
|
return null;
|
|
580
644
|
});
|
|
645
|
+
const optIns = site.hostId ? consentOptInEmails(plan, input.rows, input.existing) : [];
|
|
646
|
+
const suppressed = optIns.length ? await readSuppressedAddresses(env.firestore, site.group.hostIds.length ? site.group.hostIds : [
|
|
647
|
+
site.hostId
|
|
648
|
+
], optIns) : new Set();
|
|
649
|
+
return planContactConsent(plan, input.rows, input.existing, {
|
|
650
|
+
hostId: site.hostId,
|
|
651
|
+
attested: readContactConsentAnswer(ctx.extras).attest,
|
|
652
|
+
recordedBy: ctx.actorUid,
|
|
653
|
+
jobId: (_ctx_jobId = ctx.jobId) != null ? _ctx_jobId : null,
|
|
654
|
+
source: customerFileSource(ctx.headers),
|
|
655
|
+
suppressed
|
|
656
|
+
}).plan;
|
|
581
657
|
},
|
|
582
658
|
async apply (ctx, chunk, writer) {
|
|
583
659
|
const env = await crmTransferEnv(ctx);
|
|
@@ -585,6 +661,7 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
585
661
|
const { hostId, group } = contactImportSite(env);
|
|
586
662
|
const run = {
|
|
587
663
|
env,
|
|
664
|
+
attested: readContactConsentAnswer(ctx.extras).attest,
|
|
588
665
|
hostId,
|
|
589
666
|
group,
|
|
590
667
|
actorUid: ctx.actorUid,
|
|
@@ -616,6 +693,9 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
616
693
|
let entry;
|
|
617
694
|
try {
|
|
618
695
|
const values = plannedValues(row);
|
|
696
|
+
// The store file's marketing choice is written on its own, checked again (AGL-4245).
|
|
697
|
+
const consentChange = importedConsentChange(row);
|
|
698
|
+
delete values[MARKETING_CONSENT_FIELD_ID];
|
|
619
699
|
if (row.verdict === 'update' && row.recordId) {
|
|
620
700
|
var _get;
|
|
621
701
|
const before = (await readContacts(env, [
|
|
@@ -623,10 +703,11 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
623
703
|
], run.definitions)).get(row.recordId);
|
|
624
704
|
if (!before) throw new TransferEngineError('notFound', 404, 'That contact no longer exists.');
|
|
625
705
|
const { skipped } = await writeContact(run, row.recordId, values);
|
|
706
|
+
const consent = consentChange ? await writeConsent(run, row.recordId, consentChange.consent) : null;
|
|
626
707
|
const after = (_get = (await readContacts(env, [
|
|
627
708
|
row.recordId
|
|
628
709
|
], run.definitions)).get(row.recordId)) != null ? _get : {};
|
|
629
|
-
const message = skippedMessage(skipped);
|
|
710
|
+
const message = joinMessages(skippedMessage(skipped), consentMessage(consent));
|
|
630
711
|
result = _extends({
|
|
631
712
|
row: row.index,
|
|
632
713
|
outcome: 'updated',
|
|
@@ -634,20 +715,22 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
634
715
|
}, message ? {
|
|
635
716
|
message
|
|
636
717
|
} : {});
|
|
637
|
-
|
|
718
|
+
const undoable = undoableFields(changed, consent);
|
|
719
|
+
entry = undoable.length ? _extends({
|
|
638
720
|
row: row.index,
|
|
639
721
|
recordId: row.recordId,
|
|
640
722
|
action: 'updated',
|
|
641
|
-
previous: pickValues(before,
|
|
642
|
-
written: pickValues(after,
|
|
643
|
-
};
|
|
723
|
+
previous: pickValues(before, undoable),
|
|
724
|
+
written: pickValues(after, undoable)
|
|
725
|
+
}, consentUndoExtras(consent)) : undefined;
|
|
644
726
|
} else {
|
|
645
727
|
var _get1;
|
|
646
728
|
const made = await createContact(run, values, batch);
|
|
729
|
+
const consent = consentChange ? await writeConsent(run, made.contactId, consentChange.consent) : null;
|
|
647
730
|
const after = (_get1 = (await readContacts(env, [
|
|
648
731
|
made.contactId
|
|
649
732
|
], run.definitions)).get(made.contactId)) != null ? _get1 : {};
|
|
650
|
-
const message = made.created ? skippedMessage(made.skipped) : 'Merged into the contact already holding this email.';
|
|
733
|
+
const message = made.created ? joinMessages(skippedMessage(made.skipped), consentMessage(consent)) : 'Merged into the contact already holding this email.';
|
|
651
734
|
result = _extends({
|
|
652
735
|
row: row.index,
|
|
653
736
|
outcome: made.created ? 'created' : 'updated',
|
|
@@ -657,12 +740,12 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
657
740
|
} : {});
|
|
658
741
|
// A merge into a person somebody else created meanwhile has no
|
|
659
742
|
// "before" to put back, so undo leaves it alone.
|
|
660
|
-
entry = made.created ? {
|
|
743
|
+
entry = made.created ? _extends({
|
|
661
744
|
row: row.index,
|
|
662
745
|
recordId: made.contactId,
|
|
663
746
|
action: 'created',
|
|
664
|
-
written: pickValues(after, changed)
|
|
665
|
-
} : undefined;
|
|
747
|
+
written: pickValues(after, undoableFields(changed, consent))
|
|
748
|
+
}, consentUndoExtras(consent)) : undefined;
|
|
666
749
|
}
|
|
667
750
|
} catch (error) {
|
|
668
751
|
result = _extends({
|
|
@@ -689,6 +772,7 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
689
772
|
const { hostId, group } = contactImportSite(env);
|
|
690
773
|
const run = {
|
|
691
774
|
env,
|
|
775
|
+
attested: false,
|
|
692
776
|
hostId,
|
|
693
777
|
group,
|
|
694
778
|
actorUid: ctx.actorUid,
|
|
@@ -716,11 +800,19 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
716
800
|
done.push(step);
|
|
717
801
|
continue;
|
|
718
802
|
}
|
|
803
|
+
const consentUndo = readImportedConsentUndo(entry.extras);
|
|
719
804
|
if (entry.action === 'created') {
|
|
720
805
|
// The site lets the person go, as "Remove from this site" does: a
|
|
721
806
|
// person only this import's site held is deleted, keeping every
|
|
722
807
|
// refusal they made; one another site holds too stays theirs.
|
|
723
808
|
const contactRef = contactsOf(env).doc(entry.recordId);
|
|
809
|
+
// An opt-in the file brought goes with them; a refusal it brought
|
|
810
|
+
// is kept in the retained-refusals store by the removal (AGL-4245).
|
|
811
|
+
if (consentUndo) await revertImportedConsent({
|
|
812
|
+
firestore: env.firestore,
|
|
813
|
+
contactRef,
|
|
814
|
+
undo: consentUndo
|
|
815
|
+
});
|
|
724
816
|
let holders = [];
|
|
725
817
|
const removal = await removeContactKeepingRefusals({
|
|
726
818
|
contactRef,
|
|
@@ -747,7 +839,17 @@ const contactsOf = (env)=>env.orgRef.collection('contacts');
|
|
|
747
839
|
});
|
|
748
840
|
continue;
|
|
749
841
|
}
|
|
750
|
-
const values = step.action === 'restore' || step.action === 'conflict' ? step.values : {};
|
|
842
|
+
const values = step.action === 'restore' || step.action === 'conflict' ? _extends({}, step.values) : {};
|
|
843
|
+
if (MARKETING_CONSENT_FIELD_ID in values) {
|
|
844
|
+
delete values[MARKETING_CONSENT_FIELD_ID];
|
|
845
|
+
if (consentUndo) {
|
|
846
|
+
await revertImportedConsent({
|
|
847
|
+
firestore: env.firestore,
|
|
848
|
+
contactRef: contactsOf(env).doc(entry.recordId),
|
|
849
|
+
undo: consentUndo
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
}
|
|
751
853
|
await writeContact(run, entry.recordId, values);
|
|
752
854
|
done.push(step);
|
|
753
855
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/transfer/contacts.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*==========================================\n * CONTACTS ON THE TRANSFER FRAMEWORK (AGL-3527) — the server half of\n * `crm.contacts`.\n *\n * A contact is one person shared by every site of the workspace, and each\n * site's consent group keeps its own profile of them — its facet. So a\n * transfer reads and writes the facet of the site it was opened under: an\n * export at the organization level reads each person's primary holder, as\n * the org-level list does; an import always names a site, because a\n * contact is a person some site met.\n *\n * ## The writes are the CRM's own\n *\n * A new person goes through `captureHostContact`, the door every capture\n * goes through — the records band, the erasure check, the consent group's\n * facet, `contactCreated` for the automations, the company by the email's\n * domain and the owner by the assignment rules when the row names neither.\n * A matched person is changed through the contact profile route's own\n * patch (`contactPatch`): the composed name, the company link and its\n * contact counts, the list's filter keys. A stage that moves raises\n * `contactStageChanged`, as the stage route does.\n *\n * ## What a file may not do\n *\n * The locked rules: an existing contact's email is who they are; consent is\n * never taken from a file; a stage moves forward only; Do not call is\n * turned on by a file and never off; the full name follows the first and\n * last names once either is set. The plan holds each one back by row and\n * names it, so the person acknowledges it before anything is written.\n *=========================================*/\n\nimport {\n composeContactName,\n type ConsentGroup,\n crmDefaultScopeOf,\n ORG_CONTACT_HOLDER_ID,\n contactDisplayName,\n contactFacetPath,\n type ContactFieldDefinition,\n CRM_COLLECTIONS,\n interactionsForGroup,\n isContactLifecycleStage,\n normalizeContactBirthdate,\n normalizePhone,\n planContactDetach,\n readContactFacet,\n readMarketingBasis,\n} from '@aglyn/aglyn/server'\nimport { CONTACT_LIFECYCLE_STAGE_LABELS } from '@aglyn/aglyn/app-utils/crm'\nimport { CONTACT_EXTRA_PHONE_FIELDS as PHONE_FIELDS } from '@aglyn/aglyn/app-utils/contacts'\nimport {\n buildTransferPlan,\n planTransferUndo,\n rankTransferLookupSuggestions,\n TRANSFER_ID_FIELD,\n transferLookupNewName,\n type PlannedTransferRow,\n type TransferRowResult,\n type TransferUndoEntry,\n type TransferUndoStep,\n} from '@aglyn/aglyn/data-transfer'\nimport type { TransferRecordsHooks } from '@aglyn/aglyn/plugin-manager/plugin-transfer-resources'\nimport {\n prepareContactCaptureBatch,\n removeContactKeepingRefusals,\n restampCrmListFieldsOf,\n} from '@aglyn/tenant-data-admin'\nimport { emitHostEvent } from '@aglyn/tenant-runtime'\nimport { FieldValue } from 'firebase-admin/firestore'\nimport { contactPrimaryGroup, ORG_HOLDER_GROUP } from '../model/contact-holder'\nimport type { ContactUpdateFields } from '../model/contact-update'\nimport { captureHostContact } from '../server/capture-host-contact'\nimport { CONTACT_NOTES_MAX } from '../server/contact-profile'\nimport {\n cachedContactReader,\n clearReportsToOf,\n CONTACT_REPORTS_TO_LOOP_REFUSAL,\n CONTACT_REPORTS_TO_SELF_REFUSAL,\n contactFacetHolders,\n contactReportsToLoops,\n} from '../server/contact-reports-to'\nimport { contactPatch, settleCompanyCounts } from '../server/contact-update'\nimport { sweepDealContactRoles } from '../server/deal-contact-roles'\nimport { CONTACT_ALIASES } from './aliases'\nimport {\n addCrmPicklistValues,\n addressFromValues,\n addressValues,\n countCrmExport,\n crmCustomDefinitions,\n crmMemberEmails,\n crmMembersTarget,\n crmPicklistLists,\n crmRecordsRoom,\n crmTransferEnv,\n type CrmTransferEnv,\n customTransferValues,\n customWriteValues,\n eachWithinBudget,\n failureMessage,\n holdBackChanges,\n IN_LIMIT,\n isoOf,\n lookupByField,\n lookupById,\n lookupResult,\n pickValues,\n plannedValues,\n readCrmExportPage,\n requireCrmSuite,\n requireSite,\n textOf,\n TransferEngineError,\n visibleIn,\n} from './common'\nimport { companyForName } from './companies'\nimport { addContactByHand } from '../server/add-contact-by-hand'\nimport {\n CONTACT_TRANSFER_DERIVED,\n CONTACT_TRANSFER_FIELDS,\n CONTACT_TRANSFER_GROUPS,\n CRM_MEMBERS_TARGET,\n CRM_TIMESTAMP_FIELDS,\n crmCustomTransferFields,\n crmLifecycleStageOf,\n crmLifecycleStageRank,\n} from './fields'\n\n/** The sentence an imported person's timeline opens with. */\nexport const CONTACT_TRANSFER_INTERACTION_SUMMARY = 'Imported from a file'\n\nconst SUITE_ACT = 'Importing contacts'\n\nconst contactsOf = (env: CrmTransferEnv) => env.orgRef.collection('contacts')\n\n/** The holder a contact is read through: the site's group, else the person's primary holder. */\nfunction holderOf(env: CrmTransferEnv, contact: Record<string, unknown>): ConsentGroup {\n return env.group ?? contactPrimaryGroup(contact, env.org)\n}\n\n/** Why a capture refused a person, in the result's words. */\nconst CAPTURE_REFUSALS: Readonly<Record<string, string>> = {\n band: 'The contact limit is reached.',\n 'invalid-email': 'Not a valid email address.',\n erased: 'Erased from this workspace at their request.',\n error: 'The contact could not be saved.',\n}\n\n/**\n * One contact as values by field id, read through one holder's facet. A\n * lookup and an undo read links by id (`ids`), so the plan compares what is\n * stored; an export writes names a file can carry back (`names`).\n */\nexport function contactTransferValues(\n id: string,\n contact: Record<string, unknown>,\n group: ConsentGroup,\n definitions: readonly ContactFieldDefinition[],\n names?: { owner: (uid: string) => string | undefined; manager: (id: string) => string | undefined },\n): Record<string, unknown> {\n const facet = readContactFacet(contact, group.groupId)\n const text = (value: unknown) => textOf(value) || null\n const stage = isContactLifecycleStage(facet.lifecycleStage) ? CONTACT_LIFECYCLE_STAGE_LABELS[facet.lifecycleStage] : null\n const basis = group.groupId ? readMarketingBasis(contact, group).basis : 'unrecorded'\n const ownerUid = text(facet.ownerUid)\n const managerId = text(facet.reportsToContactId)\n return {\n // The name first: a record's label is the first text it holds.\n name: text(contactDisplayName(contact, group.groupId)),\n email: text(contact['email']),\n salutation: text(facet.salutation),\n firstName: text(facet.firstName),\n lastName: text(facet.lastName),\n birthdate: text(facet.birthdate),\n phone: text(facet.phone),\n mobilePhone: text(facet.mobilePhone),\n homePhone: text(facet.homePhone),\n otherPhone: text(facet.otherPhone),\n fax: text(facet.fax),\n doNotCall: facet.doNotCall === true,\n assistantName: text(facet.assistantName),\n assistantPhone: text(facet.assistantPhone),\n jobTitle: text(facet.jobTitle),\n department: text(facet.department),\n company: names ? text(facet.companyName) : text(facet.companyId),\n reportsTo: managerId ? (names ? (names.manager(managerId) ?? managerId) : managerId) : null,\n ...addressValues('mailing', facet.address),\n ...addressValues('other', facet.otherAddress),\n owner: ownerUid ? (names ? (names.owner(ownerUid) ?? ownerUid) : ownerUid) : null,\n lifecycleStage: stage,\n leadSource: text(facet.leadSource),\n tags: Array.isArray(facet.tags) ? facet.tags : [],\n notes: text(facet.notes),\n marketingConsent: basis === 'granted' ? true : basis === 'declined' ? false : null,\n sources: Object.keys(facet.sources ?? {}),\n lastInteractionAt: isoOf(interactionsForGroup(facet.interactions, group.hostIds)[0]?.atMs),\n lastEngagedAt: isoOf(facet.lastEmailEngagementAtMs),\n createdAt: isoOf(contact['createdAt']),\n updatedAt: isoOf(contact['updatedAt']),\n ...customTransferValues(definitions, facet.custom),\n [TRANSFER_ID_FIELD]: id,\n }\n}\n\n/** The contacts these ids name, as values through the site's facet. */\nasync function readContacts(\n env: CrmTransferEnv,\n ids: readonly string[],\n definitions: readonly ContactFieldDefinition[],\n): Promise<Map<string, Record<string, unknown>>> {\n const out = new Map<string, Record<string, unknown>>()\n for (let at = 0; at < ids.length; at += 500) {\n const page = ids.slice(at, at + 500).filter((id) => id && !id.includes('/'))\n if (!page.length) continue\n for (const snapshot of await env.firestore.getAll(...page.map((id) => contactsOf(env).doc(id)))) {\n const data = snapshot.data()\n if (snapshot.exists && data && visibleIn(env, data['visibleTo'])) {\n out.set(snapshot.id, contactTransferValues(snapshot.id, data, holderOf(env, data), definitions))\n }\n }\n }\n return out\n}\n\n/** A company link a row names: the company and its name, or `null` to unlink. */\ninterface CompanyLink {\n id: string | null\n name: string\n}\n\n/** Everything one apply call shares across its rows. */\ninterface ApplyRun {\n env: CrmTransferEnv\n /** The Source site, or `null` for a file with none (AGL-4122). */\n hostId: string | null\n group: ConsentGroup\n actorUid: string | null\n definitions: ContactFieldDefinition[]\n companies: Map<string, Promise<{ id: string; name: string } | null>>\n admit: () => Promise<boolean>\n readContact: ReturnType<typeof cachedContactReader>\n}\n\n/** The company a row's company column names, resolved: an id the engine found, or one created by name. */\nasync function companyLinkFor(run: ApplyRun, value: unknown): Promise<CompanyLink> {\n if (value === null || value === undefined || value === '') return { id: null, name: '' }\n const create = transferLookupNewName(value)\n if (create) {\n const company = await companyForName(run.env, create, run.actorUid, run.companies, run.admit)\n if (!company) throw new TransferEngineError('invalid', 422, 'The CRM records limit has no room for the company.')\n return company\n }\n const snapshot = await run.env.orgRef.collection(CRM_COLLECTIONS.companies).doc(String(value)).get()\n if (!snapshot.exists || !visibleIn(run.env, snapshot.get('visibleTo'))) {\n throw new TransferEngineError('notFound', 404, 'The company this row names no longer exists.')\n }\n return { id: snapshot.id, name: textOf(snapshot.get('name')).slice(0, 120) }\n}\n\n/**\n * A row's values as the profile route's fields: each one normalized the way\n * that route reads it, a blank clearing it. Fields a value could not be\n * stored as are left out and named in `skipped`.\n */\nfunction profileFields(\n values: Readonly<Record<string, unknown>>,\n stored: ReturnType<typeof readContactFacet>,\n): { fields: ContactUpdateFields; skipped: string[] } {\n const fields: ContactUpdateFields = {}\n const skipped: string[] = []\n const text = (key: string, max = 120) => textOf(values[key]).slice(0, max)\n for (const key of ['jobTitle', 'department', 'assistantName', 'salutation', 'leadSource'] as const) {\n if (key in values) fields[key] = text(key)\n }\n if ('firstName' in values) fields.firstName = composeContactName(text('firstName'), '')\n if ('lastName' in values) fields.lastName = composeContactName('', text('lastName'))\n for (const key of ['phone', ...PHONE_FIELDS] as const) {\n if (!(key in values)) continue\n const typed = text(key, 40)\n const phone = typed ? normalizePhone(typed) : ''\n if (phone === null) skipped.push(key)\n else fields[key] = phone\n }\n if ('birthdate' in values) {\n const birthdate = normalizeContactBirthdate(text('birthdate', 20))\n if (birthdate === null) skipped.push('birthdate')\n else fields.birthdate = birthdate\n }\n if ('doNotCall' in values) fields.doNotCall = values['doNotCall'] === true\n if ('notes' in values) fields.notes = textOf(values['notes']).slice(0, CONTACT_NOTES_MAX)\n if ('tags' in values) fields.tags = Array.isArray(values['tags']) ? (values['tags'] as string[]).slice(0, 20) : []\n if ('owner' in values) fields.ownerUid = text('owner', 128)\n if ('reportsTo' in values) fields.reportsToContactId = text('reportsTo', 200) || null\n const mailing = addressFromValues('mailing', values, stored.address)\n if (mailing !== undefined) fields.address = mailing\n const other = addressFromValues('other', values, stored.otherAddress)\n if (other !== undefined) fields.otherAddress = other\n // The name follows the first and last names once either is set; only a\n // holder with neither keeps a name of its own.\n const composed = composeContactName(fields.firstName ?? stored.firstName, fields.lastName ?? stored.lastName)\n if ('name' in values && !composed) fields.name = text('name')\n return { fields, skipped }\n}\n\n/** The sentence a row's result carries for fields it could not store. */\nfunction skippedMessage(skipped: readonly string[]): string | undefined {\n if (!skipped.length) return undefined\n const labels = skipped.map((id) => CONTACT_TRANSFER_FIELDS.find((field) => field.id === id)?.label ?? id)\n return `Not written, as the value could not be read: ${labels.join(', ')}.`\n}\n\n/**\n * Writes one existing contact's values through the profile route's patch,\n * and the stage beside it. Answers the stage it replaced when it moved one,\n * and the fields left out.\n */\nasync function writeContact(\n run: ApplyRun,\n contactId: string,\n values: Readonly<Record<string, unknown>>,\n): Promise<{ skipped: string[] }> {\n const ref = contactsOf(run.env).doc(contactId)\n const snapshot = await ref.get()\n const contact = snapshot.data()\n if (!snapshot.exists || !contact || !visibleIn(run.env, contact['visibleTo'])) {\n throw new TransferEngineError('notFound', 404, 'That contact no longer exists.')\n }\n // With no site the row is written to the holder the person already has.\n const groupId =\n (run.hostId ? run.group : contactPrimaryGroup(contact, run.env.org)).groupId ||\n ORG_CONTACT_HOLDER_ID\n const stored = readContactFacet(contact, groupId)\n const { fields, skipped } = profileFields(values, stored)\n let linkedCompanyName = ''\n if ('company' in values) {\n const link = await companyLinkFor(run, values['company'])\n fields.companyId = link.id\n linkedCompanyName = link.name\n }\n if (fields.reportsToContactId) {\n const managerId = fields.reportsToContactId\n if (managerId === contactId) throw new TransferEngineError('invalid', 422, CONTACT_REPORTS_TO_SELF_REFUSAL)\n if (await contactReportsToLoops(run.readContact, contactId, managerId, groupId)) {\n throw new TransferEngineError('invalid', 422, CONTACT_REPORTS_TO_LOOP_REFUSAL)\n }\n }\n const custom = customWriteValues(values, run.definitions, 'contact')\n if ('error' in custom) throw new TransferEngineError('invalid', 422, custom.error)\n const patch = contactPatch(contact, groupId, fields, custom.values, linkedCompanyName)\n if ('refused' in patch) throw new TransferEngineError('invalid', 422, patch.refused)\n const update: Record<string, unknown> = { ...(patch.update ?? {}) }\n\n let movedFrom: string | null = null\n if ('lifecycleStage' in values) {\n const stage = crmLifecycleStageOf(values['lifecycleStage'])\n const previous = isContactLifecycleStage(stored.lifecycleStage) ? stored.lifecycleStage : ''\n if ((stage ?? '') !== previous) {\n update[contactFacetPath(groupId, 'lifecycleStage')] = stage ?? FieldValue.delete()\n if (stage) movedFrom = previous\n }\n }\n if (!Object.keys(update).length) return { skipped }\n update['updatedAt'] = FieldValue.serverTimestamp()\n await ref.update(update)\n await restampCrmListFieldsOf([ref], 'contacts')\n await settleCompanyCounts(run.env.orgRef.collection(CRM_COLLECTIONS.companies), [\n { contactId, ref, update, counts: patch.counts },\n ])\n if (movedFrom !== null && run.hostId) {\n await emitHostEvent(\n run.hostId,\n 'contactStageChanged',\n {\n contactId,\n email: textOf(contact['email']),\n lifecycleStage: String(crmLifecycleStageOf(values['lifecycleStage'])),\n previousStage: movedFrom,\n },\n run.actorUid ? { actor: { kind: 'member', uid: run.actorUid, email: null } } : {},\n ).catch((error: unknown) => console.error('[crm] transfer stage event failed', contactId, error))\n }\n return { skipped }\n}\n\n/**\n * The site a contact import is filed under. None, when the org's records belong\n * to every site (AGL-4122): the file then has no Source site, and the people in\n * it are the organization's. An org that keeps its sites apart must name one.\n */\nfunction contactImportSite(env: CrmTransferEnv): { hostId: string | null; group: ConsentGroup } {\n if (!env.hostId && crmDefaultScopeOf(env.org) === 'org') {\n return { hostId: null, group: ORG_HOLDER_GROUP }\n }\n return requireSite(env, 'contacts')\n}\n\n/** Creates one person through the capture door, then what the door does not take. */\nasync function createContact(\n run: ApplyRun,\n values: Readonly<Record<string, unknown>>,\n batch: Awaited<ReturnType<typeof prepareContactCaptureBatch>> | undefined,\n): Promise<{ contactId: string; created: boolean; skipped: string[] }> {\n const email = textOf(values['email'])\n const { fields, skipped } = profileFields(values, { sources: {}, interactions: [] })\n const link = 'company' in values ? await companyLinkFor(run, values['company']) : null\n const custom = customWriteValues(values, run.definitions, 'contact')\n if ('error' in custom) throw new TransferEngineError('invalid', 422, custom.error)\n const stage = crmLifecycleStageOf(values['lifecycleStage'])\n const facet = {\n ...(fields.phone ? { phone: fields.phone } : {}),\n ...(fields.jobTitle ? { jobTitle: fields.jobTitle } : {}),\n ...(fields.salutation ? { salutation: fields.salutation } : {}),\n ...(fields.firstName ? { firstName: fields.firstName } : {}),\n ...(fields.lastName ? { lastName: fields.lastName } : {}),\n ...(fields.department ? { department: fields.department } : {}),\n ...Object.fromEntries(PHONE_FIELDS.filter((key) => fields[key]).map((key) => [key, fields[key]])),\n ...(fields.birthdate ? { birthdate: fields.birthdate } : {}),\n ...(fields.assistantName ? { assistantName: fields.assistantName } : {}),\n ...(fields.leadSource ? { leadSource: fields.leadSource } : {}),\n ...(fields.doNotCall ? { doNotCall: true } : {}),\n ...(fields.address ? { address: fields.address } : {}),\n ...(fields.otherAddress ? { otherAddress: fields.otherAddress } : {}),\n // The link AND the name: the merge fields read the facet's name.\n ...(link?.id ? { companyId: link.id, companyName: link.name } : {}),\n ...(fields.ownerUid ? { ownerUid: fields.ownerUid } : {}),\n ...(Object.keys(custom.values).length ? { custom: custom.values } : {}),\n }\n const staged = stage ? { lifecycleStage: stage } : {}\n /*\n * A SITE AND AN ADDRESS ARE A CAPTURE. Anything less is a person no capture\n * door could take, so a file can hold people with no email and be imported\n * with no site (AGL-4122). A row still has to say who it is about.\n */\n if (!email && !fields.name && !facet.phone && !facet.firstName && !facet.lastName && !fields['mobilePhone']) {\n throw new TransferEngineError('invalid', 422, 'A row needs a name, an email or a phone number.')\n }\n const verdict =\n run.hostId && email\n ? await captureHostContact({\n hostId: run.hostId,\n email,\n ...(fields.name ? { name: fields.name } : {}),\n source: 'import',\n ...(run.actorUid\n ? { actor: { kind: 'member' as const, uid: run.actorUid, email: null } }\n : {}),\n interaction: { summary: CONTACT_TRANSFER_INTERACTION_SUMMARY },\n tags: fields.tags ?? [],\n // The file's own stage, or none: `lifecycleStage` from the column.\n facet: { ...facet, ...staged },\n // An import files nobody under a campaign; the record's picker does.\n campaignIds: [],\n ...(batch ? { batch } : {}),\n })\n : await addContactByHand({\n orgId: run.env.orgId,\n org: run.env.org,\n sourceHostId: run.hostId,\n email: email || null,\n ...(fields.name ? { name: fields.name } : {}),\n facet: { ...facet, ...staged },\n tags: fields.tags ?? [],\n })\n if ('refused' in verdict) {\n throw new TransferEngineError('invalid', 422, CAPTURE_REFUSALS[verdict.refused] ?? CAPTURE_REFUSALS['error'] as string)\n }\n // What the capture door does not write: who they report to, and the notes.\n const extra: Record<string, unknown> = {}\n if (fields.reportsToContactId) extra['reportsTo'] = fields.reportsToContactId\n if (fields.notes) extra['notes'] = fields.notes\n if (Object.keys(extra).length) await writeContact(run, verdict.contactId, extra)\n return { contactId: verdict.contactId, created: verdict.created, skipped }\n}\n\n/** The `crm.contacts` hooks. */\nexport function contactsTransferResource(): TransferRecordsHooks {\n return {\n async fields(ctx) {\n const env = await crmTransferEnv(ctx)\n return {\n standard: CONTACT_TRANSFER_FIELDS,\n derived: CONTACT_TRANSFER_DERIVED,\n system: CRM_TIMESTAMP_FIELDS,\n custom: crmCustomTransferFields(await crmCustomDefinitions(env, 'contact'), 'contact'),\n groups: CONTACT_TRANSFER_GROUPS,\n }\n },\n matchKeys: [\n { fieldId: TRANSFER_ID_FIELD, normalizer: 'aglynId' },\n { fieldId: 'email', normalizer: 'email' },\n ],\n aliases: CONTACT_ALIASES,\n lookupTargets: { [CRM_MEMBERS_TARGET]: crmMembersTarget() },\n\n // The people a workspace holds are its own on every plan (AGL-2839):\n // exporting them asks nothing of the plan.\n async count(ctx, options) {\n const env = await crmTransferEnv(ctx)\n return countCrmExport(env, contactsOf(env), options, (data) => visibleIn(env, data['visibleTo'], options.scopeTokens))\n },\n\n async readPage(ctx, cursor, fieldIds, options) {\n const env = await crmTransferEnv(ctx)\n const definitions = await crmCustomDefinitions(env, 'contact')\n const emails = fieldIds.includes('owner') ? await crmMemberEmails(env.orgId) : new Map<string, string>()\n return readCrmExportPage(\n env,\n contactsOf(env),\n cursor,\n options,\n (data) => visibleIn(env, data['visibleTo'], options?.scopeTokens),\n async (docs) => {\n const managers = new Map<string, string>()\n if (fieldIds.includes('reportsTo')) {\n const ids = [\n ...new Set(\n docs\n .map((doc) => textOf(readContactFacet(doc.data, holderOf(env, doc.data).groupId).reportsToContactId))\n .filter((id) => id && !id.includes('/')),\n ),\n ]\n for (let at = 0; at < ids.length; at += 500) {\n const page = ids.slice(at, at + 500)\n for (const snapshot of await env.firestore.getAll(...page.map((id) => contactsOf(env).doc(id)))) {\n if (snapshot.exists) managers.set(snapshot.id, textOf(snapshot.get('email')))\n }\n }\n }\n return docs.map((doc) =>\n pickValues(\n contactTransferValues(doc.id, doc.data, holderOf(env, doc.data), definitions, {\n owner: (uid) => emails.get(uid),\n manager: (id) => managers.get(id),\n }),\n fieldIds,\n ),\n )\n },\n )\n },\n\n async lookup(ctx, requests) {\n const env = await crmTransferEnv(ctx)\n const definitions = await crmCustomDefinitions(env, 'contact')\n const visible = (data: Record<string, unknown>) => visibleIn(env, data['visibleTo'])\n const found = { lookup: new Map<string, string[]>(), docs: new Map<string, Record<string, unknown>>() }\n for (const request of requests) {\n if (request.fieldId === TRANSFER_ID_FIELD) await lookupById(env, contactsOf(env), request, visible, found)\n else if (request.fieldId === 'email') {\n await lookupByField(contactsOf(env), request, 'email', (value) => value.trim().toLowerCase(), visible, found)\n }\n }\n return lookupResult(found, (id, data) => contactTransferValues(id, data, holderOf(env, data), definitions))\n },\n\n async suggest(ctx, request) {\n const env = await crmTransferEnv(ctx)\n const answer: Record<string, ReturnType<typeof rankTransferLookupSuggestions>> = {}\n for (const value of request.values) {\n // People sharing the value's first word: the token the list's own\n // search reads, so a mistyped domain still finds the person.\n const word = value.trim().toLowerCase().split(/[\\s@.+_-]+/)[0]?.slice(0, 12) ?? ''\n if (word.length < 2) {\n answer[value] = []\n continue\n }\n const snapshot = await contactsOf(env).where('searchTokens', 'array-contains', word).limit(IN_LIMIT).get()\n const candidates = snapshot.docs\n .filter((doc) => visibleIn(env, doc.get('visibleTo')))\n .flatMap((doc) => {\n const email = textOf(doc.get('email'))\n return email ? [{ recordId: doc.id, label: email }] : []\n })\n answer[value] = rankTransferLookupSuggestions(value, candidates)\n }\n return answer\n },\n\n async picklists(ctx, ids) {\n const env = await crmTransferEnv(ctx)\n return crmPicklistLists(env, ids, await crmCustomDefinitions(env, 'contact'))\n },\n\n async addPicklistValues(ctx, picklistId, values) {\n await addCrmPicklistValues(await crmTransferEnv(ctx), picklistId, values)\n },\n\n lockedRules: () => [\n {\n fieldId: 'email',\n reason: 'An existing contact’s email is who they are; change it on the contact’s page.',\n forced: { mode: 'keepExisting' },\n },\n {\n fieldId: 'marketingConsent',\n reason:\n 'Consent is never taken from a file: a person gives it on a form, or a team member records how it was given on the contact’s page.',\n refuseValues: true,\n },\n {\n fieldId: 'lifecycleStage',\n reason: 'A stage moves forward only: a later stage in the file advances the contact, an earlier one is held back.',\n forced: { mode: 'overwrite' },\n },\n {\n fieldId: 'doNotCall',\n reason: 'A file can say a person asked not to be called, never that they took it back.',\n forced: { mode: 'overwrite' },\n },\n {\n fieldId: 'name',\n reason: 'The full name is built from the first and last names once either is set.',\n },\n ],\n\n async plan(ctx, input) {\n const env = await crmTransferEnv(ctx)\n requireCrmSuite(env, SUITE_ACT)\n contactImportSite(env)\n const plan = buildTransferPlan(input)\n return holdBackChanges(plan, (row, change) => {\n const before = row.recordId ? input.existing.get(row.recordId) : undefined\n if (change.fieldId === 'lifecycleStage' && before) {\n const from = crmLifecycleStageRank(before['lifecycleStage'])\n const to = crmLifecycleStageRank(change.after)\n if (from >= 0 && (to < from || to < 0)) return 'A stage moves forward only.'\n }\n if (change.fieldId === 'doNotCall' && before?.['doNotCall'] === true && change.after !== true) {\n return 'Do not call is turned off on the contact’s page, never by a file.'\n }\n if (change.fieldId === 'name') {\n const values = Object.fromEntries(row.diff.map((entry) => [entry.fieldId, entry.after]))\n const first = 'firstName' in values ? values['firstName'] : before?.['firstName']\n const last = 'lastName' in values ? values['lastName'] : before?.['lastName']\n if (textOf(first) || textOf(last)) return 'Built from the first and last names.'\n }\n return null\n })\n },\n\n async apply(ctx, chunk, writer) {\n const env = await crmTransferEnv(ctx)\n requireCrmSuite(env, SUITE_ACT)\n const { hostId, group } = contactImportSite(env)\n const run: ApplyRun = {\n env,\n hostId,\n group,\n actorUid: ctx.actorUid,\n definitions: await crmCustomDefinitions(env, 'contact'),\n companies: new Map(),\n admit: crmRecordsRoom(env),\n readContact: cachedContactReader(contactsOf(env)),\n }\n const rows = chunk.rows as PlannedTransferRow[]\n const creating: string[] = []\n for (const row of rows) {\n if (row.verdict === 'create' && !(await writer.alreadyApplied(row.index))) {\n const email = textOf(plannedValues(row)['email'])\n if (email) creating.push(email)\n }\n }\n // The capture door's lookups for every new person at once (AGL-3423).\n const batch =\n hostId && creating.length ? await prepareContactCaptureBatch(hostId, creating) : undefined\n const results: TransferRowResult[] = []\n const undo: TransferUndoEntry[] = []\n\n await eachWithinBudget(rows, () => writer.timeLeftMs(), async (row) => {\n const earlier = await writer.alreadyApplied(row.index)\n if (earlier) {\n results.push(earlier)\n return\n }\n const changed = row.diff.map((change) => change.fieldId)\n let result: TransferRowResult\n let entry: TransferUndoEntry | undefined\n try {\n const values = plannedValues(row)\n if (row.verdict === 'update' && row.recordId) {\n const before = (await readContacts(env, [row.recordId], run.definitions)).get(row.recordId)\n if (!before) throw new TransferEngineError('notFound', 404, 'That contact no longer exists.')\n const { skipped } = await writeContact(run, row.recordId, values)\n const after = (await readContacts(env, [row.recordId], run.definitions)).get(row.recordId) ?? {}\n const message = skippedMessage(skipped)\n result = { row: row.index, outcome: 'updated', recordId: row.recordId, ...(message ? { message } : {}) }\n entry = {\n row: row.index,\n recordId: row.recordId,\n action: 'updated',\n previous: pickValues(before, changed),\n written: pickValues(after, changed),\n }\n } else {\n const made = await createContact(run, values, batch)\n const after = (await readContacts(env, [made.contactId], run.definitions)).get(made.contactId) ?? {}\n const message = made.created\n ? skippedMessage(made.skipped)\n : 'Merged into the contact already holding this email.'\n result = {\n row: row.index,\n outcome: made.created ? 'created' : 'updated',\n recordId: made.contactId,\n ...(message ? { message } : {}),\n }\n // A merge into a person somebody else created meanwhile has no\n // \"before\" to put back, so undo leaves it alone.\n entry = made.created\n ? { row: row.index, recordId: made.contactId, action: 'created', written: pickValues(after, changed) }\n : undefined\n }\n } catch (error) {\n result = {\n row: row.index,\n outcome: 'failed',\n ...(row.recordId ? { recordId: row.recordId } : {}),\n message: failureMessage(error),\n }\n entry = undefined\n }\n await writer.markApplied(result, entry)\n results.push(result)\n if (entry) undo.push(entry)\n })\n return { results, undo }\n },\n\n async revert(ctx, snapshot, decisions) {\n const env = await crmTransferEnv(ctx)\n const { hostId, group } = contactImportSite(env)\n const run: ApplyRun = {\n env,\n hostId,\n group,\n actorUid: ctx.actorUid,\n definitions: await crmCustomDefinitions(env, 'contact'),\n companies: new Map(),\n admit: crmRecordsRoom(env),\n readContact: cachedContactReader(contactsOf(env)),\n }\n const current = await readContacts(env, snapshot.entries.map((entry) => entry.recordId), run.definitions)\n const done: TransferUndoStep[] = []\n const conflicts: TransferUndoStep[] = []\n for (const entry of snapshot.entries) {\n const step = planTransferUndo(entry, current.get(entry.recordId) ?? null)\n if (step.action === 'conflict' && decisions?.[entry.recordId] !== 'revert') {\n if (decisions?.[entry.recordId] === 'keep') done.push({ action: 'nothing', recordId: entry.recordId, why: 'alreadyReverted' })\n else conflicts.push(step)\n continue\n }\n if (step.action === 'nothing') {\n done.push(step)\n continue\n }\n if (entry.action === 'created') {\n // The site lets the person go, as \"Remove from this site\" does: a\n // person only this import's site held is deleted, keeping every\n // refusal they made; one another site holds too stays theirs.\n const contactRef = contactsOf(env).doc(entry.recordId)\n let holders: string[] = []\n const removal = await removeContactKeepingRefusals({\n contactRef,\n decide: (contact) => {\n holders = contactFacetHolders(contact)\n return planContactDetach(contact, group)\n },\n nowMs: Date.now(),\n })\n if (removal.outcome === 'deleted') {\n await clearReportsToOf(env.firestore, contactsOf(env), holders, entry.recordId)\n await sweepDealContactRoles(\n env.firestore,\n env.orgRef.collection(CRM_COLLECTIONS.deals),\n entry.recordId,\n null,\n '[crm] transfer undo',\n )\n } else if (removal.outcome === 'detached') {\n await clearReportsToOf(env.firestore, contactsOf(env), [group.groupId], entry.recordId)\n await restampCrmListFieldsOf([contactRef], 'contacts').catch(() => undefined)\n }\n done.push({ action: 'delete', recordId: entry.recordId })\n continue\n }\n const values = step.action === 'restore' || step.action === 'conflict' ? step.values : {}\n await writeContact(run, entry.recordId, values)\n done.push(step)\n }\n return { done, conflicts }\n },\n }\n}\n"],"names":["composeContactName","crmDefaultScopeOf","ORG_CONTACT_HOLDER_ID","contactDisplayName","contactFacetPath","CRM_COLLECTIONS","interactionsForGroup","isContactLifecycleStage","normalizeContactBirthdate","normalizePhone","planContactDetach","readContactFacet","readMarketingBasis","CONTACT_LIFECYCLE_STAGE_LABELS","CONTACT_EXTRA_PHONE_FIELDS","PHONE_FIELDS","buildTransferPlan","planTransferUndo","rankTransferLookupSuggestions","TRANSFER_ID_FIELD","transferLookupNewName","prepareContactCaptureBatch","removeContactKeepingRefusals","restampCrmListFieldsOf","emitHostEvent","FieldValue","contactPrimaryGroup","ORG_HOLDER_GROUP","captureHostContact","CONTACT_NOTES_MAX","cachedContactReader","clearReportsToOf","CONTACT_REPORTS_TO_LOOP_REFUSAL","CONTACT_REPORTS_TO_SELF_REFUSAL","contactFacetHolders","contactReportsToLoops","contactPatch","settleCompanyCounts","sweepDealContactRoles","CONTACT_ALIASES","addCrmPicklistValues","addressFromValues","addressValues","countCrmExport","crmCustomDefinitions","crmMemberEmails","crmMembersTarget","crmPicklistLists","crmRecordsRoom","crmTransferEnv","customTransferValues","customWriteValues","eachWithinBudget","failureMessage","holdBackChanges","IN_LIMIT","isoOf","lookupByField","lookupById","lookupResult","pickValues","plannedValues","readCrmExportPage","requireCrmSuite","requireSite","textOf","TransferEngineError","visibleIn","companyForName","addContactByHand","CONTACT_TRANSFER_DERIVED","CONTACT_TRANSFER_FIELDS","CONTACT_TRANSFER_GROUPS","CRM_MEMBERS_TARGET","CRM_TIMESTAMP_FIELDS","crmCustomTransferFields","crmLifecycleStageOf","crmLifecycleStageRank","CONTACT_TRANSFER_INTERACTION_SUMMARY","SUITE_ACT","contactsOf","env","orgRef","collection","holderOf","contact","group","org","CAPTURE_REFUSALS","band","erased","error","contactTransferValues","id","definitions","names","facet","groupId","text","value","stage","lifecycleStage","basis","ownerUid","managerId","reportsToContactId","name","email","salutation","firstName","lastName","birthdate","phone","mobilePhone","homePhone","otherPhone","fax","doNotCall","assistantName","assistantPhone","jobTitle","department","company","companyName","companyId","reportsTo","manager","address","otherAddress","owner","leadSource","tags","Array","isArray","notes","marketingConsent","sources","Object","keys","lastInteractionAt","interactions","hostIds","atMs","lastEngagedAt","lastEmailEngagementAtMs","createdAt","updatedAt","custom","readContacts","ids","out","Map","at","length","page","slice","filter","includes","snapshot","firestore","getAll","map","doc","data","exists","set","companyLinkFor","run","undefined","create","actorUid","companies","admit","String","get","profileFields","values","stored","fields","skipped","key","max","typed","push","mailing","other","composed","skippedMessage","labels","find","field","label","join","writeContact","contactId","patch","ref","hostId","linkedCompanyName","link","readContact","refused","update","movedFrom","previous","delete","serverTimestamp","counts","previousStage","actor","kind","uid","catch","console","contactImportSite","createContact","batch","fromEntries","staged","verdict","source","interaction","summary","campaignIds","orgId","sourceHostId","extra","created","contactsTransferResource","ctx","standard","derived","system","groups","matchKeys","fieldId","normalizer","aliases","lookupTargets","count","options","scopeTokens","readPage","cursor","fieldIds","emails","docs","managers","Set","lookup","requests","visible","found","request","trim","toLowerCase","suggest","answer","word","split","where","limit","candidates","flatMap","recordId","picklists","addPicklistValues","picklistId","lockedRules","reason","forced","mode","refuseValues","plan","input","row","change","before","existing","from","to","after","diff","entry","first","last","apply","chunk","writer","rows","creating","alreadyApplied","index","results","undo","timeLeftMs","earlier","changed","result","message","outcome","action","written","made","markApplied","revert","decisions","current","entries","done","conflicts","step","why","contactRef","holders","removal","decide","nowMs","Date","now","deals"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2CA6B2C,GAE3C,SACEA,kBAAkB,EAElBC,iBAAiB,EACjBC,qBAAqB,EACrBC,kBAAkB,EAClBC,gBAAgB,EAEhBC,eAAe,EACfC,oBAAoB,EACpBC,uBAAuB,EACvBC,yBAAyB,EACzBC,cAAc,EACdC,iBAAiB,EACjBC,gBAAgB,EAChBC,kBAAkB,QACb,sBAAqB;AAC5B,SAASC,8BAA8B,QAAQ,6BAA4B;AAC3E,SAASC,8BAA8BC,YAAY,QAAQ,kCAAiC;AAC5F,SACEC,iBAAiB,EACjBC,gBAAgB,EAChBC,6BAA6B,EAC7BC,iBAAiB,EACjBC,qBAAqB,QAKhB,6BAA4B;AAEnC,SACEC,0BAA0B,EAC1BC,4BAA4B,EAC5BC,sBAAsB,QACjB,2BAA0B;AACjC,SAASC,aAAa,QAAQ,wBAAuB;AACrD,SAASC,UAAU,QAAQ,2BAA0B;AACrD,SAASC,mBAAmB,EAAEC,gBAAgB,QAAQ,6BAAyB;AAE/E,SAASC,kBAAkB,QAAQ,oCAAgC;AACnE,SAASC,iBAAiB,QAAQ,+BAA2B;AAC7D,SACEC,mBAAmB,EACnBC,gBAAgB,EAChBC,+BAA+B,EAC/BC,+BAA+B,E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AAEF;gBAAMC;YAAU;QAC3B;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/transfer/contacts.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*==========================================\n * CONTACTS ON THE TRANSFER FRAMEWORK (AGL-3527) — the server half of\n * `crm.contacts`.\n *\n * A contact is one person shared by every site of the workspace, and each\n * site's consent group keeps its own profile of them — its facet. So a\n * transfer reads and writes the facet of the site it was opened under: an\n * export at the organization level reads each person's primary holder, as\n * the org-level list does; an import always names a site, because a\n * contact is a person some site met.\n *\n * ## The writes are the CRM's own\n *\n * A new person goes through `captureHostContact`, the door every capture\n * goes through — the records band, the erasure check, the consent group's\n * facet, `contactCreated` for the automations, the company by the email's\n * domain and the owner by the assignment rules when the row names neither.\n * A matched person is changed through the contact profile route's own\n * patch (`contactPatch`): the composed name, the company link and its\n * contact counts, the list's filter keys. A stage that moves raises\n * `contactStageChanged`, as the stage route does.\n *\n * ## What a file may not do\n *\n * The locked rules: an existing contact's email is who they are; the\n * Marketing consent column is never read as consent; a stage moves forward only; Do not call is\n * turned on by a file and never off; the full name follows the first and\n * last names once either is set. The plan holds each one back by row and\n * names it, so the person acknowledges it before anything is written.\n *\n * ## A store's marketing choices (AGL-4245)\n *\n * A Shopify, Wix or Squarespace customer file records whether each person\n * agreed to marketing email. Its column is read into an import-only field and\n * carried by `customer-consent-plan.ts` (the plan) and\n * `imported-consent.server.ts` (the write and its undo): an opt-in only on\n * the Permission step's statement, an opt-out always, and never an opt-in\n * over somebody who unsubscribed here. Undo takes back the opt-ins and never\n * the opt-outs: those are the person's own choice, and outlive the import.\n *=========================================*/\n\nimport {\n composeContactName,\n type ConsentGroup,\n crmDefaultScopeOf,\n ORG_CONTACT_HOLDER_ID,\n contactDisplayName,\n contactFacetPath,\n type ContactFieldDefinition,\n CRM_COLLECTIONS,\n interactionsForGroup,\n isContactLifecycleStage,\n normalizeContactBirthdate,\n normalizePhone,\n planContactDetach,\n readContactFacet,\n readMarketingBasis,\n} from '@aglyn/aglyn/server'\nimport { CONTACT_LIFECYCLE_STAGE_LABELS } from '@aglyn/aglyn/app-utils/crm'\nimport { CONTACT_EXTRA_PHONE_FIELDS as PHONE_FIELDS } from '@aglyn/aglyn/app-utils/contacts'\nimport {\n buildTransferPlan,\n planTransferUndo,\n rankTransferLookupSuggestions,\n TRANSFER_ID_FIELD,\n transferLookupNewName,\n type PlannedTransferRow,\n type TransferRowResult,\n type TransferUndoEntry,\n type TransferUndoStep,\n} from '@aglyn/aglyn/data-transfer'\nimport type { TransferRecordsHooks } from '@aglyn/aglyn/plugin-manager/plugin-transfer-resources'\nimport {\n prepareContactCaptureBatch,\n removeContactKeepingRefusals,\n restampCrmListFieldsOf,\n} from '@aglyn/tenant-data-admin'\nimport { emitHostEvent } from '@aglyn/tenant-runtime'\nimport { FieldValue } from 'firebase-admin/firestore'\nimport { contactPrimaryGroup, ORG_HOLDER_GROUP } from '../model/contact-holder'\nimport type { ContactUpdateFields } from '../model/contact-update'\nimport { captureHostContact } from '../server/capture-host-contact'\nimport { CONTACT_NOTES_MAX } from '../server/contact-profile'\nimport {\n cachedContactReader,\n clearReportsToOf,\n CONTACT_REPORTS_TO_LOOP_REFUSAL,\n CONTACT_REPORTS_TO_SELF_REFUSAL,\n contactFacetHolders,\n contactReportsToLoops,\n} from '../server/contact-reports-to'\nimport { contactPatch, settleCompanyCounts } from '../server/contact-update'\nimport { sweepDealContactRoles } from '../server/deal-contact-roles'\nimport { CONTACT_ALIASES } from './aliases'\nimport {\n consentOptInEmails,\n importedConsentChange,\n planContactConsent,\n withoutCustomerSourceFields,\n} from './customer-consent-plan'\nimport { CUSTOMER_FIELD, customerFileSource, MARKETING_CONSENT_FIELD_ID, readContactConsentAnswer } from './customer-sources'\nimport {\n readImportedConsentUndo,\n readSuppressedAddresses,\n revertImportedConsent,\n writeImportedConsent,\n type ImportedConsentOutcome,\n} from './imported-consent.server'\nimport {\n addCrmPicklistValues,\n addressFromValues,\n addressValues,\n countCrmExport,\n crmCustomDefinitions,\n crmMemberEmails,\n crmMembersTarget,\n crmPicklistLists,\n crmRecordsRoom,\n crmTransferEnv,\n type CrmTransferEnv,\n customTransferValues,\n customWriteValues,\n eachWithinBudget,\n failureMessage,\n holdBackChanges,\n IN_LIMIT,\n isoOf,\n lookupByField,\n lookupById,\n lookupResult,\n pickValues,\n plannedValues,\n readCrmExportPage,\n requireCrmSuite,\n requireSite,\n textOf,\n TransferEngineError,\n visibleIn,\n} from './common'\nimport { companyForName } from './companies'\nimport { addContactByHand } from '../server/add-contact-by-hand'\nimport {\n CONTACT_TRANSFER_DERIVED,\n CONTACT_TRANSFER_FIELDS,\n CONTACT_TRANSFER_GROUPS,\n CRM_MEMBERS_TARGET,\n CRM_TIMESTAMP_FIELDS,\n crmCustomTransferFields,\n crmLifecycleStageOf,\n crmLifecycleStageRank,\n} from './fields'\n\n/** The sentence an imported person's timeline opens with. */\nexport const CONTACT_TRANSFER_INTERACTION_SUMMARY = 'Imported from a file'\n\nconst SUITE_ACT = 'Importing contacts'\n\nconst contactsOf = (env: CrmTransferEnv) => env.orgRef.collection('contacts')\n\n/** The holder a contact is read through: the site's group, else the person's primary holder. */\nfunction holderOf(env: CrmTransferEnv, contact: Record<string, unknown>): ConsentGroup {\n return env.group ?? contactPrimaryGroup(contact, env.org)\n}\n\n/** Why a capture refused a person, in the result's words. */\nconst CAPTURE_REFUSALS: Readonly<Record<string, string>> = {\n band: 'The contact limit is reached.',\n 'invalid-email': 'Not a valid email address.',\n erased: 'Erased from this workspace at their request.',\n error: 'The contact could not be saved.',\n}\n\n/**\n * One contact as values by field id, read through one holder's facet. A\n * lookup and an undo read links by id (`ids`), so the plan compares what is\n * stored; an export writes names a file can carry back (`names`).\n */\nexport function contactTransferValues(\n id: string,\n contact: Record<string, unknown>,\n group: ConsentGroup,\n definitions: readonly ContactFieldDefinition[],\n names?: { owner: (uid: string) => string | undefined; manager: (id: string) => string | undefined },\n): Record<string, unknown> {\n const facet = readContactFacet(contact, group.groupId)\n const text = (value: unknown) => textOf(value) || null\n const stage = isContactLifecycleStage(facet.lifecycleStage) ? CONTACT_LIFECYCLE_STAGE_LABELS[facet.lifecycleStage] : null\n const basis = group.groupId ? readMarketingBasis(contact, group).basis : 'unrecorded'\n const ownerUid = text(facet.ownerUid)\n const managerId = text(facet.reportsToContactId)\n return {\n // The name first: a record's label is the first text it holds.\n name: text(contactDisplayName(contact, group.groupId)),\n email: text(contact['email']),\n salutation: text(facet.salutation),\n firstName: text(facet.firstName),\n lastName: text(facet.lastName),\n birthdate: text(facet.birthdate),\n phone: text(facet.phone),\n mobilePhone: text(facet.mobilePhone),\n homePhone: text(facet.homePhone),\n otherPhone: text(facet.otherPhone),\n fax: text(facet.fax),\n doNotCall: facet.doNotCall === true,\n assistantName: text(facet.assistantName),\n assistantPhone: text(facet.assistantPhone),\n jobTitle: text(facet.jobTitle),\n department: text(facet.department),\n company: names ? text(facet.companyName) : text(facet.companyId),\n reportsTo: managerId ? (names ? (names.manager(managerId) ?? managerId) : managerId) : null,\n ...addressValues('mailing', facet.address),\n ...addressValues('other', facet.otherAddress),\n owner: ownerUid ? (names ? (names.owner(ownerUid) ?? ownerUid) : ownerUid) : null,\n lifecycleStage: stage,\n leadSource: text(facet.leadSource),\n tags: Array.isArray(facet.tags) ? facet.tags : [],\n notes: text(facet.notes),\n marketingConsent: basis === 'granted' ? true : basis === 'declined' ? false : null,\n sources: Object.keys(facet.sources ?? {}),\n lastInteractionAt: isoOf(interactionsForGroup(facet.interactions, group.hostIds)[0]?.atMs),\n lastEngagedAt: isoOf(facet.lastEmailEngagementAtMs),\n createdAt: isoOf(contact['createdAt']),\n updatedAt: isoOf(contact['updatedAt']),\n ...customTransferValues(definitions, facet.custom),\n [TRANSFER_ID_FIELD]: id,\n }\n}\n\n/** The contacts these ids name, as values through the site's facet. */\nasync function readContacts(\n env: CrmTransferEnv,\n ids: readonly string[],\n definitions: readonly ContactFieldDefinition[],\n): Promise<Map<string, Record<string, unknown>>> {\n const out = new Map<string, Record<string, unknown>>()\n for (let at = 0; at < ids.length; at += 500) {\n const page = ids.slice(at, at + 500).filter((id) => id && !id.includes('/'))\n if (!page.length) continue\n for (const snapshot of await env.firestore.getAll(...page.map((id) => contactsOf(env).doc(id)))) {\n const data = snapshot.data()\n if (snapshot.exists && data && visibleIn(env, data['visibleTo'])) {\n out.set(snapshot.id, contactTransferValues(snapshot.id, data, holderOf(env, data), definitions))\n }\n }\n }\n return out\n}\n\n/** A company link a row names: the company and its name, or `null` to unlink. */\ninterface CompanyLink {\n id: string | null\n name: string\n}\n\n/** Everything one apply call shares across its rows. */\ninterface ApplyRun {\n env: CrmTransferEnv\n /** The job's Permission answer, as the plan was made with it. */\n attested: boolean\n /** The Source site, or `null` for a file with none (AGL-4122). */\n hostId: string | null\n group: ConsentGroup\n actorUid: string | null\n definitions: ContactFieldDefinition[]\n companies: Map<string, Promise<{ id: string; name: string } | null>>\n admit: () => Promise<boolean>\n readContact: ReturnType<typeof cachedContactReader>\n}\n\n/** The company a row's company column names, resolved: an id the engine found, or one created by name. */\nasync function companyLinkFor(run: ApplyRun, value: unknown): Promise<CompanyLink> {\n if (value === null || value === undefined || value === '') return { id: null, name: '' }\n const create = transferLookupNewName(value)\n if (create) {\n const company = await companyForName(run.env, create, run.actorUid, run.companies, run.admit)\n if (!company) throw new TransferEngineError('invalid', 422, 'The CRM records limit has no room for the company.')\n return company\n }\n const snapshot = await run.env.orgRef.collection(CRM_COLLECTIONS.companies).doc(String(value)).get()\n if (!snapshot.exists || !visibleIn(run.env, snapshot.get('visibleTo'))) {\n throw new TransferEngineError('notFound', 404, 'The company this row names no longer exists.')\n }\n return { id: snapshot.id, name: textOf(snapshot.get('name')).slice(0, 120) }\n}\n\n/**\n * A row's values as the profile route's fields: each one normalized the way\n * that route reads it, a blank clearing it. Fields a value could not be\n * stored as are left out and named in `skipped`.\n */\nfunction profileFields(\n values: Readonly<Record<string, unknown>>,\n stored: ReturnType<typeof readContactFacet>,\n): { fields: ContactUpdateFields; skipped: string[] } {\n const fields: ContactUpdateFields = {}\n const skipped: string[] = []\n const text = (key: string, max = 120) => textOf(values[key]).slice(0, max)\n for (const key of ['jobTitle', 'department', 'assistantName', 'salutation', 'leadSource'] as const) {\n if (key in values) fields[key] = text(key)\n }\n if ('firstName' in values) fields.firstName = composeContactName(text('firstName'), '')\n if ('lastName' in values) fields.lastName = composeContactName('', text('lastName'))\n for (const key of ['phone', ...PHONE_FIELDS] as const) {\n if (!(key in values)) continue\n const typed = text(key, 40)\n const phone = typed ? normalizePhone(typed) : ''\n if (phone === null) skipped.push(key)\n else fields[key] = phone\n }\n if ('birthdate' in values) {\n const birthdate = normalizeContactBirthdate(text('birthdate', 20))\n if (birthdate === null) skipped.push('birthdate')\n else fields.birthdate = birthdate\n }\n if ('doNotCall' in values) fields.doNotCall = values['doNotCall'] === true\n if ('notes' in values) fields.notes = textOf(values['notes']).slice(0, CONTACT_NOTES_MAX)\n if ('tags' in values) fields.tags = Array.isArray(values['tags']) ? (values['tags'] as string[]).slice(0, 20) : []\n if ('owner' in values) fields.ownerUid = text('owner', 128)\n if ('reportsTo' in values) fields.reportsToContactId = text('reportsTo', 200) || null\n const mailing = addressFromValues('mailing', values, stored.address)\n if (mailing !== undefined) fields.address = mailing\n const other = addressFromValues('other', values, stored.otherAddress)\n if (other !== undefined) fields.otherAddress = other\n // The name follows the first and last names once either is set; only a\n // holder with neither keeps a name of its own.\n const composed = composeContactName(fields.firstName ?? stored.firstName, fields.lastName ?? stored.lastName)\n if ('name' in values && !composed) fields.name = text('name')\n return { fields, skipped }\n}\n\n/** The sentence a row's result carries for fields it could not store. */\nfunction skippedMessage(skipped: readonly string[]): string | undefined {\n if (!skipped.length) return undefined\n const labels = skipped.map((id) => CONTACT_TRANSFER_FIELDS.find((field) => field.id === id)?.label ?? id)\n return `Not written, as the value could not be read: ${labels.join(', ')}.`\n}\n\n/**\n * Writes one existing contact's values through the profile route's patch,\n * and the stage beside it. Answers the stage it replaced when it moved one,\n * and the fields left out.\n */\nasync function writeContact(\n run: ApplyRun,\n contactId: string,\n values: Readonly<Record<string, unknown>>,\n): Promise<{ skipped: string[] }> {\n const ref = contactsOf(run.env).doc(contactId)\n const snapshot = await ref.get()\n const contact = snapshot.data()\n if (!snapshot.exists || !contact || !visibleIn(run.env, contact['visibleTo'])) {\n throw new TransferEngineError('notFound', 404, 'That contact no longer exists.')\n }\n // With no site the row is written to the holder the person already has.\n const groupId =\n (run.hostId ? run.group : contactPrimaryGroup(contact, run.env.org)).groupId ||\n ORG_CONTACT_HOLDER_ID\n const stored = readContactFacet(contact, groupId)\n const { fields, skipped } = profileFields(values, stored)\n let linkedCompanyName = ''\n if ('company' in values) {\n const link = await companyLinkFor(run, values['company'])\n fields.companyId = link.id\n linkedCompanyName = link.name\n }\n if (fields.reportsToContactId) {\n const managerId = fields.reportsToContactId\n if (managerId === contactId) throw new TransferEngineError('invalid', 422, CONTACT_REPORTS_TO_SELF_REFUSAL)\n if (await contactReportsToLoops(run.readContact, contactId, managerId, groupId)) {\n throw new TransferEngineError('invalid', 422, CONTACT_REPORTS_TO_LOOP_REFUSAL)\n }\n }\n const custom = customWriteValues(values, run.definitions, 'contact')\n if ('error' in custom) throw new TransferEngineError('invalid', 422, custom.error)\n const patch = contactPatch(contact, groupId, fields, custom.values, linkedCompanyName)\n if ('refused' in patch) throw new TransferEngineError('invalid', 422, patch.refused)\n const update: Record<string, unknown> = { ...(patch.update ?? {}) }\n\n let movedFrom: string | null = null\n if ('lifecycleStage' in values) {\n const stage = crmLifecycleStageOf(values['lifecycleStage'])\n const previous = isContactLifecycleStage(stored.lifecycleStage) ? stored.lifecycleStage : ''\n if ((stage ?? '') !== previous) {\n update[contactFacetPath(groupId, 'lifecycleStage')] = stage ?? FieldValue.delete()\n if (stage) movedFrom = previous\n }\n }\n if (!Object.keys(update).length) return { skipped }\n update['updatedAt'] = FieldValue.serverTimestamp()\n await ref.update(update)\n await restampCrmListFieldsOf([ref], 'contacts')\n await settleCompanyCounts(run.env.orgRef.collection(CRM_COLLECTIONS.companies), [\n { contactId, ref, update, counts: patch.counts },\n ])\n if (movedFrom !== null && run.hostId) {\n await emitHostEvent(\n run.hostId,\n 'contactStageChanged',\n {\n contactId,\n email: textOf(contact['email']),\n lifecycleStage: String(crmLifecycleStageOf(values['lifecycleStage'])),\n previousStage: movedFrom,\n },\n run.actorUid ? { actor: { kind: 'member', uid: run.actorUid, email: null } } : {},\n ).catch((error: unknown) => console.error('[crm] transfer stage event failed', contactId, error))\n }\n return { skipped }\n}\n\n/**\n * The site a contact import is filed under. None, when the org's records belong\n * to every site (AGL-4122): the file then has no Source site, and the people in\n * it are the organization's. An org that keeps its sites apart must name one.\n */\nfunction contactImportSite(env: CrmTransferEnv): { hostId: string | null; group: ConsentGroup } {\n if (!env.hostId && crmDefaultScopeOf(env.org) === 'org') {\n return { hostId: null, group: ORG_HOLDER_GROUP }\n }\n return requireSite(env, 'contacts')\n}\n\n/** Creates one person through the capture door, then what the door does not take. */\nasync function createContact(\n run: ApplyRun,\n values: Readonly<Record<string, unknown>>,\n batch: Awaited<ReturnType<typeof prepareContactCaptureBatch>> | undefined,\n): Promise<{ contactId: string; created: boolean; skipped: string[] }> {\n const email = textOf(values['email'])\n const { fields, skipped } = profileFields(values, { sources: {}, interactions: [] })\n const link = 'company' in values ? await companyLinkFor(run, values['company']) : null\n const custom = customWriteValues(values, run.definitions, 'contact')\n if ('error' in custom) throw new TransferEngineError('invalid', 422, custom.error)\n const stage = crmLifecycleStageOf(values['lifecycleStage'])\n const facet = {\n ...(fields.phone ? { phone: fields.phone } : {}),\n ...(fields.jobTitle ? { jobTitle: fields.jobTitle } : {}),\n ...(fields.salutation ? { salutation: fields.salutation } : {}),\n ...(fields.firstName ? { firstName: fields.firstName } : {}),\n ...(fields.lastName ? { lastName: fields.lastName } : {}),\n ...(fields.department ? { department: fields.department } : {}),\n ...Object.fromEntries(PHONE_FIELDS.filter((key) => fields[key]).map((key) => [key, fields[key]])),\n ...(fields.birthdate ? { birthdate: fields.birthdate } : {}),\n ...(fields.assistantName ? { assistantName: fields.assistantName } : {}),\n ...(fields.leadSource ? { leadSource: fields.leadSource } : {}),\n ...(fields.doNotCall ? { doNotCall: true } : {}),\n ...(fields.address ? { address: fields.address } : {}),\n ...(fields.otherAddress ? { otherAddress: fields.otherAddress } : {}),\n // The link AND the name: the merge fields read the facet's name.\n ...(link?.id ? { companyId: link.id, companyName: link.name } : {}),\n ...(fields.ownerUid ? { ownerUid: fields.ownerUid } : {}),\n ...(Object.keys(custom.values).length ? { custom: custom.values } : {}),\n }\n const staged = stage ? { lifecycleStage: stage } : {}\n /*\n * A SITE AND AN ADDRESS ARE A CAPTURE. Anything less is a person no capture\n * door could take, so a file can hold people with no email and be imported\n * with no site (AGL-4122). A row still has to say who it is about.\n */\n if (!email && !fields.name && !facet.phone && !facet.firstName && !facet.lastName && !fields['mobilePhone']) {\n throw new TransferEngineError('invalid', 422, 'A row needs a name, an email or a phone number.')\n }\n const verdict =\n run.hostId && email\n ? await captureHostContact({\n hostId: run.hostId,\n email,\n ...(fields.name ? { name: fields.name } : {}),\n source: 'import',\n ...(run.actorUid\n ? { actor: { kind: 'member' as const, uid: run.actorUid, email: null } }\n : {}),\n interaction: { summary: CONTACT_TRANSFER_INTERACTION_SUMMARY },\n tags: fields.tags ?? [],\n // The file's own stage, or none: `lifecycleStage` from the column.\n facet: { ...facet, ...staged },\n // An import files nobody under a campaign; the record's picker does.\n campaignIds: [],\n ...(batch ? { batch } : {}),\n })\n : await addContactByHand({\n orgId: run.env.orgId,\n org: run.env.org,\n sourceHostId: run.hostId,\n email: email || null,\n ...(fields.name ? { name: fields.name } : {}),\n facet: { ...facet, ...staged },\n tags: fields.tags ?? [],\n })\n if ('refused' in verdict) {\n throw new TransferEngineError('invalid', 422, CAPTURE_REFUSALS[verdict.refused] ?? CAPTURE_REFUSALS['error'] as string)\n }\n // What the capture door does not write: who they report to, and the notes.\n const extra: Record<string, unknown> = {}\n if (fields.reportsToContactId) extra['reportsTo'] = fields.reportsToContactId\n if (fields.notes) extra['notes'] = fields.notes\n if (Object.keys(extra).length) await writeContact(run, verdict.contactId, extra)\n return { contactId: verdict.contactId, created: verdict.created, skipped }\n}\n\n/** Why a row's marketing choice was not written, when the write says. */\nfunction consentMessage(outcome: ImportedConsentOutcome | null): string | undefined {\n return outcome && 'message' in outcome ? outcome.message : undefined\n}\n\n/**\n * The fields undo may take back: every changed one, but a refusal the file\n * carried only stays. An unsubscribe is the person's own choice (AGL-4245).\n */\nfunction undoableFields(changed: readonly string[], consent: ImportedConsentOutcome | null): string[] {\n return consent && 'value' in consent && consent.value === false ? changed.filter((fieldId) => fieldId !== MARKETING_CONSENT_FIELD_ID) : [...changed]\n}\n\n/** What undo needs to take back an opt-in the import wrote. */\nfunction consentUndoExtras(consent: ImportedConsentOutcome | null): { extras?: Record<string, unknown> } {\n return consent && 'undo' in consent && consent.undo ? { extras: { consent: consent.undo } } : {}\n}\n\n/** Two result sentences as one. */\nfunction joinMessages(...parts: Array<string | undefined>): string | undefined {\n const text = parts.filter(Boolean).join(' ')\n return text || undefined\n}\n\n/** Writes a row's marketing choice through the import's site (AGL-4245). */\nasync function writeConsent(\n run: ApplyRun,\n contactId: string,\n detail: Parameters<typeof writeImportedConsent>[0]['detail'],\n): Promise<ImportedConsentOutcome> {\n return writeImportedConsent({\n firestore: run.env.firestore,\n contactRef: contactsOf(run.env).doc(contactId),\n hostId: run.hostId,\n group: run.group,\n detail,\n attestedNow: run.attested,\n fallbackBy: run.actorUid,\n atMs: Date.now(),\n })\n}\n\n/** The `crm.contacts` hooks. */\nexport function contactsTransferResource(): TransferRecordsHooks {\n return {\n async fields(ctx) {\n const env = await crmTransferEnv(ctx)\n return {\n standard: CONTACT_TRANSFER_FIELDS,\n derived: CONTACT_TRANSFER_DERIVED,\n system: CRM_TIMESTAMP_FIELDS,\n custom: crmCustomTransferFields(await crmCustomDefinitions(env, 'contact'), 'contact'),\n groups: CONTACT_TRANSFER_GROUPS,\n }\n },\n matchKeys: [\n { fieldId: TRANSFER_ID_FIELD, normalizer: 'aglynId' },\n { fieldId: 'email', normalizer: 'email' },\n ],\n aliases: CONTACT_ALIASES,\n lookupTargets: { [CRM_MEMBERS_TARGET]: crmMembersTarget() },\n\n // The people a workspace holds are its own on every plan (AGL-2839):\n // exporting them asks nothing of the plan.\n async count(ctx, options) {\n const env = await crmTransferEnv(ctx)\n return countCrmExport(env, contactsOf(env), options, (data) => visibleIn(env, data['visibleTo'], options.scopeTokens))\n },\n\n async readPage(ctx, cursor, fieldIds, options) {\n const env = await crmTransferEnv(ctx)\n const definitions = await crmCustomDefinitions(env, 'contact')\n const emails = fieldIds.includes('owner') ? await crmMemberEmails(env.orgId) : new Map<string, string>()\n return readCrmExportPage(\n env,\n contactsOf(env),\n cursor,\n options,\n (data) => visibleIn(env, data['visibleTo'], options?.scopeTokens),\n async (docs) => {\n const managers = new Map<string, string>()\n if (fieldIds.includes('reportsTo')) {\n const ids = [\n ...new Set(\n docs\n .map((doc) => textOf(readContactFacet(doc.data, holderOf(env, doc.data).groupId).reportsToContactId))\n .filter((id) => id && !id.includes('/')),\n ),\n ]\n for (let at = 0; at < ids.length; at += 500) {\n const page = ids.slice(at, at + 500)\n for (const snapshot of await env.firestore.getAll(...page.map((id) => contactsOf(env).doc(id)))) {\n if (snapshot.exists) managers.set(snapshot.id, textOf(snapshot.get('email')))\n }\n }\n }\n return docs.map((doc) =>\n pickValues(\n contactTransferValues(doc.id, doc.data, holderOf(env, doc.data), definitions, {\n owner: (uid) => emails.get(uid),\n manager: (id) => managers.get(id),\n }),\n fieldIds,\n ),\n )\n },\n )\n },\n\n async lookup(ctx, requests) {\n const env = await crmTransferEnv(ctx)\n const definitions = await crmCustomDefinitions(env, 'contact')\n const visible = (data: Record<string, unknown>) => visibleIn(env, data['visibleTo'])\n const found = { lookup: new Map<string, string[]>(), docs: new Map<string, Record<string, unknown>>() }\n for (const request of requests) {\n if (request.fieldId === TRANSFER_ID_FIELD) await lookupById(env, contactsOf(env), request, visible, found)\n else if (request.fieldId === 'email') {\n await lookupByField(contactsOf(env), request, 'email', (value) => value.trim().toLowerCase(), visible, found)\n }\n }\n return lookupResult(found, (id, data) => contactTransferValues(id, data, holderOf(env, data), definitions))\n },\n\n async suggest(ctx, request) {\n const env = await crmTransferEnv(ctx)\n const answer: Record<string, ReturnType<typeof rankTransferLookupSuggestions>> = {}\n for (const value of request.values) {\n // People sharing the value's first word: the token the list's own\n // search reads, so a mistyped domain still finds the person.\n const word = value.trim().toLowerCase().split(/[\\s@.+_-]+/)[0]?.slice(0, 12) ?? ''\n if (word.length < 2) {\n answer[value] = []\n continue\n }\n const snapshot = await contactsOf(env).where('searchTokens', 'array-contains', word).limit(IN_LIMIT).get()\n const candidates = snapshot.docs\n .filter((doc) => visibleIn(env, doc.get('visibleTo')))\n .flatMap((doc) => {\n const email = textOf(doc.get('email'))\n return email ? [{ recordId: doc.id, label: email }] : []\n })\n answer[value] = rankTransferLookupSuggestions(value, candidates)\n }\n return answer\n },\n\n async picklists(ctx, ids) {\n const env = await crmTransferEnv(ctx)\n return crmPicklistLists(env, ids, await crmCustomDefinitions(env, 'contact'))\n },\n\n async addPicklistValues(ctx, picklistId, values) {\n await addCrmPicklistValues(await crmTransferEnv(ctx), picklistId, values)\n },\n\n lockedRules: () => [\n {\n fieldId: 'email',\n reason: 'An existing contact’s email is who they are; change it on the contact’s page.',\n forced: { mode: 'keepExisting' },\n },\n {\n fieldId: 'marketingConsent',\n reason:\n 'This column is never read as consent: a person gives it on a form, or a team member records how it was given on the contact’s page. A store’s customer file carries its marketing choices through Email marketing choice instead.',\n refuseValues: true,\n },\n {\n fieldId: 'lifecycleStage',\n reason: 'A stage moves forward only: a later stage in the file advances the contact, an earlier one is held back.',\n forced: { mode: 'overwrite' },\n },\n {\n fieldId: 'doNotCall',\n reason: 'A file can say a person asked not to be called, never that they took it back.',\n forced: { mode: 'overwrite' },\n },\n {\n fieldId: 'name',\n reason: 'The full name is built from the first and last names once either is set.',\n },\n // A store's customer file (AGL-4245): read under their own rules, never as a field value.\n {\n fieldId: CUSTOMER_FIELD.emailMarketing,\n reason:\n 'An unsubscribe is always kept. An opt-in comes in only when you confirm on the Permission step, and never for somebody who unsubscribed here.',\n },\n ...[CUSTOMER_FIELD.smsMarketing, CUSTOMER_FIELD.totalSpent, CUSTOMER_FIELD.orderCount, CUSTOMER_FIELD.customerId].map(\n (fieldId) => ({ fieldId, reason: 'Read from your old store’s file and not kept. The dry run says so.' }),\n ),\n ],\n\n async plan(ctx, input) {\n const env = await crmTransferEnv(ctx)\n requireCrmSuite(env, SUITE_ACT)\n const site = contactImportSite(env)\n // The store file's own columns are read below, never by the core (AGL-4245).\n const plan = holdBackChanges(buildTransferPlan({ ...input, rows: withoutCustomerSourceFields(input.rows) }), (row, change) => {\n const before = row.recordId ? input.existing.get(row.recordId) : undefined\n if (change.fieldId === 'lifecycleStage' && before) {\n const from = crmLifecycleStageRank(before['lifecycleStage'])\n const to = crmLifecycleStageRank(change.after)\n if (from >= 0 && (to < from || to < 0)) return 'A stage moves forward only.'\n }\n if (change.fieldId === 'doNotCall' && before?.['doNotCall'] === true && change.after !== true) {\n return 'Do not call is turned off on the contact’s page, never by a file.'\n }\n if (change.fieldId === 'name') {\n const values = Object.fromEntries(row.diff.map((entry) => [entry.fieldId, entry.after]))\n const first = 'firstName' in values ? values['firstName'] : before?.['firstName']\n const last = 'lastName' in values ? values['lastName'] : before?.['lastName']\n if (textOf(first) || textOf(last)) return 'Built from the first and last names.'\n }\n return null\n })\n const optIns = site.hostId ? consentOptInEmails(plan, input.rows, input.existing) : []\n const suppressed = optIns.length\n ? await readSuppressedAddresses(env.firestore, site.group.hostIds.length ? site.group.hostIds : [site.hostId as string], optIns)\n : new Set<string>()\n return planContactConsent(plan, input.rows, input.existing, {\n hostId: site.hostId,\n attested: readContactConsentAnswer(ctx.extras).attest,\n recordedBy: ctx.actorUid,\n jobId: ctx.jobId ?? null,\n source: customerFileSource(ctx.headers),\n suppressed,\n }).plan\n },\n\n async apply(ctx, chunk, writer) {\n const env = await crmTransferEnv(ctx)\n requireCrmSuite(env, SUITE_ACT)\n const { hostId, group } = contactImportSite(env)\n const run: ApplyRun = {\n env,\n attested: readContactConsentAnswer(ctx.extras).attest,\n hostId,\n group,\n actorUid: ctx.actorUid,\n definitions: await crmCustomDefinitions(env, 'contact'),\n companies: new Map(),\n admit: crmRecordsRoom(env),\n readContact: cachedContactReader(contactsOf(env)),\n }\n const rows = chunk.rows as PlannedTransferRow[]\n const creating: string[] = []\n for (const row of rows) {\n if (row.verdict === 'create' && !(await writer.alreadyApplied(row.index))) {\n const email = textOf(plannedValues(row)['email'])\n if (email) creating.push(email)\n }\n }\n // The capture door's lookups for every new person at once (AGL-3423).\n const batch =\n hostId && creating.length ? await prepareContactCaptureBatch(hostId, creating) : undefined\n const results: TransferRowResult[] = []\n const undo: TransferUndoEntry[] = []\n\n await eachWithinBudget(rows, () => writer.timeLeftMs(), async (row) => {\n const earlier = await writer.alreadyApplied(row.index)\n if (earlier) {\n results.push(earlier)\n return\n }\n const changed = row.diff.map((change) => change.fieldId)\n let result: TransferRowResult\n let entry: TransferUndoEntry | undefined\n try {\n const values = plannedValues(row)\n // The store file's marketing choice is written on its own, checked again (AGL-4245).\n const consentChange = importedConsentChange(row)\n delete values[MARKETING_CONSENT_FIELD_ID]\n if (row.verdict === 'update' && row.recordId) {\n const before = (await readContacts(env, [row.recordId], run.definitions)).get(row.recordId)\n if (!before) throw new TransferEngineError('notFound', 404, 'That contact no longer exists.')\n const { skipped } = await writeContact(run, row.recordId, values)\n const consent = consentChange ? await writeConsent(run, row.recordId, consentChange.consent) : null\n const after = (await readContacts(env, [row.recordId], run.definitions)).get(row.recordId) ?? {}\n const message = joinMessages(skippedMessage(skipped), consentMessage(consent))\n result = { row: row.index, outcome: 'updated', recordId: row.recordId, ...(message ? { message } : {}) }\n const undoable = undoableFields(changed, consent)\n entry = undoable.length\n ? {\n row: row.index,\n recordId: row.recordId,\n action: 'updated',\n previous: pickValues(before, undoable),\n written: pickValues(after, undoable),\n ...(consentUndoExtras(consent)),\n }\n : undefined\n } else {\n const made = await createContact(run, values, batch)\n const consent = consentChange ? await writeConsent(run, made.contactId, consentChange.consent) : null\n const after = (await readContacts(env, [made.contactId], run.definitions)).get(made.contactId) ?? {}\n const message = made.created\n ? joinMessages(skippedMessage(made.skipped), consentMessage(consent))\n : 'Merged into the contact already holding this email.'\n result = {\n row: row.index,\n outcome: made.created ? 'created' : 'updated',\n recordId: made.contactId,\n ...(message ? { message } : {}),\n }\n // A merge into a person somebody else created meanwhile has no\n // \"before\" to put back, so undo leaves it alone.\n entry = made.created\n ? {\n row: row.index,\n recordId: made.contactId,\n action: 'created',\n written: pickValues(after, undoableFields(changed, consent)),\n ...(consentUndoExtras(consent)),\n }\n : undefined\n }\n } catch (error) {\n result = {\n row: row.index,\n outcome: 'failed',\n ...(row.recordId ? { recordId: row.recordId } : {}),\n message: failureMessage(error),\n }\n entry = undefined\n }\n await writer.markApplied(result, entry)\n results.push(result)\n if (entry) undo.push(entry)\n })\n return { results, undo }\n },\n\n async revert(ctx, snapshot, decisions) {\n const env = await crmTransferEnv(ctx)\n const { hostId, group } = contactImportSite(env)\n const run: ApplyRun = {\n env,\n attested: false,\n hostId,\n group,\n actorUid: ctx.actorUid,\n definitions: await crmCustomDefinitions(env, 'contact'),\n companies: new Map(),\n admit: crmRecordsRoom(env),\n readContact: cachedContactReader(contactsOf(env)),\n }\n const current = await readContacts(env, snapshot.entries.map((entry) => entry.recordId), run.definitions)\n const done: TransferUndoStep[] = []\n const conflicts: TransferUndoStep[] = []\n for (const entry of snapshot.entries) {\n const step = planTransferUndo(entry, current.get(entry.recordId) ?? null)\n if (step.action === 'conflict' && decisions?.[entry.recordId] !== 'revert') {\n if (decisions?.[entry.recordId] === 'keep') done.push({ action: 'nothing', recordId: entry.recordId, why: 'alreadyReverted' })\n else conflicts.push(step)\n continue\n }\n if (step.action === 'nothing') {\n done.push(step)\n continue\n }\n const consentUndo = readImportedConsentUndo(entry.extras)\n if (entry.action === 'created') {\n // The site lets the person go, as \"Remove from this site\" does: a\n // person only this import's site held is deleted, keeping every\n // refusal they made; one another site holds too stays theirs.\n const contactRef = contactsOf(env).doc(entry.recordId)\n // An opt-in the file brought goes with them; a refusal it brought\n // is kept in the retained-refusals store by the removal (AGL-4245).\n if (consentUndo) await revertImportedConsent({ firestore: env.firestore, contactRef, undo: consentUndo })\n let holders: string[] = []\n const removal = await removeContactKeepingRefusals({\n contactRef,\n decide: (contact) => {\n holders = contactFacetHolders(contact)\n return planContactDetach(contact, group)\n },\n nowMs: Date.now(),\n })\n if (removal.outcome === 'deleted') {\n await clearReportsToOf(env.firestore, contactsOf(env), holders, entry.recordId)\n await sweepDealContactRoles(\n env.firestore,\n env.orgRef.collection(CRM_COLLECTIONS.deals),\n entry.recordId,\n null,\n '[crm] transfer undo',\n )\n } else if (removal.outcome === 'detached') {\n await clearReportsToOf(env.firestore, contactsOf(env), [group.groupId], entry.recordId)\n await restampCrmListFieldsOf([contactRef], 'contacts').catch(() => undefined)\n }\n done.push({ action: 'delete', recordId: entry.recordId })\n continue\n }\n const values = step.action === 'restore' || step.action === 'conflict' ? { ...step.values } : {}\n if (MARKETING_CONSENT_FIELD_ID in values) {\n delete values[MARKETING_CONSENT_FIELD_ID]\n if (consentUndo) {\n await revertImportedConsent({ firestore: env.firestore, contactRef: contactsOf(env).doc(entry.recordId), undo: consentUndo })\n }\n }\n await writeContact(run, entry.recordId, values)\n done.push(step)\n }\n return { done, conflicts }\n },\n }\n}\n"],"names":["composeContactName","crmDefaultScopeOf","ORG_CONTACT_HOLDER_ID","contactDisplayName","contactFacetPath","CRM_COLLECTIONS","interactionsForGroup","isContactLifecycleStage","normalizeContactBirthdate","normalizePhone","planContactDetach","readContactFacet","readMarketingBasis","CONTACT_LIFECYCLE_STAGE_LABELS","CONTACT_EXTRA_PHONE_FIELDS","PHONE_FIELDS","buildTransferPlan","planTransferUndo","rankTransferLookupSuggestions","TRANSFER_ID_FIELD","transferLookupNewName","prepareContactCaptureBatch","removeContactKeepingRefusals","restampCrmListFieldsOf","emitHostEvent","FieldValue","contactPrimaryGroup","ORG_HOLDER_GROUP","captureHostContact","CONTACT_NOTES_MAX","cachedContactReader","clearReportsToOf","CONTACT_REPORTS_TO_LOOP_REFUSAL","CONTACT_REPORTS_TO_SELF_REFUSAL","contactFacetHolders","contactReportsToLoops","contactPatch","settleCompanyCounts","sweepDealContactRoles","CONTACT_ALIASES","consentOptInEmails","importedConsentChange","planContactConsent","withoutCustomerSourceFields","CUSTOMER_FIELD","customerFileSource","MARKETING_CONSENT_FIELD_ID","readContactConsentAnswer","readImportedConsentUndo","readSuppressedAddresses","revertImportedConsent","writeImportedConsent","addCrmPicklistValues","addressFromValues","addressValues","countCrmExport","crmCustomDefinitions","crmMemberEmails","crmMembersTarget","crmPicklistLists","crmRecordsRoom","crmTransferEnv","customTransferValues","customWriteValues","eachWithinBudget","failureMessage","holdBackChanges","IN_LIMIT","isoOf","lookupByField","lookupById","lookupResult","pickValues","plannedValues","readCrmExportPage","requireCrmSuite","requireSite","textOf","TransferEngineError","visibleIn","companyForName","addContactByHand","CONTACT_TRANSFER_DERIVED","CONTACT_TRANSFER_FIELDS","CONTACT_TRANSFER_GROUPS","CRM_MEMBERS_TARGET","CRM_TIMESTAMP_FIELDS","crmCustomTransferFields","crmLifecycleStageOf","crmLifecycleStageRank","CONTACT_TRANSFER_INTERACTION_SUMMARY","SUITE_ACT","contactsOf","env","orgRef","collection","holderOf","contact","group","org","CAPTURE_REFUSALS","band","erased","error","contactTransferValues","id","definitions","names","facet","groupId","text","value","stage","lifecycleStage","basis","ownerUid","managerId","reportsToContactId","name","email","salutation","firstName","lastName","birthdate","phone","mobilePhone","homePhone","otherPhone","fax","doNotCall","assistantName","assistantPhone","jobTitle","department","company","companyName","companyId","reportsTo","manager","address","otherAddress","owner","leadSource","tags","Array","isArray","notes","marketingConsent","sources","Object","keys","lastInteractionAt","interactions","hostIds","atMs","lastEngagedAt","lastEmailEngagementAtMs","createdAt","updatedAt","custom","readContacts","ids","out","Map","at","length","page","slice","filter","includes","snapshot","firestore","getAll","map","doc","data","exists","set","companyLinkFor","run","undefined","create","actorUid","companies","admit","String","get","profileFields","values","stored","fields","skipped","key","max","typed","push","mailing","other","composed","skippedMessage","labels","find","field","label","join","writeContact","contactId","patch","ref","hostId","linkedCompanyName","link","readContact","refused","update","movedFrom","previous","delete","serverTimestamp","counts","previousStage","actor","kind","uid","catch","console","contactImportSite","createContact","batch","fromEntries","staged","verdict","source","interaction","summary","campaignIds","orgId","sourceHostId","extra","created","consentMessage","outcome","message","undoableFields","changed","consent","fieldId","consentUndoExtras","undo","extras","joinMessages","parts","Boolean","writeConsent","detail","contactRef","attestedNow","attested","fallbackBy","Date","now","contactsTransferResource","ctx","standard","derived","system","groups","matchKeys","normalizer","aliases","lookupTargets","count","options","scopeTokens","readPage","cursor","fieldIds","emails","docs","managers","Set","lookup","requests","visible","found","request","trim","toLowerCase","suggest","answer","word","split","where","limit","candidates","flatMap","recordId","picklists","addPicklistValues","picklistId","lockedRules","reason","forced","mode","refuseValues","emailMarketing","smsMarketing","totalSpent","orderCount","customerId","plan","input","site","rows","row","change","before","existing","from","to","after","diff","entry","first","last","optIns","suppressed","attest","recordedBy","jobId","headers","apply","chunk","writer","creating","alreadyApplied","index","results","timeLeftMs","earlier","result","consentChange","undoable","action","written","made","markApplied","revert","decisions","current","entries","done","conflicts","step","why","consentUndo","holders","removal","decide","now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