@hogsend/engine 0.31.1 → 0.32.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.
@@ -13,6 +13,11 @@ import {
13
13
  renderToHtml,
14
14
  } from "@hogsend/email";
15
15
  import { eq } from "drizzle-orm";
16
+ import {
17
+ deriveJourneyKey,
18
+ getJourneyBoundary,
19
+ registerKey,
20
+ } from "../journeys/journey-boundary.js";
16
21
  import { getListRegistry } from "../lists/registry-singleton.js";
17
22
  import type { TestModeState } from "./domain-status.js";
18
23
  import {
@@ -65,8 +70,47 @@ interface TrackedEmailDeps {
65
70
  testMode?: TestModeState | null;
66
71
  }
67
72
 
73
+ /**
74
+ * Boundary-aware entry point. When a send reaches the tracked mailer from INSIDE
75
+ * a journey `run()` WITHOUT an idempotency key already set — i.e. a journey that
76
+ * called the raw `getEmailService().send(...)` instead of the `sendEmail()`
77
+ * helper (which derives its own key) — derive the same deterministic, replay-
78
+ * stable key HERE so the engine's exactly-once guarantee covers that path too.
79
+ * This closes the raw-service footgun: ANY journey send is auto-keyed + memoized
80
+ * regardless of which import the author reached for. Outside a journey, or when
81
+ * a key is already set (sendEmail / POST /v1/emails), this is a transparent
82
+ * pass-through to {@link sendTrackedEmailInner}.
83
+ */
68
84
  export async function sendTrackedEmail<K extends TemplateName>(
69
85
  opts: TrackedEmailDeps & { options: SendTrackedEmailOptions<K> },
86
+ ): Promise<TrackedSendResult> {
87
+ const boundary = getJourneyBoundary();
88
+ if (!boundary || opts.options.idempotencyKey) {
89
+ return sendTrackedEmailInner(opts);
90
+ }
91
+
92
+ // Raw-service send inside a journey: derive the key the same way sendEmail
93
+ // would (site = nearest authored wait label ?? templateKey; discriminant =
94
+ // templateKey), register it for the loud intra-run collision throw, thread it
95
+ // onto the options, and memoize the whole send (Layer 1 fast path when the
96
+ // engine supports eviction; Layer 2 DB key otherwise).
97
+ const site = boundary.currentLabel ?? String(opts.options.templateKey);
98
+ const key = deriveJourneyKey({
99
+ kind: "send",
100
+ anchor: boundary.runAnchor,
101
+ site,
102
+ discriminant: String(opts.options.templateKey),
103
+ });
104
+ registerKey(boundary, key);
105
+ const keyed = {
106
+ ...opts,
107
+ options: { ...opts.options, idempotencyKey: key },
108
+ };
109
+ return boundary.memoize([key], () => sendTrackedEmailInner(keyed));
110
+ }
111
+
112
+ async function sendTrackedEmailInner<K extends TemplateName>(
113
+ opts: TrackedEmailDeps & { options: SendTrackedEmailOptions<K> },
70
114
  ): Promise<TrackedSendResult> {
71
115
  const {
72
116
  db,
@@ -106,6 +150,15 @@ export async function sendTrackedEmail<K extends TemplateName>(
106
150
  // Idempotency short-circuit (POST /v1/emails): a retry with the same key
107
151
  // returns the prior send instead of dispatching a duplicate provider call /
108
152
  // tracking artifacts (mirrors the user_events idempotency pattern).
153
+ //
154
+ // CRASH-SAFETY: only a TERMINAL-success ("sent") prior row is a satisfied
155
+ // duplicate. A "queued" row means a prior attempt inserted the row but the
156
+ // worker died before the provider returned or before the status flip — the
157
+ // email may NEVER have gone out. Returning it as a duplicate would silently
158
+ // suppress a never-delivered send. Instead, REUSE that row and re-drive the
159
+ // provider call (no second row — the unique index is honored). The failed path
160
+ // already NULLs the key, so a "failed" row never collides here.
161
+ let reuseRowId: string | undefined;
109
162
  if (options.idempotencyKey) {
110
163
  const existing = await db
111
164
  .select({ id: emailSends.id, status: emailSends.status })
@@ -114,7 +167,11 @@ export async function sendTrackedEmail<K extends TemplateName>(
114
167
  .limit(1);
115
168
  const prior = existing[0];
116
169
  if (prior) {
117
- return idempotentResult(prior);
170
+ if (prior.status === "queued") {
171
+ reuseRowId = prior.id;
172
+ } else {
173
+ return idempotentResult(prior);
174
+ }
118
175
  }
119
176
  }
120
177
 
@@ -304,45 +361,62 @@ export async function sendTrackedEmail<K extends TemplateName>(
304
361
  const wireSubject = redirect ? redirect.subject : subject;
305
362
  const wireFrom = redirect ? redirect.from : options.from;
306
363
 
307
- const baseInsert = db.insert(emailSends).values({
308
- templateKey: options.templateKey,
309
- fromEmail: wireFrom,
310
- toEmail: redirect ? redirect.redirectTo : options.to,
311
- subject: wireSubject,
312
- category: effectiveCategory,
313
- journeyStateId: options.journeyStateId,
314
- userId: options.userId,
315
- userEmail: options.userEmail ?? options.to,
316
- status: "queued",
317
- idempotencyKey: options.idempotencyKey,
318
- ...(redirect
319
- ? { metadata: { testMode: true, originalTo: options.to } }
320
- : {}),
321
- });
364
+ // Re-driving an orphaned "queued" row (crash before the provider returned):
365
+ // reuse that row instead of inserting a second one, so the provider call is
366
+ // re-attempted while the unique idempotency key stays honored.
367
+ let emailSendId: string;
368
+ if (reuseRowId) {
369
+ emailSendId = reuseRowId;
370
+ } else {
371
+ const baseInsert = db.insert(emailSends).values({
372
+ templateKey: options.templateKey,
373
+ fromEmail: wireFrom,
374
+ toEmail: redirect ? redirect.redirectTo : options.to,
375
+ subject: wireSubject,
376
+ category: effectiveCategory,
377
+ journeyStateId: options.journeyStateId,
378
+ userId: options.userId,
379
+ userEmail: options.userEmail ?? options.to,
380
+ status: "queued",
381
+ idempotencyKey: options.idempotencyKey,
382
+ ...(redirect
383
+ ? { metadata: { testMode: true, originalTo: options.to } }
384
+ : {}),
385
+ });
322
386
 
323
- // With an idempotency key, swallow a concurrent-insert collision on the unique
324
- // index (the select-then-insert above is not atomic) and return the winner.
325
- const insertRows = options.idempotencyKey
326
- ? await baseInsert
327
- .onConflictDoNothing({ target: emailSends.idempotencyKey })
328
- .returning({ id: emailSends.id })
329
- : await baseInsert.returning({ id: emailSends.id });
330
-
331
- const insertedRow = insertRows[0];
332
- if (!insertedRow && options.idempotencyKey) {
333
- // A concurrent send claimed the key first — return its row.
334
- const winner = await db
335
- .select({ id: emailSends.id, status: emailSends.status })
336
- .from(emailSends)
337
- .where(eq(emailSends.idempotencyKey, options.idempotencyKey))
338
- .limit(1);
339
- const won = winner[0];
340
- if (won) {
341
- return idempotentResult(won);
387
+ // With an idempotency key, swallow a concurrent-insert collision on the
388
+ // unique index (the select-then-insert above is not atomic) and return the
389
+ // winner.
390
+ const insertRows = options.idempotencyKey
391
+ ? await baseInsert
392
+ .onConflictDoNothing({ target: emailSends.idempotencyKey })
393
+ .returning({ id: emailSends.id })
394
+ : await baseInsert.returning({ id: emailSends.id });
395
+
396
+ const insertedRow = insertRows[0];
397
+ if (!insertedRow && options.idempotencyKey) {
398
+ // A concurrent send claimed the key first — return its row, UNLESS it is
399
+ // an orphaned "queued" row (crash mid-send), in which case re-drive it.
400
+ const winner = await db
401
+ .select({ id: emailSends.id, status: emailSends.status })
402
+ .from(emailSends)
403
+ .where(eq(emailSends.idempotencyKey, options.idempotencyKey))
404
+ .limit(1);
405
+ const won = winner[0];
406
+ if (won) {
407
+ if (won.status !== "queued") {
408
+ return idempotentResult(won);
409
+ }
410
+ emailSendId = won.id;
411
+ } else {
412
+ throw new Error("Failed to insert email_sends row");
413
+ }
414
+ } else if (!insertedRow) {
415
+ throw new Error("Failed to insert email_sends row");
416
+ } else {
417
+ emailSendId = insertedRow.id;
342
418
  }
343
419
  }
344
- if (!insertedRow) throw new Error("Failed to insert email_sends row");
345
- const emailSendId = insertedRow.id;
346
420
 
347
421
  try {
348
422
  // HTML-ONLY wire — the engine ALWAYS renders React → HTML itself. When
@@ -3,3 +3,12 @@
3
3
  // not journey content — so they live here rather than in app-side constants.
4
4
  export const EMAIL_OPENED = "email.opened" as const;
5
5
  export const EMAIL_LINK_CLICKED = "email.link_clicked" as const;
6
+
7
+ // First-party BUS event for a NON-email managed-link click (re-ingested through
8
+ // `ingestEvent` so journeys can trigger / `ctx.waitForEvent` on a click of a
9
+ // SPECIFIC managed link). It deliberately SHARES the name string with the
10
+ // existing per-hit OUTBOUND `link.clicked` webhook, but the two differ in
11
+ // payload + subject: the OUTBOUND carries `trackedLinks.id` as `linkId` + the
12
+ // raw mint distinctId; the BUS event carries the MANAGED `links.id` as `linkId`
13
+ // + the RESOLVED survivor contact key (see `pushLinkClickEvent`).
14
+ export const LINK_CLICKED = "link.clicked" as const;
@@ -4,6 +4,7 @@ import { type Database, emailSends, journeyStates } from "@hogsend/db";
4
4
  import { eq } from "drizzle-orm";
5
5
  import { type IngestResult, ingestEvent } from "./ingestion.js";
6
6
  import type { Logger } from "./logger.js";
7
+ import { LINK_CLICKED } from "./tracking-event-names.js";
7
8
 
8
9
  interface EmailSendContext {
9
10
  userId: string;
@@ -172,6 +173,78 @@ export async function pushTrackingEvent(
172
173
  userId: ctx.userId,
173
174
  userEmail: ctx.userEmail,
174
175
  eventProperties: properties,
176
+ source: "tracking",
177
+ idempotencyKey: opts.idempotencyKey,
178
+ },
179
+ });
180
+ }
181
+
182
+ export interface PushLinkClickEventOpts {
183
+ db: Database;
184
+ hatchet: HatchetClient;
185
+ registry: JourneyRegistry;
186
+ logger: Logger;
187
+ /**
188
+ * The MANAGED `links.id` — the durable, re-mint-safe key a journey filters on
189
+ * via `trigger.where`/`ctx.waitForEvent` (`where: b.prop("linkId").eq(...)`).
190
+ * Null when the tracked link has no managed parent row.
191
+ */
192
+ linkId: string | null;
193
+ /** The `tracked_links.id` (the redirect `:id`); disambiguates a re-mint. */
194
+ trackedLinkId: string;
195
+ campaign: string | null;
196
+ source: string | null;
197
+ linkType: string | null;
198
+ linkUrl: string;
199
+ /**
200
+ * The personal link's canonical contact key (`links.distinct_id`). MUST be the
201
+ * subject's EXTERNAL canonical key (an app `external_id`, or "discord:<id>" for
202
+ * a Discord member) — a raw snowflake or anonymous id would fork an orphan
203
+ * contact. Null ⇒ a broadcast/public link (no person) ⇒ no re-ingest.
204
+ */
205
+ distinctId: string | null;
206
+ idempotencyKey?: string;
207
+ }
208
+
209
+ /**
210
+ * Re-push a NON-email managed-link click onto the INTERNAL bus (`ingestEvent`)
211
+ * as the first-party `link.clicked` event so journeys can trigger / await a
212
+ * click of a SPECIFIC managed link (filtered by `linkId`/`campaign`).
213
+ *
214
+ * IDENTITY GATE (crash-guard): `ingestEvent`→`resolveOrCreateContact` THROWS on
215
+ * a zero-key event, so a broadcast/public link (`distinctId == null`) returns
216
+ * `undefined` WITHOUT calling ingest. The click route ALSO suppresses
217
+ * bot/prefetch hits upstream (`isBotOrPrefetch`), so an unfurl bot never reaches
218
+ * here. All six payload keys are scalars (or null) so they survive ingest and
219
+ * reach `trigger.where` + `waitForEvent.properties`.
220
+ *
221
+ * Distinct from the per-hit OUTBOUND `link.clicked` webhook: that fires for
222
+ * EVERY hit and carries `trackedLinks.id` + the raw mint distinctId; THIS bus
223
+ * event carries the managed `links.id` + the resolved survivor contact key.
224
+ */
225
+ export async function pushLinkClickEvent(
226
+ opts: PushLinkClickEventOpts,
227
+ ): Promise<IngestResult | undefined> {
228
+ const { db, hatchet, registry, logger, distinctId } = opts;
229
+ if (!distinctId) return undefined;
230
+
231
+ return await ingestEvent({
232
+ db,
233
+ registry,
234
+ hatchet,
235
+ logger,
236
+ event: {
237
+ event: LINK_CLICKED,
238
+ userId: distinctId,
239
+ eventProperties: {
240
+ linkId: opts.linkId,
241
+ trackedLinkId: opts.trackedLinkId,
242
+ campaign: opts.campaign,
243
+ source: opts.source,
244
+ linkType: opts.linkType,
245
+ linkUrl: opts.linkUrl,
246
+ },
247
+ source: "tracking",
175
248
  idempotencyKey: opts.idempotencyKey,
176
249
  },
177
250
  });
@@ -410,6 +410,7 @@ export const bulkRouter = new OpenAPIHono<AppEnv>()
410
410
  // Stored `user_events.properties` is the event-property bag (D2).
411
411
  eventProperties:
412
412
  (event.properties as Record<string, unknown>) ?? {},
413
+ source: "import",
413
414
  },
414
415
  }),
415
416
  ),
@@ -504,6 +505,7 @@ export const bulkRouter = new OpenAPIHono<AppEnv>()
504
505
  // Public batch-enroll request field stays `properties`
505
506
  // (decision #14); maps to the event-property bag (D2).
506
507
  eventProperties: user.properties ?? {},
508
+ source: "import",
507
509
  },
508
510
  }),
509
511
  ),
