Skip to content

Commit 28146b8

Browse files
committed
fix(audience): apply excludeFilter to broadcasts and sequences
The sequence's excludeFilter was stored but never read, and the update endpoints didn't accept it. Contacts matching it are now left out of broadcast audiences and triggered sequence enrollment (which now also honours the sequence filter). REST, MCP, the contacts list (for the recipient count) and the broadcast editor accept an exclusion. Refs #41, #79
1 parent 531d1f2 commit 28146b8

18 files changed

Lines changed: 295 additions & 48 deletions

File tree

‎apps/api/src/automation/fire-event.test.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mock.module("../db/client", async () => {
88

99
const { EventType } = await import("../config/constants");
1010
const { db } = await import("../db/client");
11-
const { ongoingSequences, rules } = await import("../db/schema");
11+
const { ongoingSequences, rules, sequences } = await import("../db/schema");
1212
const { seedTeamAndContact, truncateAll } = await import("../test/db");
1313
type TestDb = import("../test/db").TestDb;
1414
const { seedSequence } = await import("../test/fixtures");
@@ -137,4 +137,34 @@ describe("fireEvent", () => {
137137

138138
expect(await enrolledContactIds(sequenceRow.id)).toEqual([contact.id]);
139139
});
140+
141+
it("skips contacts matching the sequence's exclusion filter", async () => {
142+
const { team, contact } = await seedTeamAndContact(tdb);
143+
const { sequenceRow } = await seedSequence(tdb, {
144+
teamId: team.id,
145+
emails: [{ emailId: "email_e1" }],
146+
});
147+
await tdb
148+
.update(sequences)
149+
.set({
150+
excludeFilter: {
151+
aggregator: "or",
152+
filters: [{ name: "email", condition: "is", value: contact.email }],
153+
},
154+
})
155+
.where(eq(sequences.id, sequenceRow.id));
156+
await seedRule({
157+
teamId: team.id,
158+
sequenceId: sequenceRow.id,
159+
event: EventType.SUBSCRIBER_ADDED,
160+
});
161+
162+
await fireEvent({
163+
teamId: team.id,
164+
event: EventType.SUBSCRIBER_ADDED,
165+
contactId: contact.id,
166+
});
167+
168+
expect(await enrolledContactIds(sequenceRow.id)).toEqual([]);
169+
});
140170
});

‎apps/api/src/automation/fire-event.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { and, eq } from "drizzle-orm";
22
import { type Event, EventType } from "../config/constants";
3+
import { sequenceAudience } from "../contacts/segment";
34
import { db } from "../db/client";
45
import { rules, sequences } from "../db/schema";
56
import { captureError, captureEvent } from "../observability";
67
import logger from "../services/log";
7-
import { enrollContactsInOngoingSequence } from "./queries";
8+
import { contactMatchesAudience, enrollContactsInOngoingSequence } from "./queries";
89

