@aglyn/tenant-data-admin 1.0.0-beta.289 → 1.0.0-beta.291
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 +7 -7
- package/src/lib/server/account-acquisition.js +19 -3
- package/src/lib/server/account-acquisition.js.map +1 -1
- package/src/lib/server/email-suppression.d.ts +40 -0
- package/src/lib/server/email-suppression.js +47 -0
- package/src/lib/server/email-suppression.js.map +1 -1
- package/src/lib/server/platform-marketing-consent.d.ts +33 -0
- package/src/lib/server/platform-marketing-consent.js +58 -0
- package/src/lib/server/platform-marketing-consent.js.map +1 -1
- package/src/lib/server/platform-served-domain.d.ts +60 -0
- package/src/lib/server/platform-served-domain.js +146 -0
- package/src/lib/server/platform-served-domain.js.map +1 -0
- package/src/lib/server/visitor-upload.d.ts +169 -0
- package/src/lib/server/visitor-upload.js +366 -0
- package/src/lib/server/visitor-upload.js.map +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aglyn/tenant-data-admin",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.291",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"homepage": "https://aglyn.com",
|
|
6
6
|
"repository": {
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"./package.json": "./package.json"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@aglyn/aglyn": "1.0.0-beta.
|
|
29
|
-
"@aglyn/shared-util-email": "1.0.0-beta.
|
|
30
|
-
"@aglyn/shared-util-fbserver": "1.0.0-beta.
|
|
31
|
-
"@aglyn/shared-util-first-touch": "1.0.0-beta.
|
|
32
|
-
"@aglyn/shared-util-http": "1.0.0-beta.
|
|
33
|
-
"@aglyn/shared-util-tools": "1.0.0-beta.
|
|
28
|
+
"@aglyn/aglyn": "1.0.0-beta.291",
|
|
29
|
+
"@aglyn/shared-util-email": "1.0.0-beta.291",
|
|
30
|
+
"@aglyn/shared-util-fbserver": "1.0.0-beta.291",
|
|
31
|
+
"@aglyn/shared-util-first-touch": "1.0.0-beta.291",
|
|
32
|
+
"@aglyn/shared-util-http": "1.0.0-beta.291",
|
|
33
|
+
"@aglyn/shared-util-tools": "1.0.0-beta.291",
|
|
34
34
|
"@msgpack/msgpack": "^3.1.3",
|
|
35
35
|
"@swc/helpers": "0.5.23",
|
|
36
36
|
"sharp": "^0.35.5",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import { readRequestCity, readRequestGeo, readRequestRegionLabel } from "@aglyn/aglyn/app-utils/request-geo";
|
|
18
18
|
import { findUserByUidAcrossPools } from "./auth-pools.js";
|
|
19
19
|
import firebaseAdmin from "./firebase-admin.js";
|
|
20
|
+
import { recordPlatformAccountContact } from "./platform-marketing-consent.js";
|
|
20
21
|
/**
|
|
21
22
|
* Write where an account came from, ONCE, at account creation (AGL-3289).
|
|
22
23
|
*
|
|
@@ -67,7 +68,8 @@ import firebaseAdmin from "./firebase-admin.js";
|
|
|
67
68
|
var _input_firestore, _input_nowMs, _input_invitedToOrgId;
|
|
68
69
|
const firestore = (_input_firestore = input.firestore) != null ? _input_firestore : firebaseAdmin.app().firestore();
|
|
69
70
|
const nowMs = (_input_nowMs = input.nowMs) != null ? _input_nowMs : Date.now();
|
|
70
|
-
|
|
71
|
+
const createdAtMs = input.accountCreatedAtMs;
|
|
72
|
+
if (createdAtMs === null || !Number.isFinite(createdAtMs) || nowMs - createdAtMs > ACCOUNT_ACQUISITION_WINDOW_MS) {
|
|
71
73
|
return {
|
|
72
74
|
status: 'not-new'
|
|
73
75
|
};
|
|
@@ -79,12 +81,12 @@ import firebaseAdmin from "./firebase-admin.js";
|
|
|
79
81
|
provider: input.provider,
|
|
80
82
|
geo: requestGeography(input.headers),
|
|
81
83
|
invitedToOrgId,
|
|
82
|
-
accountCreatedAtMs:
|
|
84
|
+
accountCreatedAtMs: createdAtMs,
|
|
83
85
|
recordedBy: input.recordedBy,
|
|
84
86
|
nowMs
|
|
85
87
|
});
|
|
86
88
|
const userRef = firestore.collection('users').doc(input.uid);
|
|
87
|
-
|
|
89
|
+
const result = await firestore.runTransaction(async (tx)=>{
|
|
88
90
|
const snapshot = await tx.get(userRef);
|
|
89
91
|
const existing = snapshot.exists ? snapshot.get(ACCOUNT_ACQUISITION_FIELD) : undefined;
|
|
90
92
|
if (existing !== undefined && existing !== null) {
|
|
@@ -103,6 +105,20 @@ import firebaseAdmin from "./firebase-admin.js";
|
|
|
103
105
|
record
|
|
104
106
|
};
|
|
105
107
|
});
|
|
108
|
+
// Every door that creates an account passes here exactly once, so this is
|
|
109
|
+
// where the account becomes a contact in the operator's own CRM, whatever
|
|
110
|
+
// it said about marketing email. A failure there never fails the sign-up.
|
|
111
|
+
if (result.status === 'recorded') {
|
|
112
|
+
await recordPlatformAccountContact({
|
|
113
|
+
uid: input.uid,
|
|
114
|
+
email: input.email,
|
|
115
|
+
createdAtMs,
|
|
116
|
+
firestore
|
|
117
|
+
}).catch((error)=>{
|
|
118
|
+
console.error('[account-acquisition] platform contact failed', error);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
106
122
|
}
|
|
107
123
|
/**
|
|
108
124
|
* Record a self-serve sign-up's acquisition, deciding from the verified
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../libs/tenant/data/admin/src/lib/server/account-acquisition.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\nimport {\n ACCOUNT_ACQUISITION_FIELD,\n buildAccountAcquisition,\n readAccountAcquisition,\n signUpDoorFor,\n signUpProviderFor,\n type AccountAcquisition,\n type AcquisitionDoor,\n} from '@aglyn/aglyn/app-utils/account-acquisition'\nimport {\n readRequestCity,\n readRequestGeo,\n readRequestRegionLabel,\n type HeaderReader,\n} from '@aglyn/aglyn/app-utils/request-geo'\nimport { findUserByUidAcrossPools } from './auth-pools'\nimport firebaseAdmin from './firebase-admin'\n\n/**\n * Write where an account came from, ONCE, at account creation (AGL-3289).\n *\n * The capture kept the first touch on the visitor's device; the door that\n * created the account hands it here, with what only the server knows beside\n * it: the door, the sign-in provider from the verified token, where the\n * request came from as the edge reported it, and whether the address had an\n * invitation waiting. The record goes on `users/{uid}` inside a transaction\n * that writes nothing when one is already there — the first write wins, and\n * a retry, a second tab or a later call can never restate it.\n *\n * The rules deny the field to every client, so this Admin SDK write is the\n * only way in. `createOrganization` copies it onto the workspace the account\n * creates.\n */\n\n/**\n * How long after an account is created its door may still record where it\n * came from. A door calls within seconds of creating the account; a mobile\n * redirect or a slow verification page within minutes. Anything later is not\n * account creation, and a record written then would describe a later visit.\n */\nexport const ACCOUNT_ACQUISITION_WINDOW_MS = 30 * 60 * 1000\n\n/** What a door knows when it records an account. */\nexport interface RecordAccountAcquisitionInput {\n uid: string\n /** When the auth record says the account was created; null refuses. */\n accountCreatedAtMs: number | null\n /** The first touch the device kept — untrusted. */\n touch: unknown\n door: AcquisitionDoor\n /** From the verified token's `sign_in_provider`. */\n provider: string | null\n /** The account's address, for the invitation check; null when it has none. */\n email: string | null\n /** The organization an invitation the door already found came from. */\n invitedToOrgId?: string | null\n /** The account-creating request's headers, for its geography. */\n headers: HeaderReader\n recordedBy: 'signup' | 'sso'\n nowMs?: number\n firestore?: FirebaseFirestore.Firestore\n}\n\nexport type RecordAccountAcquisitionResult =\n | { status: 'recorded'; record: AccountAcquisition }\n | { status: 'exists'; record: AccountAcquisition | null }\n /** The account was not created just now, so this is not its creation. */\n | { status: 'not-new' }\n\n/**\n * The organization whose invitation this address has waiting, or null. The\n * same query the console's invitation banner runs, one address wide.\n */\nexport async function pendingInvitationOrgId(\n email: string | null,\n firestore: FirebaseFirestore.Firestore = firebaseAdmin.app().firestore(),\n): Promise<string | null> {\n const address = String(email ?? '').trim().toLowerCase()\n if (!address) return null\n try {\n const snapshot = await firestore\n .collectionGroup('invites')\n .where('email', 'in', [address])\n .where('acceptedAt', '==', null)\n .limit(1)\n .get()\n return snapshot.docs[0]?.ref.parent.parent?.id ?? null\n } catch (error) {\n // An unanswerable question costs the door's label, never the record.\n console.error('[account-acquisition] invitation lookup failed', error)\n return null\n }\n}\n\n/** Where the request came from, as a person reads it — no address, no coordinates. */\nexport function requestGeography(headers: HeaderReader): {\n country: string | null\n region: string | null\n city: string | null\n} {\n return {\n country: readRequestGeo(headers).country,\n region: readRequestRegionLabel(headers),\n city: readRequestCity(headers),\n }\n}\n\n/** Record the account's acquisition unless it already has one. */\nexport async function recordAccountAcquisition(\n input: RecordAccountAcquisitionInput,\n): Promise<RecordAccountAcquisitionResult> {\n const firestore = input.firestore ?? firebaseAdmin.app().firestore()\n const nowMs = input.nowMs ?? Date.now()\n if (\n input.accountCreatedAtMs === null ||\n !Number.isFinite(input.accountCreatedAtMs) ||\n nowMs - input.accountCreatedAtMs > ACCOUNT_ACQUISITION_WINDOW_MS\n ) {\n return { status: 'not-new' }\n }\n const invitedToOrgId =\n input.invitedToOrgId ??\n (input.door === 'signup-password' ||\n input.door === 'signup-google' ||\n input.door === 'signup-google-redirect'\n ? await pendingInvitationOrgId(input.email, firestore)\n : null)\n const record = buildAccountAcquisition({\n touch: input.touch,\n door: invitedToOrgId && input.door !== 'sso' ? 'invite' : input.door,\n provider: input.provider,\n geo: requestGeography(input.headers),\n invitedToOrgId,\n accountCreatedAtMs: input.accountCreatedAtMs,\n recordedBy: input.recordedBy,\n nowMs,\n })\n const userRef = firestore.collection('users').doc(input.uid)\n return firestore.runTransaction(async (tx) => {\n const snapshot = await tx.get(userRef)\n const existing = snapshot.exists ? snapshot.get(ACCOUNT_ACQUISITION_FIELD) : undefined\n if (existing !== undefined && existing !== null) {\n return { status: 'exists', record: readAccountAcquisition(existing) } as const\n }\n tx.set(userRef, { [ACCOUNT_ACQUISITION_FIELD]: record }, { merge: true })\n return { status: 'recorded', record } as const\n })\n}\n\n/** What a sign-up door knows about the account it is recording. */\nexport interface RecordSignUpAcquisitionInput {\n uid: string\n /** The verified token's `sign_in_provider`. */\n provider: string | null\n /** The verified token's address. */\n email: string | null\n /** The first touch the page sent, else the request's cookie — untrusted. */\n touch: unknown\n /** Which of the provider's doors the page says it was; see `signUpDoorFor`. */\n doorHint?: unknown\n headers: HeaderReader\n nowMs?: number\n firestore?: FirebaseFirestore.Firestore\n}\n\n/**\n * Record a self-serve sign-up's acquisition, deciding from the verified\n * provider and the auth record whether there is one to record.\n *\n * Shared by the two moments a sign-up passes through: the sign-up page's own\n * call, right after the account exists, and workspace creation, which is the\n * backstop for a page torn down before its call went out — the workspace\n * copies its creator's record at birth, so that is the last moment one can\n * still reach it. Both write through {@link recordAccountAcquisition}, so the\n * first one wins and neither can restate the other; an account older than\n * the window, or one a sign-up door did not create, writes nothing.\n *\n * `null` when the provider is not a sign-up door (SSO records its own).\n */\nexport async function recordSignUpAcquisition(\n input: RecordSignUpAcquisitionInput,\n): Promise<RecordAccountAcquisitionResult | null> {\n // A `custom` token can name no door until the auth record says which\n // provider made the account (AGL-3706), so the lookup comes first for it.\n const custom = input.provider === 'custom'\n if (!custom && !signUpDoorFor(input.provider, input.doorHint)) return null\n // Across every pool, not the project's alone (AGL-1122): a lookup that\n // cannot see an account answers \"not new\", which writes nothing.\n const pooled = await findUserByUidAcrossPools(input.uid)\n const provider = signUpProviderFor(\n input.provider,\n (pooled?.record.providerData ?? []).map((info) => info.providerId),\n )\n const door = signUpDoorFor(provider, input.doorHint)\n if (!door) return null\n const createdAtMs = Date.parse(pooled?.record.metadata.creationTime ?? '')\n return recordAccountAcquisition({\n uid: input.uid,\n accountCreatedAtMs: Number.isFinite(createdAtMs) ? createdAtMs : null,\n touch: input.touch,\n door,\n provider,\n email: input.email,\n headers: input.headers,\n recordedBy: 'signup',\n nowMs: input.nowMs,\n firestore: input.firestore,\n })\n}\n"],"names":["ACCOUNT_ACQUISITION_FIELD","buildAccountAcquisition","readAccountAcquisition","signUpDoorFor","signUpProviderFor","readRequestCity","readRequestGeo","readRequestRegionLabel","findUserByUidAcrossPools","firebaseAdmin","ACCOUNT_ACQUISITION_WINDOW_MS","pendingInvitationOrgId","email","firestore","app","address","String","trim","toLowerCase","snapshot","collectionGroup","where","limit","get","docs","ref","parent","id","error","console","requestGeography","headers","country","region","city","recordAccountAcquisition","input","nowMs","Date","now","accountCreatedAtMs","Number","isFinite","status","invitedToOrgId","door","record","touch","provider","geo","recordedBy","userRef","collection","doc","uid","runTransaction","tx","existing","exists","undefined","set","merge","recordSignUpAcquisition","custom","doorHint","pooled","providerData","map","info","providerId","createdAtMs","parse","metadata","creationTime"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SACEA,yBAAyB,EACzBC,uBAAuB,EACvBC,sBAAsB,EACtBC,aAAa,EACbC,iBAAiB,QAGZ,6CAA4C;AACnD,SACEC,eAAe,EACfC,cAAc,EACdC,sBAAsB,QAEjB,qCAAoC;AAC3C,SAASC,wBAAwB,QAAQ,kBAAc;AACvD,OAAOC,mBAAmB,sBAAkB;AAE5C;;;;;;;;;;;;;;CAcC,GAED;;;;;CAKC,GACD,OAAO,MAAMC,gCAAgC,KAAK,KAAK,KAAI;AA6B3D;;;CAGC,GACD,OAAO,eAAeC,uBACpBC,KAAoB,EACpBC,YAAyCJ,cAAcK,GAAG,GAAGD,SAAS,EAAE;IAExE,MAAME,UAAUC,OAAOJ,gBAAAA,QAAS,IAAIK,IAAI,GAAGC,WAAW;IACtD,IAAI,CAACH,SAAS,OAAO;IACrB,IAAI;;YAOKI,mCAAAA;QANP,MAAMA,WAAW,MAAMN,UACpBO,eAAe,C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,0BAAAA,OAAQnB,MAAM,CAAC0B,QAAQ,CAACC,YAAY,oBAAI;IACvE,OAAOtC,yBAAyB;QAC9BmB,KAAKlB,MAAMkB,GAAG;QACdd,oBAAoBC,OAAOC,QAAQ,CAAC4B,eAAeA,cAAc;QACjEvB,OAAOX,MAAMW,KAAK;QAClBF;QACAG;QACApC,OAAOwB,MAAMxB,KAAK;QAClBmB,SAASK,MAAML,OAAO;QACtBmB,YAAY;QACZb,OAAOD,MAAMC,KAAK;QAClBxB,WAAWuB,MAAMvB,SAAS;IAC5B;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../libs/tenant/data/admin/src/lib/server/account-acquisition.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\nimport {\n ACCOUNT_ACQUISITION_FIELD,\n buildAccountAcquisition,\n readAccountAcquisition,\n signUpDoorFor,\n signUpProviderFor,\n type AccountAcquisition,\n type AcquisitionDoor,\n} from '@aglyn/aglyn/app-utils/account-acquisition'\nimport {\n readRequestCity,\n readRequestGeo,\n readRequestRegionLabel,\n type HeaderReader,\n} from '@aglyn/aglyn/app-utils/request-geo'\nimport { findUserByUidAcrossPools } from './auth-pools'\nimport firebaseAdmin from './firebase-admin'\nimport { recordPlatformAccountContact } from './platform-marketing-consent'\n\n/**\n * Write where an account came from, ONCE, at account creation (AGL-3289).\n *\n * The capture kept the first touch on the visitor's device; the door that\n * created the account hands it here, with what only the server knows beside\n * it: the door, the sign-in provider from the verified token, where the\n * request came from as the edge reported it, and whether the address had an\n * invitation waiting. The record goes on `users/{uid}` inside a transaction\n * that writes nothing when one is already there — the first write wins, and\n * a retry, a second tab or a later call can never restate it.\n *\n * The rules deny the field to every client, so this Admin SDK write is the\n * only way in. `createOrganization` copies it onto the workspace the account\n * creates.\n */\n\n/**\n * How long after an account is created its door may still record where it\n * came from. A door calls within seconds of creating the account; a mobile\n * redirect or a slow verification page within minutes. Anything later is not\n * account creation, and a record written then would describe a later visit.\n */\nexport const ACCOUNT_ACQUISITION_WINDOW_MS = 30 * 60 * 1000\n\n/** What a door knows when it records an account. */\nexport interface RecordAccountAcquisitionInput {\n uid: string\n /** When the auth record says the account was created; null refuses. */\n accountCreatedAtMs: number | null\n /** The first touch the device kept — untrusted. */\n touch: unknown\n door: AcquisitionDoor\n /** From the verified token's `sign_in_provider`. */\n provider: string | null\n /** The account's address, for the invitation check; null when it has none. */\n email: string | null\n /** The organization an invitation the door already found came from. */\n invitedToOrgId?: string | null\n /** The account-creating request's headers, for its geography. */\n headers: HeaderReader\n recordedBy: 'signup' | 'sso'\n nowMs?: number\n firestore?: FirebaseFirestore.Firestore\n}\n\nexport type RecordAccountAcquisitionResult =\n | { status: 'recorded'; record: AccountAcquisition }\n | { status: 'exists'; record: AccountAcquisition | null }\n /** The account was not created just now, so this is not its creation. */\n | { status: 'not-new' }\n\n/**\n * The organization whose invitation this address has waiting, or null. The\n * same query the console's invitation banner runs, one address wide.\n */\nexport async function pendingInvitationOrgId(\n email: string | null,\n firestore: FirebaseFirestore.Firestore = firebaseAdmin.app().firestore(),\n): Promise<string | null> {\n const address = String(email ?? '').trim().toLowerCase()\n if (!address) return null\n try {\n const snapshot = await firestore\n .collectionGroup('invites')\n .where('email', 'in', [address])\n .where('acceptedAt', '==', null)\n .limit(1)\n .get()\n return snapshot.docs[0]?.ref.parent.parent?.id ?? null\n } catch (error) {\n // An unanswerable question costs the door's label, never the record.\n console.error('[account-acquisition] invitation lookup failed', error)\n return null\n }\n}\n\n/** Where the request came from, as a person reads it — no address, no coordinates. */\nexport function requestGeography(headers: HeaderReader): {\n country: string | null\n region: string | null\n city: string | null\n} {\n return {\n country: readRequestGeo(headers).country,\n region: readRequestRegionLabel(headers),\n city: readRequestCity(headers),\n }\n}\n\n/** Record the account's acquisition unless it already has one. */\nexport async function recordAccountAcquisition(\n input: RecordAccountAcquisitionInput,\n): Promise<RecordAccountAcquisitionResult> {\n const firestore = input.firestore ?? firebaseAdmin.app().firestore()\n const nowMs = input.nowMs ?? Date.now()\n const createdAtMs = input.accountCreatedAtMs\n if (\n createdAtMs === null ||\n !Number.isFinite(createdAtMs) ||\n nowMs - createdAtMs > ACCOUNT_ACQUISITION_WINDOW_MS\n ) {\n return { status: 'not-new' }\n }\n const invitedToOrgId =\n input.invitedToOrgId ??\n (input.door === 'signup-password' ||\n input.door === 'signup-google' ||\n input.door === 'signup-google-redirect'\n ? await pendingInvitationOrgId(input.email, firestore)\n : null)\n const record = buildAccountAcquisition({\n touch: input.touch,\n door: invitedToOrgId && input.door !== 'sso' ? 'invite' : input.door,\n provider: input.provider,\n geo: requestGeography(input.headers),\n invitedToOrgId,\n accountCreatedAtMs: createdAtMs,\n recordedBy: input.recordedBy,\n nowMs,\n })\n const userRef = firestore.collection('users').doc(input.uid)\n const result = await firestore.runTransaction(async (tx) => {\n const snapshot = await tx.get(userRef)\n const existing = snapshot.exists ? snapshot.get(ACCOUNT_ACQUISITION_FIELD) : undefined\n if (existing !== undefined && existing !== null) {\n return { status: 'exists', record: readAccountAcquisition(existing) } as const\n }\n tx.set(userRef, { [ACCOUNT_ACQUISITION_FIELD]: record }, { merge: true })\n return { status: 'recorded', record } as const\n })\n // Every door that creates an account passes here exactly once, so this is\n // where the account becomes a contact in the operator's own CRM, whatever\n // it said about marketing email. A failure there never fails the sign-up.\n if (result.status === 'recorded') {\n await recordPlatformAccountContact({\n uid: input.uid,\n email: input.email,\n createdAtMs,\n firestore,\n }).catch((error) => {\n console.error('[account-acquisition] platform contact failed', error)\n })\n }\n return result\n}\n\n/** What a sign-up door knows about the account it is recording. */\nexport interface RecordSignUpAcquisitionInput {\n uid: string\n /** The verified token's `sign_in_provider`. */\n provider: string | null\n /** The verified token's address. */\n email: string | null\n /** The first touch the page sent, else the request's cookie — untrusted. */\n touch: unknown\n /** Which of the provider's doors the page says it was; see `signUpDoorFor`. */\n doorHint?: unknown\n headers: HeaderReader\n nowMs?: number\n firestore?: FirebaseFirestore.Firestore\n}\n\n/**\n * Record a self-serve sign-up's acquisition, deciding from the verified\n * provider and the auth record whether there is one to record.\n *\n * Shared by the two moments a sign-up passes through: the sign-up page's own\n * call, right after the account exists, and workspace creation, which is the\n * backstop for a page torn down before its call went out — the workspace\n * copies its creator's record at birth, so that is the last moment one can\n * still reach it. Both write through {@link recordAccountAcquisition}, so the\n * first one wins and neither can restate the other; an account older than\n * the window, or one a sign-up door did not create, writes nothing.\n *\n * `null` when the provider is not a sign-up door (SSO records its own).\n */\nexport async function recordSignUpAcquisition(\n input: RecordSignUpAcquisitionInput,\n): Promise<RecordAccountAcquisitionResult | null> {\n // A `custom` token can name no door until the auth record says which\n // provider made the account (AGL-3706), so the lookup comes first for it.\n const custom = input.provider === 'custom'\n if (!custom && !signUpDoorFor(input.provider, input.doorHint)) return null\n // Across every pool, not the project's alone (AGL-1122): a lookup that\n // cannot see an account answers \"not new\", which writes nothing.\n const pooled = await findUserByUidAcrossPools(input.uid)\n const provider = signUpProviderFor(\n input.provider,\n (pooled?.record.providerData ?? []).map((info) => info.providerId),\n )\n const door = signUpDoorFor(provider, input.doorHint)\n if (!door) return null\n const createdAtMs = Date.parse(pooled?.record.metadata.creationTime ?? '')\n return recordAccountAcquisition({\n uid: input.uid,\n accountCreatedAtMs: Number.isFinite(createdAtMs) ? createdAtMs : null,\n touch: input.touch,\n door,\n provider,\n email: input.email,\n headers: input.headers,\n recordedBy: 'signup',\n nowMs: input.nowMs,\n firestore: input.firestore,\n })\n}\n"],"names":["ACCOUNT_ACQUISITION_FIELD","buildAccountAcquisition","readAccountAcquisition","signUpDoorFor","signUpProviderFor","readRequestCity","readRequestGeo","readRequestRegionLabel","findUserByUidAcrossPools","firebaseAdmin","recordPlatformAccountContact","ACCOUNT_ACQUISITION_WINDOW_MS","pendingInvitationOrgId","email","firestore","app","address","String","trim","toLowerCase","snapshot","collectionGroup","where","limit","get","docs","ref","parent","id","error","console","requestGeography","headers","country","region","city","recordAccountAcquisition","input","nowMs","Date","now","createdAtMs","accountCreatedAtMs","Number","isFinite","status","invitedToOrgId","door","record","touch","provider","geo","recordedBy","userRef","collection","doc","uid","result","runTransaction","tx","existing","exists","undefined","set","merge","catch","recordSignUpAcquisition","custom","doorHint","pooled","providerData","map","info","providerId","parse","metadata","creationTime"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SACEA,yBAAyB,EACzBC,uBAAuB,EACvBC,sBAAsB,EACtBC,aAAa,EACbC,iBAAiB,QAGZ,6CAA4C;AACnD,SACEC,eAAe,EACfC,cAAc,EACdC,sBAAsB,QAEjB,qCAAoC;AAC3C,SAASC,wBAAwB,QAAQ,kBAAc;AACvD,OAAOC,mBAAmB,sBAAkB;AAC5C,SAASC,4BAA4B,QAAQ,kCAA8B;AAE3E;;;;;;;;;;;;;;CAcC,GAED;;;;;CAKC,GACD,OAAO,MAAMC,gCAAgC,KAAK,KAAK,KAAI;AA6B3D;;;CAGC,GACD,OAAO,eAAeC,uBACpBC,KAAoB,EACpBC,YAAyCL,cAAcM,GAAG,GAAGD,SAAS,EAAE;IAExE,MAAME,UAAUC,OAAOJ,gBAAAA,QAAS,IAAIK,IAAI,GAAGC,WAAW;IACtD,IAAI,CAACH,SAAS,OAAO;IACrB,IAAI;;YAOKI,mCAAAA;QANP,MAAMA,WAAW,MAAMN,UACpBO,eAAe,CAAC,WAChBC,KAAK,CAAC,SAAS,MAAM;YAACN;SAAQ,EAC9BM,KAAK,CAAC,cAAc,MAAM,MAC1BC,KAAK,CAAC,GACNC,GAAG;QACN,gBAAOJ,kBAAAA,SAASK,IAAI,CAAC,EAAE,sBAAhBL,oCAAAA,gB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B,QAAQ;IACnD,IAAI,CAACrB,MAAM,OAAO;IAClB,MAAMN,cAAcF,KAAKmC,KAAK,UAACL,0BAAAA,OAAQrB,MAAM,CAAC2B,QAAQ,CAACC,YAAY,oBAAI;IACvE,OAAOxC,yBAAyB;QAC9BoB,KAAKnB,MAAMmB,GAAG;QACdd,oBAAoBC,OAAOC,QAAQ,CAACH,eAAeA,cAAc;QACjEQ,OAAOZ,MAAMY,KAAK;QAClBF;QACAG;QACArC,OAAOwB,MAAMxB,KAAK;QAClBmB,SAASK,MAAML,OAAO;QACtBoB,YAAY;QACZd,OAAOD,MAAMC,KAAK;QAClBxB,WAAWuB,MAAMvB,SAAS;IAC5B;AACF"}
|
|
@@ -437,6 +437,46 @@ export declare function releaseEmailForConfirmedOptIn(input: {
|
|
|
437
437
|
* here by returning false.
|
|
438
438
|
*/
|
|
439
439
|
export declare function isEmailSuppressed(email: string | null | undefined, injectedFirestore?: any): Promise<boolean>;
|
|
440
|
+
/**
|
|
441
|
+
* Whether a site may write to one address, person to person, about something
|
|
442
|
+
* the site starts (AGL-4097): `suppressed-platform` when it bounced or
|
|
443
|
+
* reported mail as spam anywhere, `suppressed-host` when it unsubscribed from
|
|
444
|
+
* this site or any site of its consent group, else null. Both lists, the same
|
|
445
|
+
* two the inbox's reply reads (`addressSuppression` in the inbox plugin).
|
|
446
|
+
*
|
|
447
|
+
* NOT for mail the person asked for. A quote they requested, the confirmation
|
|
448
|
+
* of a form they just sent, a receipt: those ask
|
|
449
|
+
* {@link transactionalAddressSuppression}, because unsubscribing from a
|
|
450
|
+
* site's newsletter must never stop the site answering a request (AGL-4098).
|
|
451
|
+
*
|
|
452
|
+
* Fails closed like {@link isEmailSuppressed}: an address it cannot key, or a
|
|
453
|
+
* platform list it cannot read, is `suppressed-platform`.
|
|
454
|
+
*/
|
|
455
|
+
export declare function siteAddressSuppression(hostId: string, email: string, options?: {
|
|
456
|
+
firestore?: any;
|
|
457
|
+
/** The site's consent group, whose every site's opt-outs answer; absent reads the site alone. */
|
|
458
|
+
group?: Pick<ConsentGroup, 'hostId' | 'hostIds'> | null;
|
|
459
|
+
}): Promise<'suppressed-platform' | 'suppressed-host' | null>;
|
|
460
|
+
/**
|
|
461
|
+
* Whether a site may send one address something THEY ASKED FOR (AGL-4098):
|
|
462
|
+
* a quote they requested, the confirmation of a form they just sent.
|
|
463
|
+
* `suppressed-platform` when the address bounced for good or reported mail as
|
|
464
|
+
* spam (the platform list, {@link isEmailSuppressed}), else null.
|
|
465
|
+
*
|
|
466
|
+
* The site's own list is deliberately NOT read. It holds marketing
|
|
467
|
+
* unsubscribes, and an unsubscribe is a preference about a site's newsletter,
|
|
468
|
+
* not a request to stop being answered — the reasoning the member password
|
|
469
|
+
* reset already follows (`membership-recover.ts` in commerce), and the reason
|
|
470
|
+
* an order receipt is never refused for one. A bounce or a complaint is a fact
|
|
471
|
+
* about the mailbox and the sending domain every site shares, so it still
|
|
472
|
+
* refuses.
|
|
473
|
+
*
|
|
474
|
+
* Fails closed like {@link isEmailSuppressed}: an address it cannot key, or a
|
|
475
|
+
* list it cannot read, is `suppressed-platform`.
|
|
476
|
+
*/
|
|
477
|
+
export declare function transactionalAddressSuppression(email: string, options?: {
|
|
478
|
+
firestore?: any;
|
|
479
|
+
}): Promise<'suppressed-platform' | null>;
|
|
440
480
|
/**
|
|
441
481
|
* The sendable subset of a recipient list, for the bulk senders.
|
|
442
482
|
*
|
|
@@ -528,6 +528,53 @@ async function accountBanKeys(injectedFirestore) {
|
|
|
528
528
|
return true;
|
|
529
529
|
}
|
|
530
530
|
}
|
|
531
|
+
/**
|
|
532
|
+
* Whether a site may write to one address, person to person, about something
|
|
533
|
+
* the site starts (AGL-4097): `suppressed-platform` when it bounced or
|
|
534
|
+
* reported mail as spam anywhere, `suppressed-host` when it unsubscribed from
|
|
535
|
+
* this site or any site of its consent group, else null. Both lists, the same
|
|
536
|
+
* two the inbox's reply reads (`addressSuppression` in the inbox plugin).
|
|
537
|
+
*
|
|
538
|
+
* NOT for mail the person asked for. A quote they requested, the confirmation
|
|
539
|
+
* of a form they just sent, a receipt: those ask
|
|
540
|
+
* {@link transactionalAddressSuppression}, because unsubscribing from a
|
|
541
|
+
* site's newsletter must never stop the site answering a request (AGL-4098).
|
|
542
|
+
*
|
|
543
|
+
* Fails closed like {@link isEmailSuppressed}: an address it cannot key, or a
|
|
544
|
+
* platform list it cannot read, is `suppressed-platform`.
|
|
545
|
+
*/ export async function siteAddressSuppression(hostId, email, options = {}) {
|
|
546
|
+
var _options_firestore;
|
|
547
|
+
const key = emailSuppressionKey(email);
|
|
548
|
+
if (!key) return 'suppressed-platform';
|
|
549
|
+
if (await isEmailSuppressed(email, options.firestore)) return 'suppressed-platform';
|
|
550
|
+
const group = options.group;
|
|
551
|
+
const hostIds = group && group.hostId === hostId ? consentGroupOptOutHosts(group) : [
|
|
552
|
+
hostId
|
|
553
|
+
];
|
|
554
|
+
const db = (_options_firestore = options.firestore) != null ? _options_firestore : defaultFirestore();
|
|
555
|
+
const docs = await Promise.all(hostIds.map((id)=>db.collection('hosts').doc(id).collection('suppressions').doc(key).get()));
|
|
556
|
+
return docs.some((doc)=>doc.exists) ? 'suppressed-host' : null;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Whether a site may send one address something THEY ASKED FOR (AGL-4098):
|
|
560
|
+
* a quote they requested, the confirmation of a form they just sent.
|
|
561
|
+
* `suppressed-platform` when the address bounced for good or reported mail as
|
|
562
|
+
* spam (the platform list, {@link isEmailSuppressed}), else null.
|
|
563
|
+
*
|
|
564
|
+
* The site's own list is deliberately NOT read. It holds marketing
|
|
565
|
+
* unsubscribes, and an unsubscribe is a preference about a site's newsletter,
|
|
566
|
+
* not a request to stop being answered — the reasoning the member password
|
|
567
|
+
* reset already follows (`membership-recover.ts` in commerce), and the reason
|
|
568
|
+
* an order receipt is never refused for one. A bounce or a complaint is a fact
|
|
569
|
+
* about the mailbox and the sending domain every site shares, so it still
|
|
570
|
+
* refuses.
|
|
571
|
+
*
|
|
572
|
+
* Fails closed like {@link isEmailSuppressed}: an address it cannot key, or a
|
|
573
|
+
* list it cannot read, is `suppressed-platform`.
|
|
574
|
+
*/ export async function transactionalAddressSuppression(email, options = {}) {
|
|
575
|
+
if (!emailSuppressionKey(email)) return 'suppressed-platform';
|
|
576
|
+
return await isEmailSuppressed(email, options.firestore) ? 'suppressed-platform' : null;
|
|
577
|
+
}
|
|
531
578
|
/**
|
|
532
579
|
* The sendable subset of a recipient list, for the bulk senders.
|
|
533
580
|
*
|