@@ -1,6 +1,6 @@
1
- import { userEvents } from "@hogsend/db";
1
+ import { contacts, userEvents } from "@hogsend/db";
2
2
  import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
3
- import { and, count, desc, eq, gte, lte } from "drizzle-orm";
3
+ import { and, count, desc, eq, gte, isNull, lte, or, sql } from "drizzle-orm";
4
4
  import type { AppEnv } from "../../app.js";
5
5
  import { ingestEvent } from "../../lib/ingestion.js";
6
6
 
@@ -10,20 +10,53 @@ const eventSchema = z.object({
10
10
  event: z.string(),
11
11
  properties: z.record(z.string(), z.unknown()).nullable(),
12
12
  occurredAt: z.string(),
13
+ // Where the event entered the pipeline ("posthog", "api", "studio", …).
14
+ source: z.string().nullable(),
15
+ // Resolved from the matching live contact (user_events.userId ==
16
+ // contacts.externalId), so the Events feed can show WHO an event is from.
17
+ userEmail: z.string().nullable(),
18
+ contactId: z.string().nullable(),
13
19
  });
14
20
 
15
21
  const errorSchema = z.object({ error: z.string() });
16
22
 
17
- function serializeEvent(row: typeof userEvents.$inferSelect) {
23
+ /** A `userEvents` row joined to its (optional) live contact. */
24
+ type JoinedEvent = {
25
+ event: typeof userEvents.$inferSelect;
26
+ contact: { id: string; email: string | null } | null;
27
+ };
28
+
29
+ function serializeEvent(row: JoinedEvent) {
30
+ const e = row.event;
18
31
  return {
19
- id: row.id,
20
- userId: row.userId,
21
- event: row.event,
22
- properties: (row.properties ?? null) as Record<string, unknown> | null,
23
- occurredAt: row.occurredAt.toISOString(),
32
+ id: e.id,
33
+ userId: e.userId,
34
+ event: e.event,
35
+ properties: (e.properties ?? null) as Record<string, unknown> | null,
36
+ occurredAt: e.occurredAt.toISOString(),
37
+ source: e.source ?? null,
38
+ userEmail: row.contact?.email ?? null,
39
+ contactId: row.contact?.id ?? null,
24
40
  };
25
41
  }
26
42
 
43
+ /**
44
+ * LEFT JOIN to the live contact for the userId. `userEvents.userId` holds the
45
+ * resolved canonical key (`external_id ?? anonymous_id ?? id`), so match all
46
+ * three — covering email-only/anonymous contacts, not just externalId-keyed
47
+ * ones. Each key is partial-unique among live rows, and the three namespaces
48
+ * don't overlap in practice, so at most one live contact matches per userId.
49
+ */
50
+ const contactJoin = and(
51
+ or(
52
+ eq(contacts.externalId, userEvents.userId),
53
+ eq(contacts.anonymousId, userEvents.userId),
54
+ eq(sql`${contacts.id}::text`, userEvents.userId),
55
+ ),
56
+ isNull(contacts.deletedAt),
57
+ );
58
+ const contactCols = { id: contacts.id, email: contacts.email };
59
+
27
60
  const listRoute = createRoute({
28
61
  method: "get",
29
62
  path: "/",
@@ -35,6 +68,7 @@ const listRoute = createRoute({
35
68
  offset: z.coerce.number().min(0).default(0),
36
69
  userId: z.string().optional(),
37
70
  event: z.string().optional(),
71
+ source: z.string().optional(),
38
72
  from: z.string().datetime().optional(),
39
73
  to: z.string().datetime().optional(),
40
74
  }),
@@ -140,6 +174,8 @@ export const eventsRouter = new OpenAPIHono<AppEnv>()
140
174
  userId,
141
175
  userEmail,
142
176
  eventProperties: properties ?? {},
177
+ // The Studio Debug panel ("Fire event").
178
+ source: "studio",
143
179
  },
144
180
  });
