@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.
@@ -1,17 +1,70 @@
1
- import { createDatabase, importJobs } from "@hogsend/db";
2
- import { eq } from "drizzle-orm";
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(() => ({ index: i + idx, ok: true }));
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
  });