910
/**
1011
* Tag/subscriber-based sequence triggers are event driven rather than polled
@@ -38,13 +39,27 @@ export async function fireEvent({
3839
}
3940

4041
const [sequenceRow] = await db
41-
.select({ id: sequences.id, status: sequences.status })
42+
.select({
43+
id: sequences.id,
44+
status: sequences.status,
45+
filter: sequences.filter,
46+
excludeFilter: sequences.excludeFilter,
47+
})
4248
.from(sequences)
4349
.where(and(eq(sequences.teamId, teamId), eq(sequences.id, rule.sequenceId)))
4450
.limit(1);
4551
if (sequenceRow?.status !== "active") continue;
4652

4753
try {
54+
if (
55+
!(await contactMatchesAudience(
56+
teamId,
57+
contactId,
58+
sequenceAudience(sequenceRow),
59+
))
60+
) {
61+
continue;
62+
}
4863
await enrollContactsInOngoingSequence({
4964
teamId,
5065
sequenceId: sequenceRow.id,

‎apps/api/src/automation/process-rules.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ContactFilterWithAggregator } from "../contacts/segment";
1+
import { sequenceAudience } from "../contacts/segment";
22
import { captureError, captureEvent } from "../observability";
33
import logger from "../services/log";
44
import {
@@ -76,7 +76,7 @@ export async function processRule(rule: {
7676

7777
const contactIds = await getMatchingContactIds(
7878
rule.teamId,
79-
sequenceRow.filter as ContactFilterWithAggregator | null,
79+
sequenceAudience(sequenceRow),
8080
);
8181

8282
logger.info(
@@ -102,7 +102,7 @@ export async function processRule(rule: {
102102

103103
const publicContactIds = await getMatchingPublicContactIds(
104104
rule.teamId,
105-
sequenceRow.filter as ContactFilterWithAggregator | null,
105+
sequenceAudience(sequenceRow),
106106
);
107107
// lockBroadcast must run before markBroadcastSent: the latter jsonb_set's
108108
// `{broadcast,sentAt}` and needs `report.broadcast` to already exist.

‎apps/api/src/automation/queries.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
countOngoingSequencesForSequence,
1616
enrollContactsInOngoingSequence,
1717
getDueOngoingSequences,
18+
getMatchingPublicContactIds,
1819
} = await import("./queries");
1920

2021
const tdb = db as unknown as TestDb;
@@ -23,13 +24,17 @@ beforeEach(async () => {
2324
await truncateAll(tdb);
2425
});
2526

26-
async function seedContact(teamId: string) {
27+
async function seedContact(
28+
teamId: string,
29+
values: Partial<typeof contacts.$inferInsert> = {},
30+
) {
2731
const [row] = await tdb
2832
.insert(contacts)
2933
.values({
3034
teamId,
3135
email: `reader-${crypto.randomUUID()}@example.com`,
3236
unsubscribeToken: crypto.randomUUID(),
37+
...values,
3338
})
3439
.returning();
3540
return row;
@@ -155,3 +160,49 @@ describe("countOngoingSequencesForSequence", () => {
155160
expect(await countOngoingSequencesForSequence(seqTwo.id)).toBe(0);
156161
});
157162
});
163+
164+
describe("getMatchingPublicContactIds", () => {
165+
const tagFilter = (tag: string) => ({
166+
aggregator: "or" as const,
167+
filters: [{ name: "tag" as const, condition: "is", value: tag }],
168+
});
169+
170+
it("leaves out contacts matching the exclusion filter", async () => {
171+
const { team } = await seedTeamAndContact(tdb);
172+
await tdb.delete(contacts).where(eq(contacts.teamId, team.id));
173+
const reader = await seedContact(team.id, { tags: ["newsletter"] });
174+
await seedContact(team.id, { tags: ["newsletter", "customer"] });
175+
const untagged = await seedContact(team.id, { tags: [] });
176+
177+
expect(
178+
(
179+
await getMatchingPublicContactIds(team.id, {
180+
filter: tagFilter("newsletter"),
181+
excludeFilter: tagFilter("customer"),
182+
})
183+
).sort(),
184+
).toEqual([reader.contactId]);
185+
// An empty audience filter means everyone, minus the exclusion.
186+
expect(
187+
(
188+
await getMatchingPublicContactIds(team.id, {
189+
excludeFilter: tagFilter("customer"),
190+
})
191+
).sort(),
192+
).toEqual([reader.contactId, untagged.contactId].sort());
193+
expect(
194+
(await getMatchingPublicContactIds(team.id, { excludeFilter: null })).length,
195+
).toBe(3);
196+
});
197+
198+
it("never includes unsubscribed contacts", async () => {
199+
const { team } = await seedTeamAndContact(tdb);
200+
await tdb.delete(contacts).where(eq(contacts.teamId, team.id));
201+
const subscribed = await seedContact(team.id);
202+
await seedContact(team.id, { subscribed: false });
203+
204+
expect(await getMatchingPublicContactIds(team.id, {})).toEqual([
205+
subscribed.contactId,
206+
]);
207+
});
208+
});

‎apps/api/src/automation/queries.ts‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { and, count, eq, isNull, lt, or, sql } from "drizzle-orm";
22
import { assertMarketingAllowedForContactUsage } from "../billing/entitlements";
33
import { EventType, sequenceBounceLimit } from "../config/constants";
44
import {
5+
type AudienceFilters,
6+
buildContactExclusionCondition,
57
buildContactFilterCondition,
6-
type ContactFilterWithAggregator,
78
} from "../contacts/segment";
89
import { db } from "../db/client";
910
import { contacts, ongoingSequences, rules, sequences, teams } from "../db/schema";
@@ -38,14 +39,13 @@ export async function getSequenceRowBySequenceId(teamId: string, sequenceId: str
3839
return row ?? null;
3940
}
4041

41-
function matchingContactsCondition(
42-
teamId: string,
43-
filter: ContactFilterWithAggregator | null | undefined,
44-
) {
45-
const condition = buildContactFilterCondition(filter);
46-
return condition
47-
? and(eq(contacts.teamId, teamId), eq(contacts.subscribed, true), condition)
48-
: and(eq(contacts.teamId, teamId), eq(contacts.subscribed, true));
42+
function matchingContactsCondition(teamId: string, audience: AudienceFilters) {
43+
return and(
44+
eq(contacts.teamId, teamId),
45+
eq(contacts.subscribed, true),
46+
buildContactFilterCondition(audience.filter),
47+
buildContactExclusionCondition(audience.excludeFilter),
48+
);
4949
}
5050

5151
/** Returns **internal** contact ids — the only kind
@@ -55,12 +55,12 @@ function matchingContactsCondition(
5555
* `sequences.entrants`). */
5656
export async function getMatchingContactIds(
5757
teamId: string,
58-
filter: ContactFilterWithAggregator | null | undefined,
58+
audience: AudienceFilters,
5959
): Promise<string[]> {
6060
const rows = await db
6161
.select({ id: contacts.id })
6262
.from(contacts)
63-
.where(matchingContactsCondition(teamId, filter));
63+
.where(matchingContactsCondition(teamId, audience));
6464
return rows.map((r) => r.id);
6565
}
6666

