@aglyn/plugins-email 1.0.0-beta.143
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/LICENSE +201 -0
- package/README.md +7 -0
- package/package.json +58 -0
- package/src/index.d.ts +35 -0
- package/src/index.js +35 -0
- package/src/index.js.map +1 -0
- package/src/lib/components/campaign-design-create-widget.d.ts +21 -0
- package/src/lib/components/campaign-design-create-widget.js +61 -0
- package/src/lib/components/campaign-design-create-widget.js.map +1 -0
- package/src/lib/components/campaign-sender-editor-widget.d.ts +18 -0
- package/src/lib/components/campaign-sender-editor-widget.js +39 -0
- package/src/lib/components/campaign-sender-editor-widget.js.map +1 -0
- package/src/lib/components/campaign-topic-options-widget.d.ts +19 -0
- package/src/lib/components/campaign-topic-options-widget.js +48 -0
- package/src/lib/components/campaign-topic-options-widget.js.map +1 -0
- package/src/lib/components/campaign-topic-select.d.ts +32 -0
- package/src/lib/components/campaign-topic-select.js +87 -0
- package/src/lib/components/campaign-topic-select.js.map +1 -0
- package/src/lib/components/dynamic-list-rule-fields.d.ts +173 -0
- package/src/lib/components/dynamic-list-rule-fields.js +1473 -0
- package/src/lib/components/dynamic-list-rule-fields.js.map +1 -0
- package/src/lib/components/email-blocks.d.ts +111 -0
- package/src/lib/components/email-blocks.js +875 -0
- package/src/lib/components/email-blocks.js.map +1 -0
- package/src/lib/components/email-design-preview.d.ts +62 -0
- package/src/lib/components/email-design-preview.js +174 -0
- package/src/lib/components/email-design-preview.js.map +1 -0
- package/src/lib/components/email-screens-card.d.ts +42 -0
- package/src/lib/components/email-screens-card.js +277 -0
- package/src/lib/components/email-screens-card.js.map +1 -0
- package/src/lib/components/email-template-detail.d.ts +48 -0
- package/src/lib/components/email-template-detail.js +681 -0
- package/src/lib/components/email-template-detail.js.map +1 -0
- package/src/lib/components/email-topic-detail.d.ts +32 -0
- package/src/lib/components/email-topic-detail.js +293 -0
- package/src/lib/components/email-topic-detail.js.map +1 -0
- package/src/lib/components/email-topics-card.d.ts +46 -0
- package/src/lib/components/email-topics-card.js +327 -0
- package/src/lib/components/email-topics-card.js.map +1 -0
- package/src/lib/components/email-zones.d.ts +28 -0
- package/src/lib/components/email-zones.js +20 -0
- package/src/lib/components/email-zones.js.map +1 -0
- package/src/lib/components/emails-console-page.d.ts +32 -0
- package/src/lib/components/emails-console-page.js +229 -0
- package/src/lib/components/emails-console-page.js.map +1 -0
- package/src/lib/components/emails-console-sections.d.ts +36 -0
- package/src/lib/components/emails-console-sections.js +108 -0
- package/src/lib/components/emails-console-sections.js.map +1 -0
- package/src/lib/components/list-detail-card.d.ts +47 -0
- package/src/lib/components/list-detail-card.js +273 -0
- package/src/lib/components/list-detail-card.js.map +1 -0
- package/src/lib/components/list-edit-card.d.ts +11 -0
- package/src/lib/components/list-edit-card.js +287 -0
- package/src/lib/components/list-edit-card.js.map +1 -0
- package/src/lib/components/list-import-drawer.d.ts +22 -0
- package/src/lib/components/list-import-drawer.js +662 -0
- package/src/lib/components/list-import-drawer.js.map +1 -0
- package/src/lib/components/list-members-panel.d.ts +94 -0
- package/src/lib/components/list-members-panel.js +686 -0
- package/src/lib/components/list-members-panel.js.map +1 -0
- package/src/lib/components/lists-card.d.ts +28 -0
- package/src/lib/components/lists-card.js +377 -0
- package/src/lib/components/lists-card.js.map +1 -0
- package/src/lib/components/sending-domain-detail.d.ts +26 -0
- package/src/lib/components/sending-domain-detail.js +496 -0
- package/src/lib/components/sending-domain-detail.js.map +1 -0
- package/src/lib/components/sending-domains-card.d.ts +33 -0
- package/src/lib/components/sending-domains-card.js +962 -0
- package/src/lib/components/sending-domains-card.js.map +1 -0
- package/src/lib/components/sending-sender-drawer.d.ts +94 -0
- package/src/lib/components/sending-sender-drawer.js +543 -0
- package/src/lib/components/sending-sender-drawer.js.map +1 -0
- package/src/lib/components/suppressions-card.d.ts +49 -0
- package/src/lib/components/suppressions-card.js +639 -0
- package/src/lib/components/suppressions-card.js.map +1 -0
- package/src/lib/components/use-org-email-topics.d.ts +79 -0
- package/src/lib/components/use-org-email-topics.js +111 -0
- package/src/lib/components/use-org-email-topics.js.map +1 -0
- package/src/lib/constants/bundle-common.d.ts +18 -0
- package/src/lib/constants/bundle-common.js +18 -0
- package/src/lib/constants/bundle-common.js.map +1 -0
- package/src/lib/hooks/use-org-company-options.d.ts +20 -0
- package/src/lib/hooks/use-org-company-options.js +138 -0
- package/src/lib/hooks/use-org-company-options.js.map +1 -0
- package/src/lib/hooks/use-org-contact-fields.d.ts +40 -0
- package/src/lib/hooks/use-org-contact-fields.js +91 -0
- package/src/lib/hooks/use-org-contact-fields.js.map +1 -0
- package/src/lib/hooks/use-org-contact-segments.d.ts +16 -0
- package/src/lib/hooks/use-org-contact-segments.js +55 -0
- package/src/lib/hooks/use-org-contact-segments.js.map +1 -0
- package/src/lib/hooks/use-org-crm-views.d.ts +8 -0
- package/src/lib/hooks/use-org-crm-views.js +74 -0
- package/src/lib/hooks/use-org-crm-views.js.map +1 -0
- package/src/lib/hooks/use-org-lists.d.ts +8 -0
- package/src/lib/hooks/use-org-lists.js +47 -0
- package/src/lib/hooks/use-org-lists.js.map +1 -0
- package/src/lib/model/email-design-document.d.ts +52 -0
- package/src/lib/model/email-design-document.js +62 -0
- package/src/lib/model/email-design-document.js.map +1 -0
- package/src/lib/model/index.d.ts +64 -0
- package/src/lib/model/index.js +71 -0
- package/src/lib/model/index.js.map +1 -0
- package/src/lib/model/sending-domain-status.d.ts +99 -0
- package/src/lib/model/sending-domain-status.js +196 -0
- package/src/lib/model/sending-domain-status.js.map +1 -0
- package/src/lib/model/template-provenance.d.ts +113 -0
- package/src/lib/model/template-provenance.js +107 -0
- package/src/lib/model/template-provenance.js.map +1 -0
- package/src/lib/model/template-report.d.ts +158 -0
- package/src/lib/model/template-report.js +249 -0
- package/src/lib/model/template-report.js.map +1 -0
- package/src/lib/plugin.d.ts +27 -0
- package/src/lib/plugin.js +163 -0
- package/src/lib/plugin.js.map +1 -0
- package/src/lib/server-console.d.ts +116 -0
- package/src/lib/server-console.js +422 -0
- package/src/lib/server-console.js.map +1 -0
- package/src/lib/server-email-drafts.d.ts +104 -0
- package/src/lib/server-email-drafts.js +381 -0
- package/src/lib/server-email-drafts.js.map +1 -0
- package/src/lib/server-list-gate.d.ts +183 -0
- package/src/lib/server-list-gate.js +365 -0
- package/src/lib/server-list-gate.js.map +1 -0
- package/src/lib/server-list-import.d.ts +199 -0
- package/src/lib/server-list-import.js +632 -0
- package/src/lib/server-list-import.js.map +1 -0
- package/src/lib/server-suppressions.d.ts +135 -0
- package/src/lib/server-suppressions.js +295 -0
- package/src/lib/server-suppressions.js.map +1 -0
- package/src/lib/server.d.ts +19 -0
- package/src/lib/server.js +834 -0
- package/src/lib/server.js.map +1 -0
- package/src/lib/site.d.ts +26 -0
- package/src/lib/site.js +81 -0
- package/src/lib/site.js.map +1 -0
- package/src/lib/unsubscribe-link.d.ts +311 -0
- package/src/lib/unsubscribe-link.js +398 -0
- package/src/lib/unsubscribe-link.js.map +1 -0
- package/src/lib/utils/create-email-screen.d.ts +59 -0
- package/src/lib/utils/create-email-screen.js +59 -0
- package/src/lib/utils/create-email-screen.js.map +1 -0
- package/src/lib/utils/generate-preset-id.d.ts +19 -0
- package/src/lib/utils/generate-preset-id.js +25 -0
- package/src/lib/utils/generate-preset-id.js.map +1 -0
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2026 Aglyn LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/ /**
|
|
18
|
+
* BRINGING AN EXISTING LIST IN — the four routes an import is made of.
|
|
19
|
+
*
|
|
20
|
+
* `docs/specs/email-competitive-gaps.md` G5: export works, import does not
|
|
21
|
+
* exist, and every customer arriving from another product has a list and no
|
|
22
|
+
* way to bring it. P4 is the condition attached to closing that: a bulk
|
|
23
|
+
* import is the fastest way to destroy a shared sending domain, so it ships
|
|
24
|
+
* WITH its controls.
|
|
25
|
+
*
|
|
26
|
+
* ## Every imported address goes through the checks a typed one does
|
|
27
|
+
*
|
|
28
|
+
* Not a similar set — the same functions. {@link resolveAddresses} is the
|
|
29
|
+
* resolution `email/list-members-preview` runs, `assignmentBasis` is the
|
|
30
|
+
* policy `email/list-members-add` applies, and `enrollListMember` is the one
|
|
31
|
+
* writer of the membership collection. A second bulk path with its own idea of
|
|
32
|
+
* suppression and its own idea of consent is exactly the defect class this
|
|
33
|
+
* register has a P1 entry for, and that entry is closed.
|
|
34
|
+
*
|
|
35
|
+
* What this module adds is everything ABOVE that gate: reading a file,
|
|
36
|
+
* screening it, holding the operator's attestation, and metering the work out
|
|
37
|
+
* over as many requests as it takes.
|
|
38
|
+
*
|
|
39
|
+
* ## Four routes, because an import is four separate acts
|
|
40
|
+
*
|
|
41
|
+
* - `email/list-import-preview` — reads the file and says what is in it.
|
|
42
|
+
* Writes nothing, enrolls nobody, and resolves a BOUNDED SAMPLE through the
|
|
43
|
+
* consent gate so the numbers the operator attests against are real numbers
|
|
44
|
+
* from real records rather than a promise.
|
|
45
|
+
* - `email/list-import-start` — records the attestation and stages the
|
|
46
|
+
* addresses. Still enrolls nobody: the act of saying "I have permission for
|
|
47
|
+
* these people" is separated from the act of adding them so that the
|
|
48
|
+
* attestation has a moment of its own.
|
|
49
|
+
* - `email/list-import-run` — enrolls up to {@link LIST_IMPORT_RUN_BUDGET}
|
|
50
|
+
* addresses from the cursor and moves it. Called until it answers
|
|
51
|
+
* `complete`.
|
|
52
|
+
* - `email/list-import-status` — the unfinished import on a list, if there is
|
|
53
|
+
* one, so a merchant who closed the tab is not left with a half-added
|
|
54
|
+
* audience and no way to see it.
|
|
55
|
+
*
|
|
56
|
+
* ## A budget and a cursor, not one request and not one transaction
|
|
57
|
+
*
|
|
58
|
+
* The shape `dynamic-list-materialize.ts` already uses: a per-run bound on
|
|
59
|
+
* work, a cursor recording where the run stopped, and a next run that resumes
|
|
60
|
+
* rather than restarts. A 50,000-address file is not a request that times out
|
|
61
|
+
* halfway with no record of what it did; it is 500 bounded requests over one
|
|
62
|
+
* durable job, and stopping in the middle of it leaves the addresses already
|
|
63
|
+
* enrolled enrolled and the rest staged.
|
|
64
|
+
*
|
|
65
|
+
* The run budget is deliberately {@link LIST_MEMBER_BATCH_MAX} — the same
|
|
66
|
+
* number of addresses one hand-typed add already resolves in one request — so
|
|
67
|
+
* an import run costs exactly what an add costs and no new cost profile is
|
|
68
|
+
* introduced to discover in production.
|
|
69
|
+
*
|
|
70
|
+
* ## ⛔ Nothing here is a capacity limit
|
|
71
|
+
*
|
|
72
|
+
* {@link LIST_IMPORT_MAX_ADDRESSES} refuses a FILE before anything is written.
|
|
73
|
+
* It never trims a staged import, never drops an address to fit, and never
|
|
74
|
+
* removes anybody already on the list. A ceiling in this product is enforced
|
|
75
|
+
* at the reduction, and the reduction here is refusing the upload — which the
|
|
76
|
+
* operator sees, can argue with, and can act on by splitting the file.
|
|
77
|
+
*
|
|
78
|
+
* ## Who the attester is, and why a resumer does not become one
|
|
79
|
+
*
|
|
80
|
+
* The attestation is one person's claim about where a file came from. It is
|
|
81
|
+
* recorded on the job with the account that made it, and every run reads the
|
|
82
|
+
* basis from THAT account, not from whoever pressed Resume. A colleague who
|
|
83
|
+
* finishes somebody else's import has asserted nothing, and the consent
|
|
84
|
+
* records the run writes must not say they did.
|
|
85
|
+
*/ import { ASSIGNMENT_REFUSAL_MESSAGES, assignmentBasis, createResourceUid, importedBasisReason, LIST_IMPORT_MAX_ADDRESSES, LIST_IMPORT_MAX_CHARACTERS, parseListImport, readMarketingBasis, registerPluginApiRoute, screenListImport } from "@aglyn/aglyn/server";
|
|
86
|
+
import { enrollListMember } from "@aglyn/tenant-data-admin";
|
|
87
|
+
import { FieldValue } from "firebase-admin/firestore";
|
|
88
|
+
/*
|
|
89
|
+
* The gate module directly, never `server-console.ts`'s re-export of it.
|
|
90
|
+
*
|
|
91
|
+
* That file imports THIS one to register these routes, so reaching its
|
|
92
|
+
* re-export would close a cycle — and the constant below is evaluated at
|
|
93
|
+
* module load, which is precisely where a cycle stops being harmless: the
|
|
94
|
+
* binding is still in its temporal dead zone when the loader arrives.
|
|
95
|
+
*/ import { LIST_MEMBER_BATCH_MAX, resolveAddresses, resolveListContext } from "./server-list-gate.js";
|
|
96
|
+
/** `source` stamped on every membership an import writes. */ export const CONSOLE_IMPORT_SOURCE = 'console:list-import';
|
|
97
|
+
/** Where a list's import jobs live: `orgs/{orgId}/lists/{listId}/imports`. */ export const LIST_IMPORTS_SUBCOLLECTION = 'imports';
|
|
98
|
+
/**
|
|
99
|
+
* Addresses one run enrolls before answering and handing back the cursor.
|
|
100
|
+
*
|
|
101
|
+
* The same number as {@link LIST_MEMBER_BATCH_MAX} on purpose — see the module
|
|
102
|
+
* note. It is a bound on WORK: it can never refuse a person, and the addresses
|
|
103
|
+
* it does not reach in this run are reached by the next one.
|
|
104
|
+
*/ export const LIST_IMPORT_RUN_BUDGET = LIST_MEMBER_BATCH_MAX;
|
|
105
|
+
/**
|
|
106
|
+
* Staged addresses per chunk document.
|
|
107
|
+
*
|
|
108
|
+
* Comfortably inside Firestore's one-megabyte document limit at the widths a
|
|
109
|
+
* contact file actually carries, and a multiple of the run budget so a run
|
|
110
|
+
* reads exactly one chunk. Reading two would be the common case at any size
|
|
111
|
+
* that is not a multiple, which is a round trip paid on every run to save
|
|
112
|
+
* nothing.
|
|
113
|
+
*/ export const LIST_IMPORT_CHUNK_SIZE = 500;
|
|
114
|
+
/** Unusable lines kept verbatim on the job, so the result names some of them. */ const UNUSABLE_SAMPLE_MAX = 25;
|
|
115
|
+
/** Role accounts kept verbatim on the job, for the same reason. */ const ROLE_ACCOUNT_SAMPLE_MAX = 25;
|
|
116
|
+
/** Reads the request's file text, or the refusal to send back. */ function readImportText(req) {
|
|
117
|
+
var _ref;
|
|
118
|
+
var _req_body;
|
|
119
|
+
const text = String((_ref = (_req_body = req.body) == null ? void 0 : _req_body.text) != null ? _ref : '');
|
|
120
|
+
if (!text.trim()) return {
|
|
121
|
+
error: 'The file is empty.'
|
|
122
|
+
};
|
|
123
|
+
if (text.length > LIST_IMPORT_MAX_CHARACTERS) {
|
|
124
|
+
return {
|
|
125
|
+
error: 'That file is too large to read in one go. Split it and import the ' + 'pieces — nothing is added until you do, and nothing already on the ' + 'list is affected.'
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
text
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The screening report for a parsed file.
|
|
134
|
+
*
|
|
135
|
+
* Counts plus a bounded sample rather than every offending address. The point
|
|
136
|
+
* of the warning is that the operator SEES the shape of what they are about to
|
|
137
|
+
* attest to; a list of four thousand role accounts is a scroll, not a warning,
|
|
138
|
+
* and it would put four thousand addresses into a document whose reason for
|
|
139
|
+
* existing is bookkeeping.
|
|
140
|
+
*/ function screeningReport(parsed) {
|
|
141
|
+
const screening = screenListImport(parsed);
|
|
142
|
+
return {
|
|
143
|
+
roleAccounts: screening.roleAccounts.length,
|
|
144
|
+
roleAccountSamples: screening.roleAccounts.slice(0, ROLE_ACCOUNT_SAMPLE_MAX),
|
|
145
|
+
purchaseTellColumns: screening.purchaseTellColumns,
|
|
146
|
+
declaresBasis: screening.declaresBasis
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* `POST email/list-import-preview` — what is in this file.
|
|
151
|
+
*
|
|
152
|
+
* Reads only. It answers three separate questions and keeps them separate,
|
|
153
|
+
* because collapsing them is how an import gets attested to on a number that
|
|
154
|
+
* is not the number:
|
|
155
|
+
*
|
|
156
|
+
* - what the FILE contains: usable addresses, unusable lines, duplicates
|
|
157
|
+
* collapsed, and the columns it carries;
|
|
158
|
+
* - what the SCREENING found, which decides nothing and is shown anyway;
|
|
159
|
+
* - what the CONSENT GATE says about a bounded sample of the addresses.
|
|
160
|
+
*
|
|
161
|
+
* The sample is the honest shape rather than a shortcut. Resolving fifty
|
|
162
|
+
* thousand addresses against the contacts collection and both suppression
|
|
163
|
+
* lists is the same scan the import itself performs, so a preview that did it
|
|
164
|
+
* would be the import minus the writes — twice the cost, and a request that
|
|
165
|
+
* times out on exactly the files this feature exists for. So the sample size
|
|
166
|
+
* is reported beside the total and the run reports the real figures as they
|
|
167
|
+
* become true, which is the same distinction `email/list-rule-preview` draws
|
|
168
|
+
* between `matched` and the batch it hands back.
|
|
169
|
+
*/ export const emailListImportPreviewHandler = async (req, res)=>{
|
|
170
|
+
if (req.method !== 'POST') {
|
|
171
|
+
return res.status(405).json({
|
|
172
|
+
error: 'Method not allowed'
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
const file = readImportText(req);
|
|
176
|
+
if ('error' in file) return res.status(400).json({
|
|
177
|
+
error: file.error
|
|
178
|
+
});
|
|
179
|
+
try {
|
|
180
|
+
const context = await resolveListContext(req);
|
|
181
|
+
if (context.ok === false) {
|
|
182
|
+
return res.status(context.status).json(context.body);
|
|
183
|
+
}
|
|
184
|
+
const parsed = parseListImport(file.text);
|
|
185
|
+
const sample = parsed.rows.map((row)=>row.email).filter((email)=>!!email).slice(0, LIST_MEMBER_BATCH_MAX);
|
|
186
|
+
const resolution = await resolveAddresses({
|
|
187
|
+
hostId: context.hostId,
|
|
188
|
+
inputs: sample
|
|
189
|
+
});
|
|
190
|
+
return res.status(200).json({
|
|
191
|
+
listName: context.listName,
|
|
192
|
+
columns: parsed.columns,
|
|
193
|
+
usable: parsed.usable,
|
|
194
|
+
unusable: parsed.unusable,
|
|
195
|
+
duplicates: parsed.duplicates,
|
|
196
|
+
overCeiling: parsed.overCeiling,
|
|
197
|
+
ceiling: LIST_IMPORT_MAX_ADDRESSES,
|
|
198
|
+
unusableSamples: parsed.rows.filter((row)=>!row.email).slice(0, UNUSABLE_SAMPLE_MAX).map((row)=>row.input),
|
|
199
|
+
screening: screeningReport(parsed),
|
|
200
|
+
/*
|
|
201
|
+
* The sample's verdicts, in the shape the panel's consent readout
|
|
202
|
+
* already draws, and its SIZE beside them. A count with no denominator
|
|
203
|
+
* next to it is the thing an operator misreads as the whole file.
|
|
204
|
+
*/ sampleSize: sample.length,
|
|
205
|
+
verdicts: resolution.verdicts,
|
|
206
|
+
optedIn: resolution.optedIn,
|
|
207
|
+
needAttestation: resolution.needAttestation,
|
|
208
|
+
refused: resolution.refused
|
|
209
|
+
});
|
|
210
|
+
} catch (error) {
|
|
211
|
+
console.error('[email] list import preview failed', error);
|
|
212
|
+
return res.status(500).json({
|
|
213
|
+
error: 'The file could not be read.'
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* `POST email/list-import-start` — record the attestation, stage the file.
|
|
219
|
+
*
|
|
220
|
+
* Body: `{ hostId, listId, text, attestConsent }`. Enrolls nobody. It writes
|
|
221
|
+
* the job document that every subsequent run reads, and the chunks holding
|
|
222
|
+
* the addresses, and then stops — so the moment the operator makes their
|
|
223
|
+
* claim is a moment of its own, with a record of who made it and when, rather
|
|
224
|
+
* than a flag riding along on the request that also did the work.
|
|
225
|
+
*
|
|
226
|
+
* `attestConsent` is the operator STATING they have these people's
|
|
227
|
+
* permission. It is not a way to name a basis: the basis is derived per
|
|
228
|
+
* address at run time from that person's own record, exactly as the
|
|
229
|
+
* one-address add path derives it, and this flag can only ever produce the
|
|
230
|
+
* attributable kind.
|
|
231
|
+
*/ export const emailListImportStartHandler = async (req, res)=>{
|
|
232
|
+
var _req_body;
|
|
233
|
+
if (req.method !== 'POST') {
|
|
234
|
+
return res.status(405).json({
|
|
235
|
+
error: 'Method not allowed'
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
const file = readImportText(req);
|
|
239
|
+
if ('error' in file) return res.status(400).json({
|
|
240
|
+
error: file.error
|
|
241
|
+
});
|
|
242
|
+
const attested = ((_req_body = req.body) == null ? void 0 : _req_body.attestConsent) === true;
|
|
243
|
+
try {
|
|
244
|
+
const context = await resolveListContext(req);
|
|
245
|
+
if (context.ok === false) {
|
|
246
|
+
return res.status(context.status).json(context.body);
|
|
247
|
+
}
|
|
248
|
+
const parsed = parseListImport(file.text);
|
|
249
|
+
const staged = parsed.rows.filter((row)=>!!row.email).map((row)=>_extends({
|
|
250
|
+
e: row.email
|
|
251
|
+
}, row.name ? {
|
|
252
|
+
n: row.name
|
|
253
|
+
} : {}, row.declaredSource ? {
|
|
254
|
+
s: row.declaredSource
|
|
255
|
+
} : {}, row.declaredAt ? {
|
|
256
|
+
d: row.declaredAt
|
|
257
|
+
} : {}));
|
|
258
|
+
if (!staged.length) {
|
|
259
|
+
return res.status(400).json({
|
|
260
|
+
error: 'No usable email addresses were found in that file. Check that it ' + 'has an address column, or paste one address per line.'
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
const importId = createResourceUid();
|
|
264
|
+
const importRef = context.listRef.collection(LIST_IMPORTS_SUBCOLLECTION).doc(importId);
|
|
265
|
+
const chunks = importRef.collection('chunks');
|
|
266
|
+
/*
|
|
267
|
+
* The staging area first, the job document last.
|
|
268
|
+
*
|
|
269
|
+
* A job whose chunks are not all written yet is a job a run would read
|
|
270
|
+
* past the end of, and the run is driven by a client that starts
|
|
271
|
+
* immediately. Writing the job last means the only state anybody can
|
|
272
|
+
* observe is a complete one.
|
|
273
|
+
*
|
|
274
|
+
* One document per chunk and not one batch over all of them: a batch is
|
|
275
|
+
* capped at 500 writes and, more to the point, is a transaction — the
|
|
276
|
+
* whole reason this is a staged job rather than one request is that a
|
|
277
|
+
* fifty-thousand-address import must not be a single atomic thing that
|
|
278
|
+
* either lands or does not.
|
|
279
|
+
*/ for(let at = 0; at < staged.length; at += LIST_IMPORT_CHUNK_SIZE){
|
|
280
|
+
await chunks.doc(String(at / LIST_IMPORT_CHUNK_SIZE)).set({
|
|
281
|
+
rows: staged.slice(at, at + LIST_IMPORT_CHUNK_SIZE)
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
await importRef.set({
|
|
285
|
+
listName: context.listName,
|
|
286
|
+
status: 'running',
|
|
287
|
+
total: staged.length,
|
|
288
|
+
cursor: 0,
|
|
289
|
+
enrolled: 0,
|
|
290
|
+
refused: 0,
|
|
291
|
+
refusals: {},
|
|
292
|
+
unusable: parsed.unusable,
|
|
293
|
+
duplicates: parsed.duplicates,
|
|
294
|
+
overCeiling: parsed.overCeiling,
|
|
295
|
+
unusableSamples: parsed.rows.filter((row)=>!row.email).slice(0, UNUSABLE_SAMPLE_MAX).map((row)=>row.input),
|
|
296
|
+
columns: parsed.columns,
|
|
297
|
+
screening: screeningReport(parsed),
|
|
298
|
+
/*
|
|
299
|
+
* WHO attested, stored beside WHETHER. A flag on its own is an
|
|
300
|
+
* unattributed claim, which is the one thing `list-assignment-policy`
|
|
301
|
+
* refuses to let an attestation be — and every run reads the account
|
|
302
|
+
* from here rather than from the session that triggered it.
|
|
303
|
+
*/ attested,
|
|
304
|
+
attestedByUid: attested ? context.uid : null,
|
|
305
|
+
attestedAtMs: attested ? Date.now() : null,
|
|
306
|
+
startedByUid: context.uid,
|
|
307
|
+
createdAt: FieldValue.serverTimestamp(),
|
|
308
|
+
updatedAt: FieldValue.serverTimestamp()
|
|
309
|
+
});
|
|
310
|
+
return res.status(200).json({
|
|
311
|
+
importId,
|
|
312
|
+
listName: context.listName,
|
|
313
|
+
total: staged.length,
|
|
314
|
+
attested,
|
|
315
|
+
runBudget: LIST_IMPORT_RUN_BUDGET
|
|
316
|
+
});
|
|
317
|
+
} catch (error) {
|
|
318
|
+
console.error('[email] list import start failed', error);
|
|
319
|
+
return res.status(500).json({
|
|
320
|
+
error: 'The import could not be started.'
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
/** The staged addresses a run will work on, from the cursor. */ async function readStaged(importRef, cursor, total) {
|
|
325
|
+
const take = Math.min(LIST_IMPORT_RUN_BUDGET, Math.max(total - cursor, 0));
|
|
326
|
+
if (take <= 0) return [];
|
|
327
|
+
const rows = [];
|
|
328
|
+
let at = cursor;
|
|
329
|
+
/*
|
|
330
|
+
* A loop rather than one read.
|
|
331
|
+
*
|
|
332
|
+
* `LIST_IMPORT_CHUNK_SIZE` is a multiple of `LIST_IMPORT_RUN_BUDGET`, so as
|
|
333
|
+
* those two constants stand a run reads exactly one chunk and this turns
|
|
334
|
+
* once. That relationship is a PERFORMANCE choice — one round trip per run
|
|
335
|
+
* — and the loop is what keeps it from also being a correctness
|
|
336
|
+
* requirement: change either number to something that does not divide, or
|
|
337
|
+
* resume a job whose cursor came from an older budget, and a run's batch
|
|
338
|
+
* straddles a boundary. Reading one chunk and truncating would silently
|
|
339
|
+
* import a short batch and advance the cursor past the rest.
|
|
340
|
+
*/ while(rows.length < take){
|
|
341
|
+
const index = Math.floor(at / LIST_IMPORT_CHUNK_SIZE);
|
|
342
|
+
const snapshot = await importRef.collection('chunks').doc(String(index)).get();
|
|
343
|
+
const stored = snapshot.exists ? snapshot.get('rows') : null;
|
|
344
|
+
if (!Array.isArray(stored) || !stored.length) break;
|
|
345
|
+
const offset = at - index * LIST_IMPORT_CHUNK_SIZE;
|
|
346
|
+
const slice = stored.slice(offset, offset + (take - rows.length));
|
|
347
|
+
if (!slice.length) break;
|
|
348
|
+
rows.push(...slice);
|
|
349
|
+
at += slice.length;
|
|
350
|
+
}
|
|
351
|
+
return rows;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* `POST email/list-import-run` — enroll the next batch.
|
|
355
|
+
*
|
|
356
|
+
* Body: `{ hostId, listId, importId }`. Answers `complete` when the cursor
|
|
357
|
+
* has reached the total, so the caller's loop is "call until complete" and
|
|
358
|
+
* nothing has to guess how many runs a file needs.
|
|
359
|
+
*
|
|
360
|
+
* ## The cursor moves for every address, enrolled or refused
|
|
361
|
+
*
|
|
362
|
+
* A refusal is a finished address. Advancing only on success would put a
|
|
363
|
+
* suppressed address at the head of the queue forever and turn the import
|
|
364
|
+
* into a loop that never terminates on exactly the files that most need to
|
|
365
|
+
* terminate.
|
|
366
|
+
*
|
|
367
|
+
* ## The counters are incremented, not recomputed
|
|
368
|
+
*
|
|
369
|
+
* `FieldValue.increment` rather than a read-modify-write, so two runs racing
|
|
370
|
+
* on one job — a merchant with the drawer open in two tabs — cannot lose a
|
|
371
|
+
* batch's worth of tally. The cursor is written as an absolute value because
|
|
372
|
+
* it is the position the NEXT run reads from, and two racing runs that both
|
|
373
|
+
* incremented it would skip a batch rather than repeat one; repeating is safe
|
|
374
|
+
* (`enrollListMember` is keyed by the person), skipping is not.
|
|
375
|
+
*/ export const emailListImportRunHandler = async (req, res)=>{
|
|
376
|
+
var _ref;
|
|
377
|
+
var _req_body;
|
|
378
|
+
if (req.method !== 'POST') {
|
|
379
|
+
return res.status(405).json({
|
|
380
|
+
error: 'Method not allowed'
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
const importId = String((_ref = (_req_body = req.body) == null ? void 0 : _req_body.importId) != null ? _ref : '').trim();
|
|
384
|
+
if (!importId) return res.status(400).json({
|
|
385
|
+
error: 'Missing importId'
|
|
386
|
+
});
|
|
387
|
+
try {
|
|
388
|
+
var _job_get, _job_get1, _job_get2;
|
|
389
|
+
const context = await resolveListContext(req);
|
|
390
|
+
if (context.ok === false) {
|
|
391
|
+
return res.status(context.status).json(context.body);
|
|
392
|
+
}
|
|
393
|
+
const importRef = context.listRef.collection(LIST_IMPORTS_SUBCOLLECTION).doc(importId);
|
|
394
|
+
const job = await importRef.get();
|
|
395
|
+
if (!job.exists) {
|
|
396
|
+
return res.status(404).json({
|
|
397
|
+
error: 'Unknown import'
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
const total = Number((_job_get = job.get('total')) != null ? _job_get : 0);
|
|
401
|
+
const cursor = Number((_job_get1 = job.get('cursor')) != null ? _job_get1 : 0);
|
|
402
|
+
if (cursor >= total) {
|
|
403
|
+
return res.status(200).json(finishedPayload(job, context));
|
|
404
|
+
}
|
|
405
|
+
const staged = await readStaged(importRef, cursor, total);
|
|
406
|
+
if (!staged.length) {
|
|
407
|
+
/*
|
|
408
|
+
* The staging area is short of what the job claims. Recorded as
|
|
409
|
+
* complete rather than retried forever: the addresses that were
|
|
410
|
+
* enrolled stay enrolled, and a job that cannot be finished is more
|
|
411
|
+
* useful marked finished with its real numbers than left as a
|
|
412
|
+
* permanently unfinished import a merchant is told to resume.
|
|
413
|
+
*/ await importRef.set({
|
|
414
|
+
status: 'complete',
|
|
415
|
+
cursor: total,
|
|
416
|
+
updatedAt: FieldValue.serverTimestamp()
|
|
417
|
+
}, {
|
|
418
|
+
merge: true
|
|
419
|
+
});
|
|
420
|
+
return res.status(200).json(_extends({}, finishedPayload(job, context), {
|
|
421
|
+
complete: true,
|
|
422
|
+
cursor: total
|
|
423
|
+
}));
|
|
424
|
+
}
|
|
425
|
+
const attested = job.get('attested') === true;
|
|
426
|
+
/*
|
|
427
|
+
* The ATTESTER's account, not the caller's. See the module note: a
|
|
428
|
+
* colleague who resumes somebody else's import has asserted nothing, and
|
|
429
|
+
* a consent record naming them would be a claim nobody made.
|
|
430
|
+
*/ const attestingUid = String((_job_get2 = job.get('attestedByUid')) != null ? _job_get2 : '');
|
|
431
|
+
const nowMs = Date.now();
|
|
432
|
+
const resolution = await resolveAddresses({
|
|
433
|
+
hostId: context.hostId,
|
|
434
|
+
inputs: staged.map((row)=>row.e)
|
|
435
|
+
});
|
|
436
|
+
const byEmail = new Map(staged.map((row)=>[
|
|
437
|
+
row.e,
|
|
438
|
+
row
|
|
439
|
+
]));
|
|
440
|
+
let enrolled = 0;
|
|
441
|
+
const refusals = {};
|
|
442
|
+
const results = [];
|
|
443
|
+
const refuse = (email, reason)=>{
|
|
444
|
+
var _refusals_reason;
|
|
445
|
+
refusals[reason] = ((_refusals_reason = refusals[reason]) != null ? _refusals_reason : 0) + 1;
|
|
446
|
+
results.push({
|
|
447
|
+
email,
|
|
448
|
+
enrolled: false,
|
|
449
|
+
reason,
|
|
450
|
+
error: ASSIGNMENT_REFUSAL_MESSAGES[reason]
|
|
451
|
+
});
|
|
452
|
+
};
|
|
453
|
+
for (const verdict of resolution.verdicts){
|
|
454
|
+
var _resolution_stored_get, _row_s, _row_d;
|
|
455
|
+
if (verdict.refusal || !verdict.email) {
|
|
456
|
+
var _verdict_refusal;
|
|
457
|
+
refuse(verdict.email, (_verdict_refusal = verdict.refusal) != null ? _verdict_refusal : 'unroutable-address');
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
const decision = assignmentBasis({
|
|
461
|
+
stored: (_resolution_stored_get = resolution.stored.get(verdict.email)) != null ? _resolution_stored_get : readMarketingBasis(null, resolution.group),
|
|
462
|
+
attested,
|
|
463
|
+
actingUid: attestingUid,
|
|
464
|
+
nowMs
|
|
465
|
+
});
|
|
466
|
+
if ('refusal' in decision) {
|
|
467
|
+
refuse(verdict.email, decision.refusal);
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
const row = byEmail.get(verdict.email);
|
|
471
|
+
const enrollment = await enrollListMember(_extends({
|
|
472
|
+
listRef: context.listRef,
|
|
473
|
+
group: resolution.group,
|
|
474
|
+
email: verdict.email
|
|
475
|
+
}, (row == null ? void 0 : row.n) ? {
|
|
476
|
+
name: row.n
|
|
477
|
+
} : {}, {
|
|
478
|
+
source: CONSOLE_IMPORT_SOURCE,
|
|
479
|
+
// Never `'rule'`: the dynamic-list materializer reconciles its own
|
|
480
|
+
// rows away when somebody stops matching, and a file a merchant
|
|
481
|
+
// uploaded is not a rule match that can lapse.
|
|
482
|
+
via: 'manual',
|
|
483
|
+
consent: _extends({}, decision, decision.basis === 'operator-attested' && row ? {
|
|
484
|
+
reason: importedBasisReason({
|
|
485
|
+
declaredSource: (_row_s = row.s) != null ? _row_s : '',
|
|
486
|
+
declaredAt: (_row_d = row.d) != null ? _row_d : ''
|
|
487
|
+
})
|
|
488
|
+
} : {})
|
|
489
|
+
}));
|
|
490
|
+
if (enrollment.enrolled === false) {
|
|
491
|
+
refuse(verdict.email, enrollment.refusal === 'declined' ? 'declined' : 'unroutable-address');
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
enrolled += 1;
|
|
495
|
+
results.push({
|
|
496
|
+
email: verdict.email,
|
|
497
|
+
enrolled: true
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
const nextCursor = cursor + staged.length;
|
|
501
|
+
const complete = nextCursor >= total;
|
|
502
|
+
await importRef.set({
|
|
503
|
+
cursor: nextCursor,
|
|
504
|
+
status: complete ? 'complete' : 'running',
|
|
505
|
+
enrolled: FieldValue.increment(enrolled),
|
|
506
|
+
refused: FieldValue.increment(staged.length - enrolled),
|
|
507
|
+
/*
|
|
508
|
+
* A NESTED map, not dotted keys. `set({merge:true})` reads its keys
|
|
509
|
+
* as literal field names — only `update()` expands a dot into a field
|
|
510
|
+
* path — so `refusals.declined` here would create a top-level field
|
|
511
|
+
* with a dot in its name and leave the map it was meant to update
|
|
512
|
+
* empty. A deep merge over a nested map does what is wanted and
|
|
513
|
+
* honors the increments inside it.
|
|
514
|
+
*/ refusals: Object.fromEntries(Object.entries(refusals).map(([reason, count])=>[
|
|
515
|
+
reason,
|
|
516
|
+
FieldValue.increment(count)
|
|
517
|
+
])),
|
|
518
|
+
updatedAt: FieldValue.serverTimestamp()
|
|
519
|
+
}, {
|
|
520
|
+
merge: true
|
|
521
|
+
});
|
|
522
|
+
return res.status(200).json({
|
|
523
|
+
importId,
|
|
524
|
+
listName: context.listName,
|
|
525
|
+
complete,
|
|
526
|
+
total,
|
|
527
|
+
cursor: nextCursor,
|
|
528
|
+
/*
|
|
529
|
+
* This RUN's numbers, named as this run's. The job's running totals are
|
|
530
|
+
* read back by `email/list-import-status`; reporting an increment as a
|
|
531
|
+
* total is how a progress readout comes to disagree with the record.
|
|
532
|
+
*/ ranEnrolled: enrolled,
|
|
533
|
+
ranRefused: staged.length - enrolled,
|
|
534
|
+
refusals,
|
|
535
|
+
results
|
|
536
|
+
});
|
|
537
|
+
} catch (error) {
|
|
538
|
+
console.error('[email] list import run failed', error);
|
|
539
|
+
return res.status(500).json({
|
|
540
|
+
error: 'The import could not continue.'
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
/** The payload for a job that has nothing left to do. */ function finishedPayload(job, context) {
|
|
545
|
+
var _job_get, _job_get1;
|
|
546
|
+
return {
|
|
547
|
+
importId: job.id,
|
|
548
|
+
listName: context.listName,
|
|
549
|
+
complete: true,
|
|
550
|
+
total: Number((_job_get = job.get('total')) != null ? _job_get : 0),
|
|
551
|
+
cursor: Number((_job_get1 = job.get('cursor')) != null ? _job_get1 : 0),
|
|
552
|
+
ranEnrolled: 0,
|
|
553
|
+
ranRefused: 0,
|
|
554
|
+
refusals: {},
|
|
555
|
+
results: []
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* `POST email/list-import-status` — the import on this list, if there is one.
|
|
560
|
+
*
|
|
561
|
+
* Reached when the import drawer opens, and at no other time. It exists
|
|
562
|
+
* because a browser is not a durable thing: a merchant who closed the tab
|
|
563
|
+
* during a large import has an audience that is part-way filled and, without
|
|
564
|
+
* this, no way to see that or to finish it. What they must never be offered
|
|
565
|
+
* instead is a fresh import of the same file, which would re-run the whole
|
|
566
|
+
* gate over addresses already enrolled.
|
|
567
|
+
*
|
|
568
|
+
* Ordered on `createdAt`, which every job document written by
|
|
569
|
+
* `email/list-import-start` carries — a `limit()` with no `orderBy` answers in
|
|
570
|
+
* document-id order, and the ids come from `createResourceUid()`, so the
|
|
571
|
+
* "latest" import would be an arbitrary one.
|
|
572
|
+
*/ export const emailListImportStatusHandler = async (req, res)=>{
|
|
573
|
+
if (req.method !== 'POST') {
|
|
574
|
+
return res.status(405).json({
|
|
575
|
+
error: 'Method not allowed'
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
try {
|
|
579
|
+
var _job_get, _job_get1, _job_get2, _job_get3, _job_get4, _job_get5, _job_get6, _job_get7, _job_get8, _job_get9;
|
|
580
|
+
const context = await resolveListContext(req);
|
|
581
|
+
if (context.ok === false) {
|
|
582
|
+
return res.status(context.status).json(context.body);
|
|
583
|
+
}
|
|
584
|
+
const snapshot = await context.listRef.collection(LIST_IMPORTS_SUBCOLLECTION).orderBy('createdAt', 'desc').limit(1).get();
|
|
585
|
+
const job = snapshot.docs[0];
|
|
586
|
+
if (!job) return res.status(200).json({
|
|
587
|
+
listName: context.listName,
|
|
588
|
+
job: null
|
|
589
|
+
});
|
|
590
|
+
return res.status(200).json({
|
|
591
|
+
listName: context.listName,
|
|
592
|
+
job: {
|
|
593
|
+
importId: job.id,
|
|
594
|
+
status: String((_job_get = job.get('status')) != null ? _job_get : 'running'),
|
|
595
|
+
total: Number((_job_get1 = job.get('total')) != null ? _job_get1 : 0),
|
|
596
|
+
cursor: Number((_job_get2 = job.get('cursor')) != null ? _job_get2 : 0),
|
|
597
|
+
enrolled: Number((_job_get3 = job.get('enrolled')) != null ? _job_get3 : 0),
|
|
598
|
+
refused: Number((_job_get4 = job.get('refused')) != null ? _job_get4 : 0),
|
|
599
|
+
refusals: (_job_get5 = job.get('refusals')) != null ? _job_get5 : {},
|
|
600
|
+
attested: job.get('attested') === true,
|
|
601
|
+
unusable: Number((_job_get6 = job.get('unusable')) != null ? _job_get6 : 0),
|
|
602
|
+
duplicates: Number((_job_get7 = job.get('duplicates')) != null ? _job_get7 : 0),
|
|
603
|
+
unusableSamples: (_job_get8 = job.get('unusableSamples')) != null ? _job_get8 : [],
|
|
604
|
+
screening: (_job_get9 = job.get('screening')) != null ? _job_get9 : null
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
} catch (error) {
|
|
608
|
+
console.error('[email] list import status failed', error);
|
|
609
|
+
return res.status(500).json({
|
|
610
|
+
error: 'The import could not be looked up.'
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
/**
|
|
615
|
+
* Import route registration.
|
|
616
|
+
*
|
|
617
|
+
* Reached by a person pressing a button in a browser, like the rest of the
|
|
618
|
+
* console half, so none of these is on the machine-path exemption list in
|
|
619
|
+
* `plugin-api-rate-limit.ts` — with one consequence worth stating: the RUN
|
|
620
|
+
* route is called repeatedly by design, once per {@link
|
|
621
|
+
* LIST_IMPORT_RUN_BUDGET} addresses, so the visitor limiter's per-(site, IP)
|
|
622
|
+
* budget is the ceiling on how fast a large import can proceed. That is the
|
|
623
|
+
* correct ceiling for a path that enrolls people into a marketing audience,
|
|
624
|
+
* and it degrades into a slower import rather than a failed one.
|
|
625
|
+
*/ export function registerEmailListImportApi() {
|
|
626
|
+
registerPluginApiRoute('email/list-import-preview', emailListImportPreviewHandler);
|
|
627
|
+
registerPluginApiRoute('email/list-import-start', emailListImportStartHandler);
|
|
628
|
+
registerPluginApiRoute('email/list-import-run', emailListImportRunHandler);
|
|
629
|
+
registerPluginApiRoute('email/list-import-status', emailListImportStatusHandler);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
//# sourceMappingURL=server-list-import.js.map
|