145
181
 
@@ -147,11 +183,13 @@ export const eventsRouter = new OpenAPIHono<AppEnv>()
147
183
  })
148
184
  .openapi(listRoute, async (c) => {
149
185
  const { db } = c.get("container");
150
- const { limit, offset, userId, event, from, to } = c.req.valid("query");
186
+ const { limit, offset, userId, event, source, from, to } =
187
+ c.req.valid("query");
151
188
 
152
189
  const conditions = [];
153
190
  if (userId) conditions.push(eq(userEvents.userId, userId));
154
191
  if (event) conditions.push(eq(userEvents.event, event));
192
+ if (source) conditions.push(eq(userEvents.source, source));
155
193
  if (from) conditions.push(gte(userEvents.occurredAt, new Date(from)));
156
194
  if (to) conditions.push(lte(userEvents.occurredAt, new Date(to)));
157
195
 
@@ -159,8 +197,9 @@ export const eventsRouter = new OpenAPIHono<AppEnv>()
159
197
 
160
198
  const [rows, totalRows] = await Promise.all([
161
199
  db
162
- .select()
200
+ .select({ event: userEvents, contact: contactCols })
163
201
  .from(userEvents)
202
+ .leftJoin(contacts, contactJoin)
164
203
  .where(where)
165
204
  .orderBy(desc(userEvents.occurredAt))
166
205
  .limit(limit)
@@ -183,8 +222,9 @@ export const eventsRouter = new OpenAPIHono<AppEnv>()
183
222
  const { id } = c.req.valid("param");
184
223
 
185
224
  const rows = await db
186
- .select()
225
+ .select({ event: userEvents, contact: contactCols })
187
226
  .from(userEvents)
227
+ .leftJoin(contacts, contactJoin)
188
228
  .where(eq(userEvents.id, id))
189
229
  .limit(1);
190
230
 
@@ -21,6 +21,7 @@ import { linksRouter } from "./links.js";
21
21
  import { metricsRouter } from "./metrics.js";
22
22
  import { preferencesRouter } from "./preferences.js";
23
23
  import { providerCredentialsRouter } from "./provider-credentials.js";
24
+ import { readinessRouter } from "./readiness.js";
24
25
  import { reportingRouter } from "./reporting.js";
25
26
  import { suppressionsRouter } from "./suppressions.js";
26
27
  import { templatesRouter } from "./templates.js";
@@ -54,3 +55,4 @@ adminRouter.route("/audit-logs", auditLogsRouter);
54
55
  adminRouter.route("/alerts", alertsRouter);
55
56
  adminRouter.route("/dlq", dlqRouter);
56
57
  adminRouter.route("/domain", domainRouter);
58
+ adminRouter.route("/readiness", readinessRouter);
@@ -1,11 +1,21 @@
1
1
  import {
2
2
  type Database,
3
+ emailSends,
3
4
  journeyConfigs,
4
5
  journeyLogs,
5
6
  journeyStates,
6
7
  } from "@hogsend/db";
7
8
  import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
8
- import { and, count, desc, eq, inArray, isNull } from "drizzle-orm";
9
+ import {
10
+ and,
11
+ count,
12
+ desc,
13
+ eq,
14
+ inArray,
15
+ isNotNull,
16
+ isNull,
17
+ sql,
18
+ } from "drizzle-orm";
9
19
  import type { AppEnv } from "../../app.js";
10
20
  import { ingestEvent } from "../../lib/ingestion.js";
11
21
 
@@ -358,6 +368,41 @@ const enrollRoute = createRoute({
358
368
 
359
369
  // --- Handlers ---
360
370
 
371
+ const templatesRoute = createRoute({
372
+ method: "get",
373
+ path: "/{id}/templates",
374
+ tags: ["Admin — Journeys"],
375
+ summary: "Email templates this journey has sent (observed from email_sends)",
376
+ request: {
377
+ params: z.object({ id: z.string() }),
378
+ },
379
+ responses: {
380
+ 200: {
381
+ content: {
382
+ "application/json": {
383
+ schema: z.object({
384
+ templates: z.array(
385
+ z.object({
386
+ templateKey: z.string(),
387
+ sent: z.number(),
388
+ opened: z.number(),
389
+ clicked: z.number(),
390
+ lastSentAt: z.string().nullable(),
391
+ }),
392
+ ),
393
+ }),
394
+ },
395
+ },
396
+ description:
397
+ "Distinct templates sent within this journey, with engagement counts",
398
+ },
399
+ 404: {
400
+ content: { "application/json": { schema: errorSchema } },
401
+ description: "Journey not found",
402
+ },
403
+ },
404
+ });
405
+
361
406
  export const journeysRouter = new OpenAPIHono<AppEnv>()
362
407
  .openapi(listRoute, async (c) => {
363
408
  const { db, registry } = c.get("container");
@@ -665,6 +710,8 @@ export const journeysRouter = new OpenAPIHono<AppEnv>()
665
710
  // Public request field stays `properties` (decision #14); it maps to
666
711
  // the event-property bag of the IngestEvent (D2 split).
667
712
  eventProperties: body.properties ?? {},
713
+ // Studio "Enroll" action (admin manual enrollment).
714
+ source: "studio",
668
715
  },
669
716
  });
670
717
 
@@ -676,4 +723,51 @@ export const journeysRouter = new OpenAPIHono<AppEnv>()
676
723
  },
677
724
  202,
678
725
  );
726
+ })
727
+ .openapi(templatesRoute, async (c) => {
728
+ const { db, registry } = c.get("container");
729
+ const { id } = c.req.valid("param");
730
+
731
+ if (!registry.has(id)) {
732
+ return c.json({ error: "Journey not found" }, 404);
733
+ }
734
+
735
+ // Observed-from-sends: the templates a journey actually sent, routed via
736
+ // emailSends.journeyStateId → journeyStates.journeyId (no journeyId on
737
+ // emailSends). NULL templateKeys (ad-hoc HTML sends) are excluded.
738
+ const rows = await db
739
+ .select({
740
+ templateKey: emailSends.templateKey,
741
+ sent: sql<number>`count(*) filter (where ${emailSends.sentAt} is not null)`,
742
+ opened: sql<number>`count(*) filter (where ${emailSends.openedAt} is not null)`,
743
+ clicked: sql<number>`count(*) filter (where ${emailSends.clickedAt} is not null)`,
744
+ lastSentAt: sql<string | null>`max(${emailSends.sentAt})`,
745
+ })
746
+ .from(emailSends)
747
+ .innerJoin(journeyStates, eq(emailSends.journeyStateId, journeyStates.id))
748
+ .where(
749
+ and(
750
+ eq(journeyStates.journeyId, id),
751
+ isNull(journeyStates.deletedAt),
752
+ isNotNull(emailSends.templateKey),
753
+ ),
754
+ )
755
+ .groupBy(emailSends.templateKey)
756
+ .orderBy(desc(sql`count(*)`));
757
+
758
+ return c.json(
759
+ {
760
+ templates: rows.map((r) => ({
761
+ // isNotNull guarantees a string at runtime; satisfy the type too.
762
+ templateKey: r.templateKey ?? "",
763
+ sent: Number(r.sent),
764
+ opened: Number(r.opened),
765
+ clicked: Number(r.clicked),
766
+ lastSentAt: r.lastSentAt
767
+ ? new Date(r.lastSentAt).toISOString()
768
+ : null,
769
+ })),
770
+ },
771
+ 200,
772
+ );
679
773
  });
@@ -2,7 +2,7 @@ import { linkClicks, links, trackedLinks } from "@hogsend/db";
2
2
  import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
3
3
  import { and, count, desc, eq, inArray, isNull, sql, sum } from "drizzle-orm";
4
4
  import type { AppEnv } from "../../app.js";
5
- import { mintLink } from "../../lib/links.js";
5
+ import { assertHttpUrl, mintLink } from "../../lib/links.js";
6
6
  import { errorSchema } from "../../lib/schemas.js";
7
7
 
8
8
  // ---------------------------------------------------------------------------
@@ -197,13 +197,16 @@ const updateLinkRoute = createRoute({
197
197
  method: "patch",
198
198
  path: "/{id}",
199
199
  tags: ["Admin — Links"],
200
- summary: "Update a managed link (label + campaign)",
200
+ summary: "Update a managed link (destination URL + label + campaign)",
201
201
  request: {
202
202
  params: z.object({ id: z.string().uuid() }),
203
203
  body: {
204
204
  content: {
205
205
  "application/json": {
206
206
  schema: z.object({
207
+ // NOT NULL on both tables — omit = no change, provide = re-target
208
+ // both links.originalUrl and the linked tracked_links row.
209
+ originalUrl: z.string().url().optional(),
207
210
  label: z.string().nullable().optional(),
208
211
  campaign: z.string().nullable().optional(),
209
212
  }),
@@ -216,6 +219,10 @@ const updateLinkRoute = createRoute({
216
219
  content: { "application/json": { schema: linkSchema } },
217
220
  description: "Updated link",
218
221
  },
222
+ 400: {
223
+ content: { "application/json": { schema: errorSchema } },
224
+ description: "Invalid destination URL",
225
+ },
219
226
  404: {
220
227
  content: { "application/json": { schema: errorSchema } },
221
228
  description: "Link not found",
@@ -406,17 +413,53 @@ export const linksRouter = new OpenAPIHono<AppEnv>()
406
413
  const { id } = c.req.valid("param");
407
414
  const body = c.req.valid("json");
408
415
 
409
- const patch: Partial<Pick<LinkRow, "label" | "campaign">> & {
416
+ // Re-targeting the destination: validate http(s) BEFORE opening the tx so an
417
+ // invalid URL never starts a transaction. Reuses the exact open-redirect
418
+ // guard mintLink applies at create time.
419
+ if (body.originalUrl !== undefined) {
420
+ try {
421
+ assertHttpUrl(body.originalUrl);
422
+ } catch (err) {
423
+ const message =
424
+ err instanceof Error ? err.message : "Invalid destination URL";
425
+ return c.json({ error: message }, 400);
426
+ }
427
+ }
428
+
429
+ const patch: Partial<
430
+ Pick<LinkRow, "label" | "campaign" | "originalUrl">
431
+ > & {
410
432
  updatedAt: Date;
411
433
  } = { updatedAt: new Date() };
412
434
  if (body.label !== undefined) patch.label = body.label;
413
435
  if (body.campaign !== undefined) patch.campaign = body.campaign;
436
+ if (body.originalUrl !== undefined) patch.originalUrl = body.originalUrl;
437
+
438
+ // Both writes in ONE transaction so the two originalUrls can never diverge.
439
+ // CRITICAL: the click redirect reads tracked_links.originalUrl fresh per
440
+ // hit (no cache), NOT links.originalUrl — so re-targeting MUST also update
441
+ // the 1:1 tracked_links row (scoped by link_id, which can only ever be the
442
+ // managed tracked row). The links row is the display/source of truth.
443
+ const updated = await db.transaction(async (tx) => {
444
+ const [row] = await tx
445
+ .update(links)
446
+ .set(patch)
447
+ .where(eq(links.id, id))
448
+ .returning();
449
+
450
+ // Signal 404 to the caller; returning null (vs throwing) commits an empty
451
+ // tx and avoids bubbling as a 500.
452
+ if (!row) return null;
453
+
454
+ if (body.originalUrl !== undefined) {
455
+ await tx
456
+ .update(trackedLinks)
457
+ .set({ originalUrl: body.originalUrl, updatedAt: new Date() })
458
+ .where(eq(trackedLinks.linkId, id));
459
+ }
414
460
 
415
- const [updated] = await db
416
- .update(links)
417
- .set(patch)
418
- .where(eq(links.id, id))
419
- .returning();
461
+ return row;
462
+ });
420
463
 
421
464
  if (!updated) {
422
465
  return c.json({ error: "Link not found" }, 404);