@hogsend/engine 0.42.0 → 0.43.0
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 +9 -7
- package/src/container.ts +192 -4
- package/src/env.ts +28 -0
- package/src/index.ts +39 -0
- package/src/journeys/journey-boundary.ts +5 -1
- package/src/journeys/journey-context.ts +26 -0
- package/src/lib/journey-suppress.ts +33 -2
- package/src/lib/outbound.ts +58 -0
- package/src/lib/phone.ts +19 -0
- package/src/lib/preferences.ts +32 -0
- package/src/lib/sms-frequency-cap.ts +47 -0
- package/src/lib/sms-inbound.ts +200 -0
- package/src/lib/sms-link-tracking.ts +160 -0
- package/src/lib/sms-mailer.ts +338 -0
- package/src/lib/sms-provider-registry.ts +35 -0
- package/src/lib/sms-providers-from-env.ts +100 -0
- package/src/lib/sms-service-types.ts +169 -0
- package/src/lib/sms-tracked.ts +590 -0
- package/src/lib/sms.ts +117 -0
- package/src/lib/tracking-event-names.ts +6 -0
- package/src/lib/tracking-events.ts +107 -1
- package/src/lib/tracking.ts +2 -1
- package/src/lib/webhook-signing.ts +17 -1
- package/src/lists/channels.ts +21 -0
- package/src/routes/email/unsubscribe.ts +1 -0
- package/src/routes/index.ts +6 -0
- package/src/routes/lists/index.ts +54 -7
- package/src/routes/tracking/click-pipeline.ts +83 -2
- package/src/routes/tracking/short.ts +61 -0
- package/src/routes/webhooks/index.ts +5 -1
- package/src/routes/webhooks/sms-provider.ts +110 -0
- package/src/workflows/import-contacts.ts +61 -4
|
@@ -1,17 +1,70 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { Database } from "@hogsend/db";
|
|
2
|
+
import { contacts, createDatabase, importJobs } from "@hogsend/db";
|
|
3
|
+
import { and, eq, isNull } from "drizzle-orm";
|
|
3
4
|
import Papa from "papaparse";
|
|
4
5
|
import { resolveOrCreateContact } from "../lib/contacts.js";
|
|
5
6
|
import { hatchet } from "../lib/hatchet.js";
|
|
7
|
+
import { normalizePhone } from "../lib/phone.js";
|
|
6
8
|
|
|
7
9
|
const BATCH_SIZE = 500;
|
|
8
10
|
|
|
9
11
|
interface ImportRow {
|
|
10
12
|
externalId?: string;
|
|
11
13
|
email?: string;
|
|
14
|
+
/** Optional E.164 phone; attached to the resolved contact (SMS channel). */
|
|
15
|
+
phone?: string;
|
|
12
16
|
properties?: Record<string, unknown>;
|
|
13
17
|
}
|
|
14
18
|
|
|
19
|
+
/**
|
|
20
|
+
* True when `err` is the `contacts_phone_unique_idx` partial-unique violation
|
|
21
|
+
* (another live contact already owns the number). Walks the drizzle cause
|
|
22
|
+
* chain for PG 23505, mirroring `isSlugUniqueViolation` in lib/links.ts.
|
|
23
|
+
*/
|
|
24
|
+
function isPhoneUniqueViolation(err: unknown): boolean {
|
|
25
|
+
let candidate: unknown = err;
|
|
26
|
+
while (candidate && typeof candidate === "object") {
|
|
27
|
+
const c = candidate as {
|
|
28
|
+
code?: string;
|
|
29
|
+
constraint_name?: string;
|
|
30
|
+
constraint?: string;
|
|
31
|
+
cause?: unknown;
|
|
32
|
+
};
|
|
33
|
+
if (c.code === "23505") {
|
|
34
|
+
const constraint = c.constraint_name ?? c.constraint ?? "";
|
|
35
|
+
return constraint.includes("contacts_phone_unique");
|
|
36
|
+
}
|
|
37
|
+
candidate = c.cause;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Best-effort attach a normalized phone to a resolved contact's `phone` column
|
|
44
|
+
* without minting an identity conflict. Only fills a NULL column, and swallows
|
|
45
|
+
* ONLY the partial-unique conflict (another live contact already owns that
|
|
46
|
+
* number) so one duplicate phone never fails the whole import row — any other
|
|
47
|
+
* DB error rethrows into the row's error accounting instead of silently
|
|
48
|
+
* dropping the phone (the task runs with retries: 0).
|
|
49
|
+
*/
|
|
50
|
+
async function attachPhone(
|
|
51
|
+
db: Database,
|
|
52
|
+
contactId: string,
|
|
53
|
+
rawPhone: string | undefined,
|
|
54
|
+
): Promise<void> {
|
|
55
|
+
const phone = normalizePhone(rawPhone);
|
|
56
|
+
if (!phone) return;
|
|
57
|
+
try {
|
|
58
|
+
await db
|
|
59
|
+
.update(contacts)
|
|
60
|
+
.set({ phone, updatedAt: new Date() })
|
|
61
|
+
.where(and(eq(contacts.id, contactId), isNull(contacts.phone)));
|
|
62
|
+
} catch (err) {
|
|
63
|
+
if (!isPhoneUniqueViolation(err)) throw err;
|
|
64
|
+
// Unique-index conflict (another contact owns this phone) — leave unset.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
15
68
|
export const importContactsTask = hatchet.task({
|
|
16
69
|
name: "import-contacts",
|
|
17
70
|
retries: 0,
|
|
@@ -77,7 +130,10 @@ export const importContactsTask = hatchet.task({
|
|
|
77
130
|
userId: row.externalId,
|
|
78
131
|
email: row.email,
|
|
79
132
|
contactProperties: row.properties,
|
|
80
|
-
}).then(() =>
|
|
133
|
+
}).then(async (result) => {
|
|
134
|
+
await attachPhone(db, result.id, row.phone);
|
|
135
|
+
return { index: i + idx, ok: true };
|
|
136
|
+
});
|
|
81
137
|
}),
|
|
82
138
|
);
|
|
83
139
|
|
|
@@ -131,11 +187,12 @@ function parseCsv(data: string): ImportRow[] {
|
|
|
131
187
|
}
|
|
132
188
|
|
|
133
189
|
return result.data.map((row) => {
|
|
134
|
-
const { externalId, email, ...rest } = row;
|
|
190
|
+
const { externalId, email, phone, ...rest } = row;
|
|
135
191
|
const properties = Object.keys(rest).length > 0 ? rest : undefined;
|
|
136
192
|
return {
|
|
137
193
|
externalId: externalId || undefined,
|
|
138
194
|
email: email || undefined,
|
|
195
|
+
phone: phone || undefined,
|
|
139
196
|
properties: properties as Record<string, unknown> | undefined,
|
|
140
197
|
};
|
|
141
198
|
});
|