@@ -69,15 +69,30 @@ export async function getMatchingContactIds(
6969
* intentionally aren't live FKs. */
7070
export async function getMatchingPublicContactIds(
7171
teamId: string,
72-
filter: ContactFilterWithAggregator | null | undefined,
72+
audience: AudienceFilters,
7373
): Promise<string[]> {
7474
const rows = await db
7575
.select({ contactId: contacts.contactId })
7676
.from(contacts)
77-
.where(matchingContactsCondition(teamId, filter));
77+
.where(matchingContactsCondition(teamId, audience));
7878
return rows.map((r) => r.contactId);
7979
}
8080

81+
/** Whether one contact (internal `contacts.id`) is in a sequence's audience,
82+
* for event-triggered enrollment. */
83+
export async function contactMatchesAudience(
84+
teamId: string,
85+
contactId: string,
86+
audience: AudienceFilters,
87+
): Promise<boolean> {
88+
const [row] = await db
89+
.select({ id: contacts.id })
90+
.from(contacts)
91+
.where(and(eq(contacts.id, contactId), matchingContactsCondition(teamId, audience)))
92+
.limit(1);
93+
return Boolean(row);
94+
}
95+
8196
export async function enrollContactsInOngoingSequence({
8297
teamId,
8398
sequenceId,

‎apps/api/src/contacts/queries.ts‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { captureEvent } from "../observability";
1818
// opaque secret rather than a public resource ID.
1919
import { generateUniqueId } from "../utils/id";
2020
import {
21+
buildContactExclusionCondition,
2122
buildContactFilterCondition,
2223
type ContactFilterWithAggregator,
2324
} from "./segment";
@@ -150,15 +151,19 @@ export async function getContactByUnsubscribeToken(
150151
return row ?? null;
151152
}
152153

154+
interface ContactListQuery {
155+
searchText?: string;
156+
filter?: ContactListFilter;
157+
/** Contacts matching this filter are left out. */
158+
excludeFilter?: ContactFilterWithAggregator;
159+
}
160+
153161
function listContactsConditions({
154162
teamId,
155163
searchText,
156164
filter,
157-
}: {
158-
teamId: string;
159-
searchText?: string;
160-
filter?: ContactListFilter;
161-
}) {
165+
excludeFilter,
166+
}: ContactListQuery & { teamId: string }) {
162167
const conditions = [eq(contacts.teamId, teamId)];
163168
if (searchText) {
164169
conditions.push(
@@ -175,38 +180,36 @@ function listContactsConditions({
175180
conditions.push(filterCondition);
176181
}
177182
}
183+
const exclusion = buildContactExclusionCondition(excludeFilter);
184+
if (exclusion) conditions.push(exclusion);
178185
return and(...conditions);
179186
}
180187

181188
export async function listContacts({
182-
teamId,
183-
searchText,
184-
filter,
185189
offset = 1,
186190
rowsPerPage = itemsPerPage,
187-
}: {
191+
...query
192+
}: ContactListQuery & {
188193
teamId: string;
189-
searchText?: string;
190-
filter?: ContactListFilter;
191194
offset?: number;
192195
rowsPerPage?: number;
193196
}): Promise<Contact[]> {
194197
return db
195198
.select()
196199
.from(contacts)
197-
.where(listContactsConditions({ teamId, searchText, filter }))
200+
.where(listContactsConditions(query))
198201
.limit(rowsPerPage)
199202
.offset((Math.max(offset, 1) - 1) * rowsPerPage);
200203
}
201204

202205
export async function countContacts(
203206
teamId: string,
204-
{ searchText, filter }: { searchText?: string; filter?: ContactListFilter } = {},
207+
query: ContactListQuery = {},
205208
): Promise<number> {
206209
const [row] = await db
207210
.select({ value: count() })
208211
.from(contacts)
209-
.where(listContactsConditions({ teamId, searchText, filter }));
212+
.where(listContactsConditions({ ...query, teamId }));
210213
return row?.value ?? 0;
211214
}
212215

‎apps/api/src/contacts/routes.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
removeTagFromContact,
2222
updateContact,
2323
} from "./queries";
24+
import type { ContactFilterWithAggregator } from "./segment";
2425
import { getSegment } from "./segments-queries";
2526

2627
const router = Router();
@@ -79,16 +80,25 @@ const impl = s.router(contract.contacts, {
7980
filters.push(parsedFilter.data as any);
8081
}
8182
const filter = filters.length ? filters : undefined;
83+
let excludeFilter: ContactFilterWithAggregator | undefined;
84+
if (query.excludeFilter) {
85+
const parsedExclude = parseContactFilterQueryParam(query.excludeFilter);
86+
if (!parsedExclude.success) {
87+
return { status: 400, body: { error: "Invalid excludeFilter" } };
88+
}
89+
excludeFilter = parsedExclude.data as ContactFilterWithAggregator;
90+
}
8291

8392
const [items, total] = await Promise.all([
8493
listContacts({
8594
teamId,
8695
searchText: query.q,
8796
filter,
97+
excludeFilter,
8898
offset: query.offset,
8999
rowsPerPage: query.rowsPerPage,
90100
}),
91-
countContacts(teamId, { searchText: query.q, filter }),
101+
countContacts(teamId, { searchText: query.q, filter, excludeFilter }),
92102
]);
93103
return {
94104
status: 200,

0 commit comments

Comments
 (0)