@hogsend/engine 0.50.0 → 0.51.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 +10 -10
- package/src/campaigns/cohort-sql.ts +8 -9
- package/src/container.ts +1 -1
- package/src/destinations/define-destination.ts +2 -2
- package/src/flags/reconcile.ts +7 -2
- package/src/lib/campaign-send-key.ts +9 -18
- package/src/lib/conversions.ts +1 -1
- package/src/lib/crm-deals.ts +1 -1
- package/src/lib/crm-ingest.ts +1 -1
- package/src/lib/event-names.ts +7 -1
- package/src/lib/frequency-cap.ts +4 -2
- package/src/lib/funnel-progress.ts +1 -1
- package/src/lib/holdout.ts +1 -1
- package/src/lib/journey-suppress.ts +4 -2
- package/src/lib/lift-stats.ts +2 -2
- package/src/lib/tracked.ts +9 -2
- package/src/routes/admin/attribution.ts +1 -2
- package/src/routes/admin/campaigns.ts +84 -45
- package/src/routes/admin/conversions.ts +1 -1
- package/src/routes/admin/deals.ts +43 -34
- package/src/routes/admin/emails.ts +4 -6
- package/src/routes/admin/events.ts +128 -1
- package/src/routes/admin/groups.ts +34 -0
- package/src/routes/admin/impact.ts +19 -19
- package/src/routes/admin/targeting.ts +4 -1
- package/src/workflows/crm-reconcile.ts +1 -1
- package/src/middleware/auth.ts +0 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hogsend/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.51.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -53,17 +53,17 @@
|
|
|
53
53
|
"svix": "^1.95.1",
|
|
54
54
|
"winston": "^3.19.0",
|
|
55
55
|
"zod": "^4.4.3",
|
|
56
|
-
"@hogsend/attribution": "^0.
|
|
57
|
-
"@hogsend/core": "^0.
|
|
58
|
-
"@hogsend/db": "^0.
|
|
59
|
-
"@hogsend/email": "^0.
|
|
60
|
-
"@hogsend/plugin-posthog": "^0.
|
|
61
|
-
"@hogsend/plugin-resend": "^0.
|
|
62
|
-
"@hogsend/sms": "^0.
|
|
56
|
+
"@hogsend/attribution": "^0.51.0",
|
|
57
|
+
"@hogsend/core": "^0.51.0",
|
|
58
|
+
"@hogsend/db": "^0.51.0",
|
|
59
|
+
"@hogsend/email": "^0.51.0",
|
|
60
|
+
"@hogsend/plugin-posthog": "^0.51.0",
|
|
61
|
+
"@hogsend/plugin-resend": "^0.51.0",
|
|
62
|
+
"@hogsend/sms": "^0.51.0"
|
|
63
63
|
},
|
|
64
64
|
"optionalDependencies": {
|
|
65
|
-
"@hogsend/plugin-postmark": "^0.
|
|
66
|
-
"@hogsend/plugin-twilio": "^0.
|
|
65
|
+
"@hogsend/plugin-postmark": "^0.51.0",
|
|
66
|
+
"@hogsend/plugin-twilio": "^0.51.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@types/node": "^22.15.3",
|
|
@@ -29,7 +29,6 @@ import {
|
|
|
29
29
|
} from "@hogsend/db";
|
|
30
30
|
import { type SQL, sql } from "drizzle-orm";
|
|
31
31
|
import type { PgColumn } from "drizzle-orm/pg-core";
|
|
32
|
-
import { campaignSendKeyPattern } from "../lib/campaign-send-key.js";
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
34
|
* The v1 linked-identity map: connector id → the `contacts` column that holds
|
|
@@ -107,13 +106,13 @@ export function waveConditionSql(opts: {
|
|
|
107
106
|
}
|
|
108
107
|
|
|
109
108
|
/**
|
|
110
|
-
* Engagement over THIS campaign's prior deliveries
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
109
|
+
* Engagement over THIS campaign's prior deliveries, anchored on the indexed
|
|
110
|
+
* `campaign_id` column (stamped on every campaign-dispatched row regardless
|
|
111
|
+
* of key format; suppressed rows carry it too but have no engagement
|
|
112
|
+
* timestamps, so they can never match positively). Negative checks are NOT
|
|
113
|
+
* EXISTS of the same positive-engagement subquery, so `notOpened()` reads
|
|
114
|
+
* "no opened send of this campaign", including members who received nothing
|
|
115
|
+
* (e.g. suppressed on the prior wave).
|
|
117
116
|
*/
|
|
118
117
|
function engagementSql(
|
|
119
118
|
condition: EmailEngagementCondition,
|
|
@@ -129,7 +128,7 @@ function engagementSql(
|
|
|
129
128
|
: sql.empty();
|
|
130
129
|
const positive = sql`exists (
|
|
131
130
|
select 1 from ${emailSends}
|
|
132
|
-
where ${emailSends.
|
|
131
|
+
where ${emailSends.campaignId} = ${campaignId}
|
|
133
132
|
and ${emailSends.toEmail} = ${campaignRecipients.email}
|
|
134
133
|
and ${engagedAt} is not null${templateFilter}
|
|
135
134
|
)`;
|
package/src/container.ts
CHANGED
|
@@ -490,7 +490,7 @@ export interface HogsendClientOptions {
|
|
|
490
490
|
*/
|
|
491
491
|
conversionDestinations?: ConversionDestination[];
|
|
492
492
|
/**
|
|
493
|
-
* CRM sync providers
|
|
493
|
+
* CRM sync providers — the pluggable
|
|
494
494
|
* layer that pushes leads INTO client CRMs and lands pipeline stage changes
|
|
495
495
|
* + deal values back on the event spine as `funnel.stage_changed`. Register
|
|
496
496
|
* one (`provider`) or many (`providers`); each is webhook-served at
|
|
@@ -23,8 +23,8 @@ import type { OutboundEventName } from "../lib/outbound.js";
|
|
|
23
23
|
* NOTE: `defineDestination` is for event FAN-OUT to product/data tools
|
|
24
24
|
* (PostHog, Segment, Slack, a CRM, a warehouse). Ad-platform conversion
|
|
25
25
|
* forwarding (Meta CAPI, Google Enhanced Conversions) is Hogsend-native but
|
|
26
|
-
* lives in its own sibling layer (`defineConversionDestination
|
|
27
|
-
*
|
|
26
|
+
* lives in its own sibling layer (`defineConversionDestination`) — it needs
|
|
27
|
+
* identifier hashing, click-id
|
|
28
28
|
* joins, and deterministic event_id idempotency this generic pipe deliberately
|
|
29
29
|
* does not have. It is NOT deferred to any third-party CDP: the money path has
|
|
30
30
|
* no external dependency.
|
package/src/flags/reconcile.ts
CHANGED
|
@@ -117,10 +117,15 @@ export async function reconcileDefinedFlags(opts: {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
// A same-key flag owned by an operator/Studio (origin != "code") is never
|
|
120
|
-
// clobbered by a code definition — leave it and warn.
|
|
120
|
+
// clobbered by a code definition — leave it and warn. The warn names the
|
|
121
|
+
// resolution because this is easy to trip innocently: `origin` defaults
|
|
122
|
+
// to "native" on insert, so any row seeded out-of-band (SQL, a seed
|
|
123
|
+
// script) for a code-defined key lands here on every boot.
|
|
121
124
|
if (existing.origin !== "code") {
|
|
122
125
|
logger.warn(
|
|
123
|
-
|
|
126
|
+
`flags: key "${key}" already owned by a non-code flag (origin "${existing.origin}") — leaving it untouched. ` +
|
|
127
|
+
"If this row should be code-owned (contract-synced from defineFlag), set origin = 'code' on it; " +
|
|
128
|
+
"otherwise remove the defineFlag that shares this key.",
|
|
124
129
|
{ key, origin: existing.origin },
|
|
125
130
|
);
|
|
126
131
|
result.skipped++;
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The deterministic per-recipient idempotency key a campaign send writes to
|
|
3
|
-
* `email_sends.idempotency_key`.
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
3
|
+
* `email_sends.idempotency_key`. The key exists for DEDUP; campaign
|
|
4
|
+
* attribution is the `email_sends.campaign_id` column (stamped on every
|
|
5
|
+
* campaign-dispatched row including suppressed sends, which write no key at
|
|
6
|
+
* all; legacy rows backfilled by migration 0051). The one thing the key
|
|
7
|
+
* still uniquely carries is the STEP NUMBER of a multi-step campaign, which
|
|
8
|
+
* is why the per-step stats breakdown matches `campaignStepSendKeyPattern`
|
|
9
|
+
* (scoped to the campaign_id FK first).
|
|
8
10
|
*
|
|
9
11
|
* Two formats, chosen by the campaign's step count (NOT per call site):
|
|
10
12
|
*
|
|
11
13
|
* - Single-step campaigns (steps.length === 1 or a NULL steps blob) ALWAYS
|
|
12
|
-
* use the legacy `campaign:<id>:<email>` — behavior
|
|
13
|
-
*
|
|
14
|
+
* use the legacy `campaign:<id>:<email>` — behavior and any in-flight
|
|
15
|
+
* campaign at deploy time are byte-for-byte unchanged.
|
|
14
16
|
* - Multi-step campaigns use `campaign:<id>:<step>:<email>` for ALL steps
|
|
15
17
|
* including 0. No ambiguity with the legacy format is possible (no
|
|
16
18
|
* multi-step campaign exists before this ships), and per-step stats
|
|
@@ -34,17 +36,6 @@ function escapeLike(value: string): string {
|
|
|
34
36
|
return value.replace(/([\\%_])/g, "\\$1");
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
/**
|
|
38
|
-
* SQL LIKE pattern matching every send of one campaign — a correct superset
|
|
39
|
-
* of BOTH key formats (legacy and step-scoped), so campaign-level stats need
|
|
40
|
-
* no format awareness. The id is escaped so LIKE metacharacters in it can't
|
|
41
|
-
* widen the match (ids are UUIDs today, but the route accepts arbitrary
|
|
42
|
-
* strings).
|
|
43
|
-
*/
|
|
44
|
-
export function campaignSendKeyPattern(campaignId: string): string {
|
|
45
|
-
return `campaign:${escapeLike(campaignId)}:%`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
39
|
/**
|
|
49
40
|
* SQL LIKE pattern matching every send of ONE step of a multi-step campaign
|
|
50
41
|
* (`campaign:<id>:<step>:%`). Meaningless against legacy single-step keys —
|
package/src/lib/conversions.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { REVENUE_EXCLUDED_EVENTS } from "./revenue.js";
|
|
|
12
12
|
import { createOptionalSingleton } from "./singleton.js";
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Conversion evaluation at ingest
|
|
15
|
+
* Conversion evaluation at ingest —
|
|
16
16
|
* the hook `ingestEvent` calls after a FRESH event insert. Definitions are
|
|
17
17
|
* code-first (`createHogsendClient({ conversions })`); a fired instance is
|
|
18
18
|
* recorded once per (definition, event row) — the unique index makes a
|
package/src/lib/crm-deals.ts
CHANGED
|
@@ -10,7 +10,7 @@ import type { Logger } from "./logger.js";
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* The deals projection + external-id alias map
|
|
13
|
-
|
|
13
|
+
*. Events stay the append-only truth;
|
|
14
14
|
* this module materializes current deal state with the MONOTONIC stage rule:
|
|
15
15
|
* a change applies only when it advances the canonical rank (heals
|
|
16
16
|
* webhook+poll double-detection and out-of-order delivery), `lost` applies
|
package/src/lib/crm-ingest.ts
CHANGED
|
@@ -20,7 +20,7 @@ import { emitOutbound } from "./outbound.js";
|
|
|
20
20
|
/**
|
|
21
21
|
* Land normalized {@link CrmStageEvent}s on the spine — the shared sink of
|
|
22
22
|
* BOTH the webhook route and the reconciliation poll
|
|
23
|
-
|
|
23
|
+
*.
|
|
24
24
|
*
|
|
25
25
|
* Per event: (1) resolve the contact — `crm_links` alias first (works with
|
|
26
26
|
* zero PII in the payload), else the payload email (minting the links so the
|
package/src/lib/event-names.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import { journeyBlueprints, userEvents } from "@hogsend/db";
|
|
16
16
|
import { z } from "@hono/zod-openapi";
|
|
17
|
-
import { count, desc, ilike, inArray, max } from "drizzle-orm";
|
|
17
|
+
import { count, desc, ilike, inArray, max, min } from "drizzle-orm";
|
|
18
18
|
import type { HogsendClient } from "../container.js";
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -25,6 +25,7 @@ import type { HogsendClient } from "../container.js";
|
|
|
25
25
|
export const eventNameEntrySchema = z.object({
|
|
26
26
|
name: z.string(),
|
|
27
27
|
occurrences: z.number(),
|
|
28
|
+
firstSeenAt: z.string().nullable(),
|
|
28
29
|
lastSeenAt: z.string().nullable(),
|
|
29
30
|
// Where the name is declared as a trigger: "journey:<id>" for code journeys,
|
|
30
31
|
// "blueprint:<id> (<status>)" for blueprints.
|
|
@@ -58,6 +59,7 @@ export async function listEventNameVocabulary(opts: {
|
|
|
58
59
|
.select({
|
|
59
60
|
name: userEvents.event,
|
|
60
61
|
occurrences: count(),
|
|
62
|
+
firstSeenAt: min(userEvents.occurredAt),
|
|
61
63
|
lastSeenAt: lastSeen,
|
|
62
64
|
})
|
|
63
65
|
.from(userEvents)
|
|
@@ -79,6 +81,7 @@ export async function listEventNameVocabulary(opts: {
|
|
|
79
81
|
byName.set(row.name, {
|
|
80
82
|
name: row.name,
|
|
81
83
|
occurrences: Number(row.occurrences),
|
|
84
|
+
firstSeenAt: row.firstSeenAt?.toISOString() ?? null,
|
|
82
85
|
lastSeenAt: row.lastSeenAt?.toISOString() ?? null,
|
|
83
86
|
usedBy: [],
|
|
84
87
|
});
|
|
@@ -92,6 +95,7 @@ export async function listEventNameVocabulary(opts: {
|
|
|
92
95
|
const created: EventNameEntry = {
|
|
93
96
|
name,
|
|
94
97
|
occurrences: 0,
|
|
98
|
+
firstSeenAt: null,
|
|
95
99
|
lastSeenAt: null,
|
|
96
100
|
usedBy: [],
|
|
97
101
|
};
|
|
@@ -126,6 +130,7 @@ export async function listEventNameVocabulary(opts: {
|
|
|
126
130
|
.select({
|
|
127
131
|
name: userEvents.event,
|
|
128
132
|
occurrences: count(),
|
|
133
|
+
firstSeenAt: min(userEvents.occurredAt),
|
|
129
134
|
lastSeenAt: max(userEvents.occurredAt),
|
|
130
135
|
})
|
|
131
136
|
.from(userEvents)
|
|
@@ -135,6 +140,7 @@ export async function listEventNameVocabulary(opts: {
|
|
|
135
140
|
const entry = byName.get(row.name);
|
|
136
141
|
if (!entry) continue;
|
|
137
142
|
entry.occurrences = Number(row.occurrences);
|
|
143
|
+
entry.firstSeenAt = row.firstSeenAt?.toISOString() ?? null;
|
|
138
144
|
entry.lastSeenAt = row.lastSeenAt?.toISOString() ?? null;
|
|
139
145
|
}
|
|
140
146
|
}
|
package/src/lib/frequency-cap.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { durationToMs } from "@hogsend/core";
|
|
2
2
|
import type { Database } from "@hogsend/db";
|
|
3
3
|
import { emailSends } from "@hogsend/db";
|
|
4
|
-
import { and, count, eq, gte,
|
|
4
|
+
import { and, count, eq, gte, notInArray } from "drizzle-orm";
|
|
5
5
|
import type { FrequencyCapConfig } from "./email-service-types.js";
|
|
6
6
|
|
|
7
7
|
const DEFAULT_EXEMPT = ["transactional"];
|
|
@@ -28,7 +28,9 @@ export async function countRecentSends(opts: {
|
|
|
28
28
|
const conditions = [
|
|
29
29
|
eq(emailSends.toEmail, to),
|
|
30
30
|
gte(emailSends.createdAt, since),
|
|
31
|
-
|
|
31
|
+
// Neither dispatch failures nor policy-suppressed rows reached the inbox,
|
|
32
|
+
// so neither consumes cap budget.
|
|
33
|
+
notInArray(emailSends.status, ["failed", "suppressed"]),
|
|
32
34
|
];
|
|
33
35
|
if (category !== undefined) {
|
|
34
36
|
conditions.push(eq(emailSends.category, category));
|
|
@@ -5,7 +5,7 @@ import { resolveFunnelTargets } from "./funnel-transitions.js";
|
|
|
5
5
|
import type { Logger } from "./logger.js";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* Event-funnel REPORTING projection
|
|
8
|
+
* Event-funnel REPORTING projection —
|
|
9
9
|
* one first-reach row per (contact, funnel, stage), the raw material for
|
|
10
10
|
* progression counts ("how many reached activated") and velocity ("median
|
|
11
11
|
* time signed_up → activated"). Complements the deals projection the
|
package/src/lib/holdout.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Deterministic holdout assignment
|
|
4
|
+
* Deterministic holdout assignment.
|
|
5
5
|
* NO RNG and NO clock — the replay law: journey tasks replay-from-top on
|
|
6
6
|
* eviction/crash, so the same (userId, journeyId, salt) must bucket
|
|
7
7
|
* identically on every evaluation, forever. sha256's first 4 bytes give a
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Database } from "@hogsend/db";
|
|
2
2
|
import { emailSends, journeyStates, smsSends } from "@hogsend/db";
|
|
3
|
-
import { and, eq, gte, ne } from "drizzle-orm";
|
|
3
|
+
import { and, eq, gte, ne, notInArray } from "drizzle-orm";
|
|
4
4
|
import type { JourneyBoundary } from "../journeys/journey-boundary.js";
|
|
5
5
|
import { recordOnce } from "../journeys/record-once.js";
|
|
6
6
|
|
|
@@ -29,7 +29,9 @@ export async function hasRecentJourneySend(opts: {
|
|
|
29
29
|
eq(journeyStates.journeyId, opts.journeyId),
|
|
30
30
|
eq(emailSends.toEmail, opts.to),
|
|
31
31
|
gte(emailSends.createdAt, opts.since),
|
|
32
|
-
|
|
32
|
+
// Failed and policy-suppressed rows never reached the inbox — neither
|
|
33
|
+
// counts as a recent journey touch for the min-gap.
|
|
34
|
+
notInArray(emailSends.status, ["failed", "suppressed"]),
|
|
33
35
|
),
|
|
34
36
|
)
|
|
35
37
|
.limit(1);
|
package/src/lib/lift-stats.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Holdout lift statistics
|
|
3
|
-
*
|
|
2
|
+
* Holdout lift statistics: Bayesian beta-binomial with a uniform Beta(1,1)
|
|
3
|
+
* prior — the
|
|
4
4
|
* lean, honest method at self-hosted volumes. Win probability is
|
|
5
5
|
* P(treatment rate > control rate) under independent posteriors
|
|
6
6
|
* Beta(k+1, n−k+1), computed by DETERMINISTIC numeric integration (no
|
package/src/lib/tracked.ts
CHANGED
|
@@ -213,7 +213,12 @@ async function sendTrackedEmailInner<K extends TemplateName>(
|
|
|
213
213
|
campaignId: options.campaignId,
|
|
214
214
|
userId: options.userId,
|
|
215
215
|
userEmail: options.userEmail ?? options.to,
|
|
216
|
-
status:
|
|
216
|
+
// First-class "suppressed" status: a provider dispatch failure
|
|
217
|
+
// releases its idempotency key (catch block below), making it
|
|
218
|
+
// byte-identical to this row EXCEPT for the status — so campaign
|
|
219
|
+
// stats can only tell "gated by policy" from "failed at dispatch"
|
|
220
|
+
// if suppression names itself here.
|
|
221
|
+
status: "suppressed",
|
|
217
222
|
// A suppressed send does NOT consume the idempotency key — leaving it
|
|
218
223
|
// unset lets a later retry (e.g. after the recipient re-subscribes)
|
|
219
224
|
// actually attempt the send rather than dedup to the suppressed row.
|
|
@@ -398,7 +403,9 @@ async function sendTrackedEmailInner<K extends TemplateName>(
|
|
|
398
403
|
campaignId: options.campaignId,
|
|
399
404
|
userId: options.userId,
|
|
400
405
|
userEmail: options.userEmail ?? options.to,
|
|
401
|
-
|
|
406
|
+
// Policy-gated like suppression (the provider is never reached), so it
|
|
407
|
+
// shares the "suppressed" status; metadata.testMode marks the cause.
|
|
408
|
+
status: "suppressed",
|
|
402
409
|
metadata: { testMode: true, originalTo: options.to },
|
|
403
410
|
})
|
|
404
411
|
.returning({ id: emailSends.id });
|
|
@@ -6,8 +6,7 @@ import { backfillAttributionBatch } from "../../lib/attribution-backfill.js";
|
|
|
6
6
|
import { getConversionRegistry } from "../../lib/conversions.js";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Admin attribution API
|
|
10
|
-
* docs/attribution-impact-plan.md §1.5) — reads over the credit ledger.
|
|
9
|
+
* Admin attribution API — reads over the credit ledger.
|
|
11
10
|
* Every model was persisted at conversion time, so model comparison is a
|
|
12
11
|
* GROUP BY, not a re-computation — and since the ledger carries
|
|
13
12
|
* journey/campaign/template scope, "revenue by journey" is too.
|
|
@@ -9,12 +9,19 @@
|
|
|
9
9
|
import { type CampaignStep, durationToMs } from "@hogsend/core";
|
|
10
10
|
import { campaigns, emailSends } from "@hogsend/db";
|
|
11
11
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
12
|
-
import { and, count, desc, eq, inArray, like, max, sql } from "drizzle-orm";
|
|
13
|
-
import type { AppEnv } from "../../app.js";
|
|
14
12
|
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
and,
|
|
14
|
+
count,
|
|
15
|
+
desc,
|
|
16
|
+
eq,
|
|
17
|
+
inArray,
|
|
18
|
+
like,
|
|
19
|
+
max,
|
|
20
|
+
type SQL,
|
|
21
|
+
sql,
|
|
22
|
+
} from "drizzle-orm";
|
|
23
|
+
import type { AppEnv } from "../../app.js";
|
|
24
|
+
import { campaignStepSendKeyPattern } from "../../lib/campaign-send-key.js";
|
|
18
25
|
import { errorSchema } from "../../lib/schemas.js";
|
|
19
26
|
import { campaignSchema, serializeCampaign } from "../campaigns/index.js";
|
|
20
27
|
|
|
@@ -68,9 +75,12 @@ const getRouteDef = createRoute({
|
|
|
68
75
|
/**
|
|
69
76
|
* One row of the per-step breakdown for a multi-step campaign: the step's
|
|
70
77
|
* identity (`kind` + `templateKey` for send steps, `durationMs` for wait
|
|
71
|
-
* steps) plus the same engagement aggregate as the campaign level
|
|
72
|
-
* the
|
|
73
|
-
*
|
|
78
|
+
* steps) plus the same engagement aggregate as the campaign level. The step
|
|
79
|
+
* number only lives in the `campaign:<id>:<step>:%` idempotency-key format,
|
|
80
|
+
* so step rows anchor on that pattern (scoped to the campaign_id FK first) —
|
|
81
|
+
* which also means suppressed sends (no key) can never attribute to a step,
|
|
82
|
+
* so step rows carry no `skipped` (it lives at the campaign level only).
|
|
83
|
+
* Wait steps deliver nothing, so their counts are zeroed.
|
|
74
84
|
*/
|
|
75
85
|
const campaignStepStatsSchema = z.object({
|
|
76
86
|
index: z.number(),
|
|
@@ -89,13 +99,16 @@ const campaignStepStatsSchema = z.object({
|
|
|
89
99
|
|
|
90
100
|
/**
|
|
91
101
|
* Post-dispatch engagement for one campaign, aggregated from the `email_sends`
|
|
92
|
-
* rows the blast wrote
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
102
|
+
* rows the blast wrote — attributed via the indexed `campaign_id` column
|
|
103
|
+
* (stamped at send time, backfilled from legacy idempotency keys by migration
|
|
104
|
+
* 0051), NOT by parsing the key: suppressed sends write no key at all, so a
|
|
105
|
+
* key scan can never see them. Complements the counters on the campaign row
|
|
106
|
+
* itself: the row knows sent/skipped/failed at dispatch time, this knows what
|
|
107
|
+
* happened to the mail AFTERWARDS (delivered/opened/clicked/bounced/complained
|
|
108
|
+
* via first-party tracking + provider webhooks) plus the policy-suppressed
|
|
109
|
+
* rows (status `suppressed`) as `skipped`. Multi-step campaigns additionally
|
|
110
|
+
* carry `steps` — one entry per step, in step order (the campaign-level
|
|
111
|
+
* numbers are a superset of every step's).
|
|
99
112
|
*/
|
|
100
113
|
const campaignStatsSchema = z.object({
|
|
101
114
|
sends: z.number(),
|
|
@@ -105,6 +118,7 @@ const campaignStatsSchema = z.object({
|
|
|
105
118
|
bounced: z.number(),
|
|
106
119
|
complained: z.number(),
|
|
107
120
|
failed: z.number(),
|
|
121
|
+
skipped: z.number(),
|
|
108
122
|
lastSentAt: z.string().nullable(),
|
|
109
123
|
steps: z.array(campaignStepStatsSchema).optional(),
|
|
110
124
|
});
|
|
@@ -213,15 +227,22 @@ export const adminCampaignsRouter = new OpenAPIHono<AppEnv>()
|
|
|
213
227
|
}
|
|
214
228
|
|
|
215
229
|
// count(column) counts non-NULL values, so each engagement timestamp
|
|
216
|
-
// doubles as its own tally.
|
|
217
|
-
//
|
|
218
|
-
//
|
|
230
|
+
// doubles as its own tally. Attribution is the indexed `campaign_id`
|
|
231
|
+
// column (stamped on every campaign row including suppressed sends;
|
|
232
|
+
// legacy rows backfilled by migration 0051). The status column carries
|
|
233
|
+
// the dispatch verdict: policy-gated rows are `suppressed` (the `skipped`
|
|
234
|
+
// bucket), everything else was a dispatch attempt (`sends`), and `failed`
|
|
235
|
+
// means failed AT dispatch — a provider failure releases its idempotency
|
|
236
|
+
// key, so key-nullness deliberately plays no part here. Shared by the
|
|
219
237
|
// campaign-level aggregate and the per-step breakdown.
|
|
220
|
-
const aggregate = async (
|
|
238
|
+
const aggregate = async (where: SQL | undefined) => {
|
|
221
239
|
const agg = (
|
|
222
240
|
await db
|
|
223
241
|
.select({
|
|
224
|
-
sends:
|
|
242
|
+
sends:
|
|
243
|
+
sql<number>`count(*) filter (where ${emailSends.status} <> 'suppressed')`.mapWith(
|
|
244
|
+
Number,
|
|
245
|
+
),
|
|
225
246
|
delivered: count(emailSends.deliveredAt),
|
|
226
247
|
opened: count(emailSends.openedAt),
|
|
227
248
|
clicked: count(emailSends.clickedAt),
|
|
@@ -231,10 +252,14 @@ export const adminCampaignsRouter = new OpenAPIHono<AppEnv>()
|
|
|
231
252
|
sql<number>`count(*) filter (where ${emailSends.status} = 'failed')`.mapWith(
|
|
232
253
|
Number,
|
|
233
254
|
),
|
|
255
|
+
skipped:
|
|
256
|
+
sql<number>`count(*) filter (where ${emailSends.status} = 'suppressed')`.mapWith(
|
|
257
|
+
Number,
|
|
258
|
+
),
|
|
234
259
|
lastSentAt: max(emailSends.sentAt),
|
|
235
260
|
})
|
|
236
261
|
.from(emailSends)
|
|
237
|
-
.where(
|
|
262
|
+
.where(where)
|
|
238
263
|
)[0];
|
|
239
264
|
return {
|
|
240
265
|
sends: agg?.sends ?? 0,
|
|
@@ -244,21 +269,23 @@ export const adminCampaignsRouter = new OpenAPIHono<AppEnv>()
|
|
|
244
269
|
bounced: agg?.bounced ?? 0,
|
|
245
270
|
complained: agg?.complained ?? 0,
|
|
246
271
|
failed: agg?.failed ?? 0,
|
|
272
|
+
skipped: agg?.skipped ?? 0,
|
|
247
273
|
lastSentAt: agg?.lastSentAt ? agg.lastSentAt.toISOString() : null,
|
|
248
274
|
};
|
|
249
275
|
};
|
|
250
276
|
|
|
251
|
-
//
|
|
252
|
-
//
|
|
253
|
-
// legacy single-send rows.
|
|
254
|
-
const campaignLevel = await aggregate(
|
|
277
|
+
// Campaign-level: the FK matches every row of both key formats AND the
|
|
278
|
+
// keyless suppressed rows, so these numbers are format-agnostic and
|
|
279
|
+
// unchanged for legacy single-send rows.
|
|
280
|
+
const campaignLevel = await aggregate(eq(emailSends.campaignId, id));
|
|
255
281
|
|
|
256
282
|
// Per-step breakdown, only when the row carries a steps blob (NULL =
|
|
257
|
-
// legacy single-send — no `steps` in the response).
|
|
258
|
-
//
|
|
259
|
-
//
|
|
260
|
-
//
|
|
261
|
-
//
|
|
283
|
+
// legacy single-send — no `steps` in the response). The step number only
|
|
284
|
+
// exists inside the key, so send-step aggregates still filter on the
|
|
285
|
+
// step-scoped pattern (`campaign:<id>:<k>:%` — multi-step campaigns key
|
|
286
|
+
// ALL steps including 0 that way), anchored on the indexed FK first; wait
|
|
287
|
+
// steps deliver nothing. The step queries are independent, so they run
|
|
288
|
+
// concurrently.
|
|
262
289
|
const blob = row.steps;
|
|
263
290
|
if (!blob) {
|
|
264
291
|
return c.json(campaignLevel, 200);
|
|
@@ -267,18 +294,30 @@ export const adminCampaignsRouter = new OpenAPIHono<AppEnv>()
|
|
|
267
294
|
// db cannot import @hogsend/core, so the blob's elements are opaque
|
|
268
295
|
// Record<string, unknown> there; the engine owns the narrowing.
|
|
269
296
|
const defs = blob.steps as unknown as CampaignStep[];
|
|
270
|
-
const steps: z.infer<typeof campaignStepStatsSchema>[] =
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
297
|
+
const steps: z.infer<typeof campaignStepStatsSchema>[] = await Promise.all(
|
|
298
|
+
defs.map(async (step, index) => {
|
|
299
|
+
if (step.kind === "send") {
|
|
300
|
+
// Suppressed rows write no key, so they can never match the
|
|
301
|
+
// step-scoped pattern — the always-zero `skipped` is dropped from
|
|
302
|
+
// step rows rather than reported as a dead field.
|
|
303
|
+
const { skipped: _campaignOnly, ...stepStats } = await aggregate(
|
|
304
|
+
and(
|
|
305
|
+
eq(emailSends.campaignId, id),
|
|
306
|
+
like(
|
|
307
|
+
emailSends.idempotencyKey,
|
|
308
|
+
campaignStepSendKeyPattern(id, index),
|
|
309
|
+
),
|
|
310
|
+
),
|
|
311
|
+
);
|
|
312
|
+
return {
|
|
313
|
+
index,
|
|
314
|
+
kind: step.kind,
|
|
315
|
+
templateKey: step.template,
|
|
316
|
+
durationMs: null,
|
|
317
|
+
...stepStats,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
282
321
|
index,
|
|
283
322
|
kind: step.kind,
|
|
284
323
|
templateKey: null,
|
|
@@ -291,9 +330,9 @@ export const adminCampaignsRouter = new OpenAPIHono<AppEnv>()
|
|
|
291
330
|
complained: 0,
|
|
292
331
|
failed: 0,
|
|
293
332
|
lastSentAt: null,
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
333
|
+
};
|
|
334
|
+
}),
|
|
335
|
+
);
|
|
297
336
|
|
|
298
337
|
return c.json({ ...campaignLevel, steps }, 200);
|
|
299
338
|
})
|
|
@@ -4,7 +4,7 @@ import { and, asc, count, desc, eq, sql } from "drizzle-orm";
|
|
|
4
4
|
import type { AppEnv } from "../../app.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Admin conversions API
|
|
7
|
+
* Admin conversions API — fired
|
|
8
8
|
* conversion points and their per-destination delivery state. Answers the
|
|
9
9
|
* operator question "did the ad platform actually receive the sale?".
|
|
10
10
|
*/
|
|
@@ -17,7 +17,7 @@ import type { AnyPgColumn } from "drizzle-orm/pg-core";
|
|
|
17
17
|
import type { AppEnv } from "../../app.js";
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
* Admin deals API
|
|
20
|
+
* Admin deals API — the ledger view
|
|
21
21
|
* over the `deals` projection: filterable list for the Studio pipeline board
|
|
22
22
|
* + the revenue stats block (per-currency, never cross-summed). Stages are
|
|
23
23
|
* per-funnel ladders (`client.funnels`), not a fixed enum; the `funnel`
|
|
@@ -140,6 +140,8 @@ const statsSchema = z.object({
|
|
|
140
140
|
),
|
|
141
141
|
/** Mean sold-deal cycle time in hours (created → soldAt), lifetime. */
|
|
142
142
|
avgTimeToCloseHours: z.number().nullable(),
|
|
143
|
+
/** Distinct deal providers in this funnel — the list filter's vocabulary. */
|
|
144
|
+
providers: z.array(z.string()),
|
|
143
145
|
});
|
|
144
146
|
|
|
145
147
|
const statsRoute = createRoute({
|
|
@@ -389,39 +391,45 @@ export const adminDealsRouter = new OpenAPIHono<AppEnv>()
|
|
|
389
391
|
Date.now() - 30 * 24 * 60 * 60 * 1000,
|
|
390
392
|
).toISOString();
|
|
391
393
|
|
|
392
|
-
const [stageRows, rankRows, currencyRows, cycleRows] =
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
394
|
+
const [stageRows, rankRows, currencyRows, cycleRows, providerRows] =
|
|
395
|
+
await Promise.all([
|
|
396
|
+
db
|
|
397
|
+
.select({ stage: deals.canonicalStage, n: count() })
|
|
398
|
+
.from(deals)
|
|
399
|
+
.where(scopeWhere)
|
|
400
|
+
.groupBy(deals.canonicalStage),
|
|
401
|
+
db
|
|
402
|
+
.select({ rank: deals.stageRank, n: count() })
|
|
403
|
+
.from(deals)
|
|
404
|
+
.where(scopeWhere)
|
|
405
|
+
.groupBy(deals.stageRank),
|
|
406
|
+
db
|
|
407
|
+
.select({
|
|
408
|
+
currency: deals.currency,
|
|
409
|
+
soldRevenue30d: sql<number>`coalesce(sum(${deals.value}) filter (where ${deals.soldAt} >= ${thirtyDaysAgo}::timestamptz), 0)::float8`,
|
|
410
|
+
soldRevenueLifetime: sql<number>`coalesce(sum(${deals.value}) filter (where ${deals.soldAt} is not null), 0)::float8`,
|
|
411
|
+
soldCount30d: sql<number>`count(*) filter (where ${deals.soldAt} >= ${thirtyDaysAgo}::timestamptz)::int`,
|
|
412
|
+
soldCountLifetime: sql<number>`count(*) filter (where ${deals.soldAt} is not null)::int`,
|
|
413
|
+
openPipelineValue: sql<number>`coalesce(sum(${deals.value}) filter (where ${deals.soldAt} is null and ${deals.canonicalStage} != 'lost'), 0)::float8`,
|
|
414
|
+
openPipelineCount: sql<number>`count(*) filter (where ${deals.soldAt} is null and ${deals.canonicalStage} != 'lost')::int`,
|
|
415
|
+
})
|
|
416
|
+
.from(deals)
|
|
417
|
+
.where(scopeWhere)
|
|
418
|
+
.groupBy(deals.currency),
|
|
419
|
+
db
|
|
420
|
+
.select({
|
|
421
|
+
avgHours: sql<
|
|
422
|
+
number | null
|
|
423
|
+
>`avg(extract(epoch from (${deals.soldAt} - ${deals.createdAt})) / 3600)::float8`,
|
|
424
|
+
})
|
|
425
|
+
.from(deals)
|
|
426
|
+
.where(and(sql`${deals.soldAt} is not null`, ...scope)),
|
|
427
|
+
db
|
|
428
|
+
.selectDistinct({ provider: deals.provider })
|
|
429
|
+
.from(deals)
|
|
430
|
+
.where(scopeWhere)
|
|
431
|
+
.orderBy(asc(deals.provider)),
|
|
432
|
+
]);
|
|
425
433
|
|
|
426
434
|
const stageOrder = [...ladder.stages, "lost"];
|
|
427
435
|
const stages: Record<string, number> = {};
|
|
@@ -466,6 +474,7 @@ export const adminDealsRouter = new OpenAPIHono<AppEnv>()
|
|
|
466
474
|
}))
|
|
467
475
|
.sort((a, b) => b.soldRevenueLifetime - a.soldRevenueLifetime),
|
|
468
476
|
avgTimeToCloseHours: cycleRows[0]?.avgHours ?? null,
|
|
477
|
+
providers: providerRows.map((row) => row.provider),
|
|
469
478
|
},
|
|
470
479
|
200,
|
|
471
480
|
);
|
|
@@ -17,12 +17,10 @@ import {
|
|
|
17
17
|
inArray,
|
|
18
18
|
isNotNull,
|
|
19
19
|
isNull,
|
|
20
|
-
like,
|
|
21
20
|
lte,
|
|
22
21
|
or,
|
|
23
22
|
} from "drizzle-orm";
|
|
24
23
|
import type { AppEnv } from "../../app.js";
|
|
25
|
-
import { campaignSendKeyPattern } from "../../lib/campaign-send-key.js";
|
|
26
24
|
|
|
27
25
|
const emailSchema = z.object({
|
|
28
26
|
id: z.string(),
|
|
@@ -142,7 +140,7 @@ const listRoute = createRoute({
|
|
|
142
140
|
* Only the sends of one campaign — matched on the deterministic
|
|
143
141
|
* `campaign:<id>:<email>` idempotency key the blast wrote per recipient.
|
|
144
142
|
*/
|
|
145
|
-
campaignId: z.string().optional(),
|
|
143
|
+
campaignId: z.string().uuid().optional(),
|
|
146
144
|
userId: z.string().optional(),
|
|
147
145
|
category: z.string().optional(),
|
|
148
146
|
engagement: z
|
|
@@ -278,10 +276,10 @@ export const emailsRouter = new OpenAPIHono<AppEnv>()
|
|
|
278
276
|
if (status) conditions.push(eq(emailSends.status, status));
|
|
279
277
|
if (category) conditions.push(eq(emailSends.category, category));
|
|
280
278
|
if (journeyId) conditions.push(eq(journeyStates.journeyId, journeyId));
|
|
279
|
+
// The indexed campaign_id column is the attribution — a key scan would
|
|
280
|
+
// miss suppressed sends, which write no idempotency key at all.
|
|
281
281
|
if (campaignId) {
|
|
282
|
-
conditions.push(
|
|
283
|
-
like(emailSends.idempotencyKey, campaignSendKeyPattern(campaignId)),
|
|
284
|
-
);
|
|
282
|
+
conditions.push(eq(emailSends.campaignId, campaignId));
|
|
285
283
|
}
|
|
286
284
|
// Match the denormalized identity OR the journey-state join, so journeyless
|
|
287
285
|
// sends (which only carry the denormalized userId) are still filterable.
|
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import { contacts, userEvents } from "@hogsend/db";
|
|
2
2
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
and,
|
|
5
|
+
count,
|
|
6
|
+
desc,
|
|
7
|
+
eq,
|
|
8
|
+
gte,
|
|
9
|
+
isNotNull,
|
|
10
|
+
isNull,
|
|
11
|
+
lte,
|
|
12
|
+
max,
|
|
13
|
+
min,
|
|
14
|
+
or,
|
|
15
|
+
sql,
|
|
16
|
+
} from "drizzle-orm";
|
|
4
17
|
import type { AppEnv } from "../../app.js";
|
|
5
18
|
import type { HogsendClient } from "../../container.js";
|
|
6
19
|
import {
|
|
@@ -169,6 +182,59 @@ const namesRoute = createRoute({
|
|
|
169
182
|
},
|
|
170
183
|
});
|
|
171
184
|
|
|
185
|
+
/**
|
|
186
|
+
* One-line explanations for the engine-stamped ingest origins, so the Studio
|
|
187
|
+
* source picker can say what a source IS rather than just echo its slug.
|
|
188
|
+
* Anything not listed here and not a registered connector is "observed" —
|
|
189
|
+
* sources are an open vocabulary exactly like event names.
|
|
190
|
+
*/
|
|
191
|
+
const BUILTIN_SOURCES: Record<string, string> = {
|
|
192
|
+
api: "Server-side ingest — the secret-key /v1/events endpoint and SDKs.",
|
|
193
|
+
inapp: "Browser SDK ingest (publishable key).",
|
|
194
|
+
studio: "Fired from the Studio (debug drawer, admin tools).",
|
|
195
|
+
journey: "Emitted by a journey via ctx.trigger().",
|
|
196
|
+
connector: "Inbound connector engagement (Discord, Telegram, …).",
|
|
197
|
+
tracking: "First-party email tracking — opens, clicks, semantic actions.",
|
|
198
|
+
bucket: "Bucket entry/exit transitions.",
|
|
199
|
+
import: "Contact import jobs.",
|
|
200
|
+
sms: "SMS pipeline — delivery status, link clicks, inbound messages.",
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const sourceEntrySchema = z.object({
|
|
204
|
+
name: z.string(),
|
|
205
|
+
occurrences: z.number(),
|
|
206
|
+
firstSeenAt: z.string().nullable(),
|
|
207
|
+
lastSeenAt: z.string().nullable(),
|
|
208
|
+
// "connector" = a registered inbound connector (label = its display name),
|
|
209
|
+
// "builtin" = an engine-stamped origin (label = what it means),
|
|
210
|
+
// "observed" = seen in the event store but not otherwise known.
|
|
211
|
+
kind: z.enum(["connector", "builtin", "observed"]),
|
|
212
|
+
label: z.string().nullable(),
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const sourcesRoute = createRoute({
|
|
216
|
+
method: "get",
|
|
217
|
+
path: "/sources",
|
|
218
|
+
tags: ["Admin — Events"],
|
|
219
|
+
summary: "List event sources (observed + registered)",
|
|
220
|
+
description:
|
|
221
|
+
"Best-effort source vocabulary for the events filter. Merges sources " +
|
|
222
|
+
"actually observed on stored events (with occurrence counts and " +
|
|
223
|
+
"first/last seen) with registered inbound connectors that haven't " +
|
|
224
|
+
"fired yet (zero-backfilled). Sources are an open vocabulary — any " +
|
|
225
|
+
"other value is also valid.",
|
|
226
|
+
responses: {
|
|
227
|
+
200: {
|
|
228
|
+
content: {
|
|
229
|
+
"application/json": {
|
|
230
|
+
schema: z.object({ sources: z.array(sourceEntrySchema) }),
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
description: "Merged observed + registered source vocabulary",
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
|
|
172
238
|
const exitSchema = z.object({
|
|
173
239
|
journeyId: z.string(),
|
|
174
240
|
stateId: z.string(),
|
|
@@ -292,6 +358,67 @@ export const eventsRouter = new OpenAPIHono<AppEnv>()
|
|
|
292
358
|
});
|
|
293
359
|
return c.json({ note, events }, 200);
|
|
294
360
|
})
|
|
361
|
+
// Registered before getRoute so the literal "sources" is never captured as
|
|
362
|
+
// an {id}. One grouped scan over the indexed source column (sources number
|
|
363
|
+
// in the dozens at most), then registered-connector + builtin labeling.
|
|
364
|
+
.openapi(sourcesRoute, async (c) => {
|
|
365
|
+
const container = c.get("container");
|
|
366
|
+
const { db } = container;
|
|
367
|
+
|
|
368
|
+
const observed = await db
|
|
369
|
+
.select({
|
|
370
|
+
name: userEvents.source,
|
|
371
|
+
occurrences: count(),
|
|
372
|
+
firstSeenAt: min(userEvents.occurredAt),
|
|
373
|
+
lastSeenAt: max(userEvents.occurredAt),
|
|
374
|
+
})
|
|
375
|
+
.from(userEvents)
|
|
376
|
+
.where(isNotNull(userEvents.source))
|
|
377
|
+
.groupBy(userEvents.source)
|
|
378
|
+
.orderBy(desc(count()));
|
|
379
|
+
|
|
380
|
+
const connectors = new Map(
|
|
381
|
+
container.connectorRegistry
|
|
382
|
+
.getAll()
|
|
383
|
+
.map((conn) => [conn.meta.id, conn.meta.name ?? conn.meta.id]),
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
const entries = observed
|
|
387
|
+
.filter((row) => row.name !== null)
|
|
388
|
+
.map((row) => {
|
|
389
|
+
const name = row.name as string;
|
|
390
|
+
const connectorLabel = connectors.get(name);
|
|
391
|
+
return {
|
|
392
|
+
name,
|
|
393
|
+
occurrences: row.occurrences,
|
|
394
|
+
firstSeenAt: row.firstSeenAt?.toISOString() ?? null,
|
|
395
|
+
lastSeenAt: row.lastSeenAt?.toISOString() ?? null,
|
|
396
|
+
kind: connectorLabel
|
|
397
|
+
? ("connector" as const)
|
|
398
|
+
: BUILTIN_SOURCES[name]
|
|
399
|
+
? ("builtin" as const)
|
|
400
|
+
: ("observed" as const),
|
|
401
|
+
label: connectorLabel ?? BUILTIN_SOURCES[name] ?? null,
|
|
402
|
+
};
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
// Zero-backfill registered connectors that haven't fired yet, so a
|
|
406
|
+
// freshly-wired source is pickable before its first event arrives.
|
|
407
|
+
const seen = new Set(entries.map((e) => e.name));
|
|
408
|
+
for (const [id, label] of connectors) {
|
|
409
|
+
if (seen.has(id)) continue;
|
|
410
|
+
entries.push({
|
|
411
|
+
name: id,
|
|
412
|
+
occurrences: 0,
|
|
413
|
+
firstSeenAt: null,
|
|
414
|
+
lastSeenAt: null,
|
|
415
|
+
kind: "connector" as const,
|
|
416
|
+
label,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return c.json({ sources: entries }, 200);
|
|
421
|
+
})
|
|
295
422
|
.openapi(getRoute, async (c) => {
|
|
296
423
|
const { db } = c.get("container");
|
|
297
424
|
const { id } = c.req.valid("param");
|
|
@@ -575,6 +575,28 @@ const listRoute = createRoute({
|
|
|
575
575
|
},
|
|
576
576
|
});
|
|
577
577
|
|
|
578
|
+
const listTypesRoute = createRoute({
|
|
579
|
+
method: "get",
|
|
580
|
+
path: "/types",
|
|
581
|
+
tags: ["Admin — Groups"],
|
|
582
|
+
summary: "List distinct group types",
|
|
583
|
+
responses: {
|
|
584
|
+
200: {
|
|
585
|
+
content: {
|
|
586
|
+
"application/json": {
|
|
587
|
+
schema: z.object({
|
|
588
|
+
types: z.array(
|
|
589
|
+
z.object({ groupType: z.string(), count: z.number() }),
|
|
590
|
+
),
|
|
591
|
+
}),
|
|
592
|
+
},
|
|
593
|
+
},
|
|
594
|
+
description:
|
|
595
|
+
"Distinct group types across live groups with per-type counts, largest first",
|
|
596
|
+
},
|
|
597
|
+
},
|
|
598
|
+
});
|
|
599
|
+
|
|
578
600
|
const getRoute = createRoute({
|
|
579
601
|
method: "get",
|
|
580
602
|
path: "/{groupType}/{groupKey}",
|
|
@@ -697,6 +719,18 @@ export const groupsRouter = new OpenAPIHono<AppEnv>()
|
|
|
697
719
|
200,
|
|
698
720
|
);
|
|
699
721
|
})
|
|
722
|
+
// Registered before the `/{groupType}/{groupKey}` param route so the
|
|
723
|
+
// literal "types" isn't captured as a groupType.
|
|
724
|
+
.openapi(listTypesRoute, async (c) => {
|
|
725
|
+
const { db } = c.get("container");
|
|
726
|
+
const rows = await db
|
|
727
|
+
.select({ groupType: groups.groupType, count: count() })
|
|
728
|
+
.from(groups)
|
|
729
|
+
.where(isNull(groups.deletedAt))
|
|
730
|
+
.groupBy(groups.groupType)
|
|
731
|
+
.orderBy(desc(count()), asc(groups.groupType));
|
|
732
|
+
return c.json({ types: rows }, 200);
|
|
733
|
+
})
|
|
700
734
|
.openapi(listMembersRoute, async (c) => {
|
|
701
735
|
const { db } = c.get("container");
|
|
702
736
|
const { groupType, groupKey } = c.req.valid("param");
|
|
@@ -353,12 +353,15 @@ export const impactOverviewRouter = new OpenAPIHono<AppEnv>().openapi(
|
|
|
353
353
|
);
|
|
354
354
|
|
|
355
355
|
// (d) Campaigns — correlational only, ACTIVITY-windowed enumeration:
|
|
356
|
-
// ids come from the in-window email_sends
|
|
356
|
+
// ids come from the in-window email_sends campaign_id rollup ∪ in-window
|
|
357
357
|
// attribution_credits.campaign_id (NOT "newest 50 created in window" —
|
|
358
358
|
// that drops older-but-active multi-step/scheduled campaigns whose
|
|
359
|
-
// sends fall inside the window). Cap 50 by send volume desc.
|
|
360
|
-
|
|
361
|
-
|
|
359
|
+
// sends fall inside the window). Cap 50 by send volume desc. The
|
|
360
|
+
// indexed campaign_id column is the attribution (stamped at send time,
|
|
361
|
+
// backfilled from legacy idempotency keys by migration 0051) — never
|
|
362
|
+
// key parsing. Policy-suppressed rows (status 'suppressed') carry the FK
|
|
363
|
+
// but were never dispatched, so they stay out of the funnel; provider
|
|
364
|
+
// failures (status 'failed', key released) stay IN it as attempts.
|
|
362
365
|
const [campaignSendRows, campaignCreditRows] = await Promise.all([
|
|
363
366
|
db.execute<{
|
|
364
367
|
campaign_id: string;
|
|
@@ -367,13 +370,14 @@ export const impactOverviewRouter = new OpenAPIHono<AppEnv>().openapi(
|
|
|
367
370
|
opened: number;
|
|
368
371
|
clicked: number;
|
|
369
372
|
}>(sql`
|
|
370
|
-
select
|
|
373
|
+
select campaign_id::text as campaign_id,
|
|
371
374
|
count(*)::int as sends,
|
|
372
375
|
count(delivered_at)::int as delivered,
|
|
373
376
|
count(opened_at)::int as opened,
|
|
374
377
|
count(clicked_at)::int as clicked
|
|
375
378
|
from email_sends
|
|
376
|
-
where
|
|
379
|
+
where campaign_id is not null
|
|
380
|
+
and status <> 'suppressed'
|
|
377
381
|
and created_at >= ${sinceTs}
|
|
378
382
|
group by 1
|
|
379
383
|
`),
|
|
@@ -395,19 +399,15 @@ export const impactOverviewRouter = new OpenAPIHono<AppEnv>().openapi(
|
|
|
395
399
|
]);
|
|
396
400
|
|
|
397
401
|
const sendsByCampaign = new Map(
|
|
398
|
-
[...campaignSendRows]
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
r.
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
opened: Number(r.opened),
|
|
408
|
-
clicked: Number(r.clicked),
|
|
409
|
-
},
|
|
410
|
-
]),
|
|
402
|
+
[...campaignSendRows].map((r) => [
|
|
403
|
+
r.campaign_id,
|
|
404
|
+
{
|
|
405
|
+
sends: Number(r.sends),
|
|
406
|
+
delivered: Number(r.delivered),
|
|
407
|
+
opened: Number(r.opened),
|
|
408
|
+
clicked: Number(r.clicked),
|
|
409
|
+
},
|
|
410
|
+
]),
|
|
411
411
|
);
|
|
412
412
|
const creditsByCampaign = new Map<
|
|
413
413
|
string,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CANONICAL_STAGES, flagTargetingSchema } from "@hogsend/core";
|
|
2
2
|
import { campaigns, contacts } from "@hogsend/db";
|
|
3
|
+
import { getTemplateNames } from "@hogsend/email";
|
|
3
4
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
4
5
|
import { desc, isNull, sql } from "drizzle-orm";
|
|
5
6
|
import type { AppEnv } from "../../app.js";
|
|
@@ -93,6 +94,7 @@ const catalogSchema = z.object({
|
|
|
93
94
|
dealStages: z.array(z.string()),
|
|
94
95
|
events: z.array(eventNameEntrySchema),
|
|
95
96
|
campaigns: z.array(idNameSchema),
|
|
97
|
+
templates: z.array(z.string()),
|
|
96
98
|
});
|
|
97
99
|
|
|
98
100
|
const catalogRoute = createRoute({
|
|
@@ -146,7 +148,7 @@ const countRoute = createRoute({
|
|
|
146
148
|
|
|
147
149
|
export const targetingRouter = new OpenAPIHono<AppEnv>()
|
|
148
150
|
.openapi(catalogRoute, async (c) => {
|
|
149
|
-
const { db, registry, bucketRegistry } = c.get("container");
|
|
151
|
+
const { db, registry, bucketRegistry, templates } = c.get("container");
|
|
150
152
|
|
|
151
153
|
// Distinct top-level property keys across a bounded SAMPLE of the most
|
|
152
154
|
// recently-updated live contacts, sorted + capped. Sampling first (inner
|
|
@@ -191,6 +193,7 @@ export const targetingRouter = new OpenAPIHono<AppEnv>()
|
|
|
191
193
|
dealStages: DEAL_STAGES,
|
|
192
194
|
events: events.events,
|
|
193
195
|
campaigns: campaignRows,
|
|
196
|
+
templates: getTemplateNames(templates),
|
|
194
197
|
},
|
|
195
198
|
200,
|
|
196
199
|
);
|
|
@@ -9,7 +9,7 @@ import { hatchet } from "../lib/hatchet.js";
|
|
|
9
9
|
import { createLogger, type Logger } from "../lib/logger.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* CRM reconciliation poll
|
|
12
|
+
* CRM reconciliation poll — the pull
|
|
13
13
|
* half of the webhook+poll hybrid. Every provider implementing `poll` is
|
|
14
14
|
* walked on a cron: read the persisted cursor, pull changed deals, feed them
|
|
15
15
|
* through the SAME `ingestCrmStageEvents` sink the webhook route uses (the
|
package/src/middleware/auth.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { Context } from "hono";
|
|
2
|
-
import { createMiddleware } from "hono/factory";
|
|
3
|
-
import type { AppEnv } from "../app.js";
|
|
4
|
-
|
|
5
|
-
async function resolveSession(c: Context<AppEnv>) {
|
|
6
|
-
const { auth } = c.get("container");
|
|
7
|
-
const session = await auth.api.getSession({ headers: c.req.raw.headers });
|
|
8
|
-
c.set("user", session?.user ?? null);
|
|
9
|
-
c.set("session", session?.session ?? null);
|
|
10
|
-
return session;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const sessionMiddleware = createMiddleware<AppEnv>(async (c, next) => {
|
|
14
|
-
await resolveSession(c);
|
|
15
|
-
return next();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
export const requireAuth = createMiddleware<AppEnv>(async (c, next) => {
|
|
19
|
-
const session = await resolveSession(c);
|
|
20
|
-
if (!session) {
|
|
21
|
-
return c.json({ error: "Unauthorized" }, 401);
|
|
22
|
-
}
|
|
23
|
-
return next();
|
|
24
|
-
});
|