@app-vitals/agent-admin 1.101.2 → 1.101.3
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 +2 -2
- package/src/agent-members.integration.test.ts +99 -0
- package/src/agent-members.ts +56 -14
- package/src/agents-api.integration.test.ts +22 -6
- package/src/agents.unit.test.ts +6 -1
- package/src/member-id-test-support.ts +29 -0
- package/src/push-service.integration.test.ts +154 -0
- package/src/push-service.ts +83 -34
- package/src/push-service.unit.test.ts +84 -82
- package/src/user-notification-prefs.integration.test.ts +84 -0
- package/src/user-notification-prefs.ts +77 -0
- package/src/agent-members.unit.test.ts +0 -255
package/src/push-service.ts
CHANGED
|
@@ -14,13 +14,14 @@
|
|
|
14
14
|
* only constructs a PushService when isPushEnabled() is true.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
import { PrincipalLookupService } from "./principal-lookup.ts";
|
|
17
18
|
import {
|
|
18
|
-
type NotificationThread,
|
|
19
|
-
type PushDetailLevel,
|
|
20
|
-
type SessionNotificationKind,
|
|
21
19
|
buildNotificationPayload,
|
|
22
20
|
buildSessionNotificationPayload,
|
|
21
|
+
type NotificationThread,
|
|
22
|
+
type PushDetailLevel,
|
|
23
23
|
resolveDetailLevel,
|
|
24
|
+
type SessionNotificationKind,
|
|
24
25
|
} from "./push-content.ts";
|
|
25
26
|
import {
|
|
26
27
|
PushSender,
|
|
@@ -33,21 +34,24 @@ import {
|
|
|
33
34
|
interface PushPrismaLike {
|
|
34
35
|
chatThreadWatch: {
|
|
35
36
|
findMany(args: {
|
|
36
|
-
where: { threadId: string };
|
|
37
|
-
select: {
|
|
38
|
-
}): Promise<Array<{
|
|
37
|
+
where: { threadId: string; memberId: { not: null } };
|
|
38
|
+
select: { memberId: true };
|
|
39
|
+
}): Promise<Array<{ memberId: string | null }>>;
|
|
39
40
|
upsert(args: {
|
|
40
41
|
where: { userEmail_threadId: { userEmail: string; threadId: string } };
|
|
41
|
-
create: {
|
|
42
|
-
|
|
42
|
+
create: {
|
|
43
|
+
userEmail: string;
|
|
44
|
+
threadId: string;
|
|
45
|
+
agentId: string;
|
|
46
|
+
memberId: string | null;
|
|
47
|
+
};
|
|
48
|
+
update: { agentId: string; memberId: string | null };
|
|
43
49
|
}): Promise<{ id: string }>;
|
|
44
50
|
};
|
|
45
51
|
pushSubscription: {
|
|
46
|
-
findMany(args: {
|
|
47
|
-
where: { userEmail: { in: string[] } };
|
|
48
|
-
}): Promise<
|
|
52
|
+
findMany(args: { where: { memberId: { in: string[] } } }): Promise<
|
|
49
53
|
Array<{
|
|
50
|
-
|
|
54
|
+
memberId: string | null;
|
|
51
55
|
endpoint: string;
|
|
52
56
|
p256dh: string;
|
|
53
57
|
auth: string;
|
|
@@ -58,16 +62,24 @@ interface PushPrismaLike {
|
|
|
58
62
|
where: { endpoint: string };
|
|
59
63
|
create: {
|
|
60
64
|
userEmail: string;
|
|
65
|
+
memberId: string | null;
|
|
61
66
|
endpoint: string;
|
|
62
67
|
p256dh: string;
|
|
63
68
|
auth: string;
|
|
64
69
|
};
|
|
65
|
-
update: {
|
|
70
|
+
update: {
|
|
71
|
+
userEmail: string;
|
|
72
|
+
memberId: string | null;
|
|
73
|
+
p256dh: string;
|
|
74
|
+
auth: string;
|
|
75
|
+
};
|
|
66
76
|
}): Promise<{ id: string }>;
|
|
67
77
|
deleteMany(
|
|
68
78
|
args:
|
|
69
79
|
| { where: { endpoint: { in: string[] } } }
|
|
70
|
-
| {
|
|
80
|
+
| {
|
|
81
|
+
where: { endpoint: string; memberId?: string; userEmail?: string };
|
|
82
|
+
},
|
|
71
83
|
): Promise<{ count: number }>;
|
|
72
84
|
};
|
|
73
85
|
}
|
|
@@ -103,39 +115,59 @@ export interface NotificationSession {
|
|
|
103
115
|
title?: string | null;
|
|
104
116
|
}
|
|
105
117
|
|
|
118
|
+
/** The slice of PrincipalLookupService PushService needs (email -> member id). */
|
|
119
|
+
export interface MemberIdLookup {
|
|
120
|
+
findIdByEmail(email: string): Promise<{ id: string } | null>;
|
|
121
|
+
}
|
|
122
|
+
|
|
106
123
|
export class PushService {
|
|
107
124
|
private readonly sender: PushSender;
|
|
125
|
+
private readonly lookup: MemberIdLookup;
|
|
108
126
|
|
|
127
|
+
/**
|
|
128
|
+
* ACM-4.1: watches and subscriptions are read and written by memberId
|
|
129
|
+
* (AccountMember.id); rows with a null memberId are skipped. `lookup`
|
|
130
|
+
* resolves the emails callers still pass; it defaults to a
|
|
131
|
+
* PrincipalLookupService over `prisma`.
|
|
132
|
+
*/
|
|
109
133
|
constructor(
|
|
110
134
|
private readonly prisma: PushPrismaLike,
|
|
111
135
|
vapid: VapidConfig,
|
|
112
136
|
fetchImpl: typeof fetch,
|
|
113
137
|
private readonly maxDetail: PushDetailLevel | string,
|
|
138
|
+
lookup?: MemberIdLookup,
|
|
114
139
|
) {
|
|
115
140
|
this.sender = new PushSender(vapid, fetchImpl);
|
|
141
|
+
this.lookup = lookup ?? new PrincipalLookupService(prisma as never);
|
|
116
142
|
}
|
|
117
143
|
|
|
118
|
-
|
|
144
|
+
private async memberIdForEmail(email: string): Promise<string | null> {
|
|
145
|
+
return (await this.lookup.findIdByEmail(email))?.id ?? null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The deduped set of member ids watching a thread (null-memberId rows skipped). */
|
|
119
149
|
private async watchersForThread(threadId: string): Promise<string[]> {
|
|
120
150
|
const watchers = await this.prisma.chatThreadWatch.findMany({
|
|
121
|
-
where: { threadId },
|
|
122
|
-
select: {
|
|
151
|
+
where: { threadId, memberId: { not: null } },
|
|
152
|
+
select: { memberId: true },
|
|
123
153
|
});
|
|
124
|
-
return [
|
|
154
|
+
return [
|
|
155
|
+
...new Set(watchers.flatMap((w) => (w.memberId ? [w.memberId] : []))),
|
|
156
|
+
];
|
|
125
157
|
}
|
|
126
158
|
|
|
127
|
-
/** All push subscriptions belonging to the given
|
|
128
|
-
private async
|
|
129
|
-
if (
|
|
159
|
+
/** All push subscriptions belonging to the given member ids. */
|
|
160
|
+
private async subscriptionsForMembers(memberIds: string[]) {
|
|
161
|
+
if (memberIds.length === 0) return [];
|
|
130
162
|
return this.prisma.pushSubscription.findMany({
|
|
131
|
-
where: {
|
|
163
|
+
where: { memberId: { in: memberIds } },
|
|
132
164
|
});
|
|
133
165
|
}
|
|
134
166
|
|
|
135
167
|
/** All subscriptions belonging to the users watching a thread. */
|
|
136
168
|
private async subscriptionsForThread(threadId: string) {
|
|
137
|
-
const
|
|
138
|
-
return this.
|
|
169
|
+
const memberIds = await this.watchersForThread(threadId);
|
|
170
|
+
return this.subscriptionsForMembers(memberIds);
|
|
139
171
|
}
|
|
140
172
|
|
|
141
173
|
/**
|
|
@@ -161,7 +193,21 @@ export class PushService {
|
|
|
161
193
|
emails: string[],
|
|
162
194
|
buildPayload: (level: PushDetailLevel) => string,
|
|
163
195
|
): Promise<{ delivered: number; pruned: number }> {
|
|
164
|
-
const
|
|
196
|
+
const resolved = await Promise.all(
|
|
197
|
+
[...new Set(emails)].map((e) => this.memberIdForEmail(e)),
|
|
198
|
+
);
|
|
199
|
+
return this.sendToMembers(
|
|
200
|
+
resolved.flatMap((id) => (id ? [id] : [])),
|
|
201
|
+
buildPayload,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** sendToUsers for callers that already hold AccountMember ids. */
|
|
206
|
+
async sendToMembers(
|
|
207
|
+
memberIds: string[],
|
|
208
|
+
buildPayload: (level: PushDetailLevel) => string,
|
|
209
|
+
): Promise<{ delivered: number; pruned: number }> {
|
|
210
|
+
const subs = await this.subscriptionsForMembers(memberIds);
|
|
165
211
|
if (subs.length === 0) return { delivered: 0, pruned: 0 };
|
|
166
212
|
|
|
167
213
|
// Group subscriptions by their effective detail level so each group gets
|
|
@@ -205,9 +251,9 @@ export class PushService {
|
|
|
205
251
|
thread: NotificationThread,
|
|
206
252
|
): Promise<{ delivered: number; pruned: number }> {
|
|
207
253
|
try {
|
|
208
|
-
const
|
|
209
|
-
if (
|
|
210
|
-
return await this.
|
|
254
|
+
const memberIds = await this.watchersForThread(thread.threadId);
|
|
255
|
+
if (memberIds.length === 0) return { delivered: 0, pruned: 0 };
|
|
256
|
+
return await this.sendToMembers(memberIds, (level) =>
|
|
211
257
|
JSON.stringify(buildNotificationPayload(level, thread)),
|
|
212
258
|
);
|
|
213
259
|
} catch (err) {
|
|
@@ -270,10 +316,11 @@ export class PushService {
|
|
|
270
316
|
): Promise<void> {
|
|
271
317
|
if (!this.prisma.chatThreadWatch) return;
|
|
272
318
|
try {
|
|
319
|
+
const memberId = await this.memberIdForEmail(userEmail);
|
|
273
320
|
await this.prisma.chatThreadWatch.upsert({
|
|
274
321
|
where: { userEmail_threadId: { userEmail, threadId } },
|
|
275
|
-
create: { userEmail, threadId, agentId },
|
|
276
|
-
update: { agentId },
|
|
322
|
+
create: { userEmail, threadId, agentId, memberId },
|
|
323
|
+
update: { agentId, memberId },
|
|
277
324
|
});
|
|
278
325
|
} catch (err) {
|
|
279
326
|
console.error("[push] watchThread upsert failed:", err);
|
|
@@ -296,10 +343,11 @@ export class PushService {
|
|
|
296
343
|
if (!this.prisma.pushSubscription)
|
|
297
344
|
return { ok: false, reason: "unavailable" };
|
|
298
345
|
try {
|
|
346
|
+
const memberId = await this.memberIdForEmail(userEmail);
|
|
299
347
|
await this.prisma.pushSubscription.upsert({
|
|
300
348
|
where: { endpoint },
|
|
301
|
-
create: { userEmail, endpoint, p256dh, auth },
|
|
302
|
-
update: { userEmail, p256dh, auth },
|
|
349
|
+
create: { userEmail, memberId, endpoint, p256dh, auth },
|
|
350
|
+
update: { userEmail, memberId, p256dh, auth },
|
|
303
351
|
});
|
|
304
352
|
return { ok: true };
|
|
305
353
|
} catch (err) {
|
|
@@ -310,7 +358,7 @@ export class PushService {
|
|
|
310
358
|
|
|
311
359
|
/**
|
|
312
360
|
* Deletes a browser push subscription, scoped to both endpoint and
|
|
313
|
-
* userEmail — this scoping prevents a stale endpoint from pruning another
|
|
361
|
+
* the owning member (memberId; falls back to userEmail for an email with no member) — this scoping prevents a stale endpoint from pruning another
|
|
314
362
|
* user's subscription. Delete failures are best-effort: logged and
|
|
315
363
|
* swallowed, never surfaced as a distinct outcome.
|
|
316
364
|
*/
|
|
@@ -321,8 +369,9 @@ export class PushService {
|
|
|
321
369
|
if (!this.prisma.pushSubscription)
|
|
322
370
|
return { ok: false, reason: "unavailable" };
|
|
323
371
|
try {
|
|
372
|
+
const memberId = await this.memberIdForEmail(userEmail);
|
|
324
373
|
await this.prisma.pushSubscription.deleteMany({
|
|
325
|
-
where: { endpoint, userEmail },
|
|
374
|
+
where: memberId ? { endpoint, memberId } : { endpoint, userEmail },
|
|
326
375
|
});
|
|
327
376
|
} catch (err) {
|
|
328
377
|
console.error("[push] unsubscribe delete failed:", err);
|
|
@@ -11,6 +11,9 @@ import { buildSessionNotificationPayload } from "./push-content.ts";
|
|
|
11
11
|
import type { PushSubscriptionLike } from "./push-sender.ts";
|
|
12
12
|
import { PushService } from "./push-service.ts";
|
|
13
13
|
|
|
14
|
+
const unusedFetch = (async () =>
|
|
15
|
+
new Response(null, { status: 201 })) as unknown as typeof fetch;
|
|
16
|
+
|
|
14
17
|
const vapid = {
|
|
15
18
|
publicKey:
|
|
16
19
|
"BAl-hYOXgpZwx-qqRoAapu5iSeHrlcJdy89wjv1NyctfsYSyGKCVO97vvlOVR4h61MROU8CZLAxzQEj8vIFsTaI",
|
|
@@ -31,18 +34,38 @@ interface Row {
|
|
|
31
34
|
detailOptIn: string;
|
|
32
35
|
}
|
|
33
36
|
|
|
37
|
+
// Member ids equal emails in these doubles: the injected lookup maps an email
|
|
38
|
+
// to itself, and a "member" exists for every email except nobody@x.com.
|
|
39
|
+
const lookup = {
|
|
40
|
+
findIdByEmail: async (email: string) =>
|
|
41
|
+
email === "nobody@x.com" ? null : { id: email },
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function makeService(
|
|
45
|
+
prisma: never,
|
|
46
|
+
vapidConfig: typeof vapid,
|
|
47
|
+
fetchImpl: typeof fetch,
|
|
48
|
+
maxDetail: string,
|
|
49
|
+
) {
|
|
50
|
+
return new PushService(prisma, vapidConfig, fetchImpl, maxDetail, lookup);
|
|
51
|
+
}
|
|
52
|
+
|
|
34
53
|
function fakePrisma(watchers: string[], subs: Row[]) {
|
|
35
54
|
const deleted: string[] = [];
|
|
36
55
|
const prisma = {
|
|
37
56
|
chatThreadWatch: {
|
|
38
|
-
findMany: async () => watchers.map((email) => ({
|
|
57
|
+
findMany: async () => watchers.map((email) => ({ memberId: email })),
|
|
39
58
|
},
|
|
40
59
|
pushSubscription: {
|
|
41
|
-
findMany: async ({ where }: { where: {
|
|
42
|
-
subs
|
|
60
|
+
findMany: async ({ where }: { where: { memberId: { in: string[] } } }) =>
|
|
61
|
+
subs
|
|
62
|
+
.filter((s) => where.memberId.in.includes(s.userEmail))
|
|
63
|
+
.map((s) => ({ ...s, memberId: s.userEmail })),
|
|
43
64
|
deleteMany: async ({
|
|
44
65
|
where,
|
|
45
|
-
}: {
|
|
66
|
+
}: {
|
|
67
|
+
where: { endpoint: { in: string[] } };
|
|
68
|
+
}) => {
|
|
46
69
|
deleted.push(...where.endpoint.in);
|
|
47
70
|
return { count: where.endpoint.in.length };
|
|
48
71
|
},
|
|
@@ -85,7 +108,7 @@ describe("PushService.notifyThreadReply", () => {
|
|
|
85
108
|
});
|
|
86
109
|
}) as unknown as typeof fetch;
|
|
87
110
|
|
|
88
|
-
const service =
|
|
111
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
89
112
|
const result = await service.notifyThreadReply({
|
|
90
113
|
threadId: "thr_1",
|
|
91
114
|
agentId: "agt_1",
|
|
@@ -105,7 +128,7 @@ describe("PushService.notifyThreadReply", () => {
|
|
|
105
128
|
called += 1;
|
|
106
129
|
return new Response(null, { status: 201 });
|
|
107
130
|
}) as unknown as typeof fetch;
|
|
108
|
-
const service =
|
|
131
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
109
132
|
const result = await service.notifyThreadReply({
|
|
110
133
|
threadId: "thr_x",
|
|
111
134
|
agentId: "agt_x",
|
|
@@ -117,6 +140,27 @@ describe("PushService.notifyThreadReply", () => {
|
|
|
117
140
|
expect(deleted).toEqual([]);
|
|
118
141
|
});
|
|
119
142
|
|
|
143
|
+
it("skips watchers whose memberId is null without crashing", async () => {
|
|
144
|
+
const prisma = {
|
|
145
|
+
chatThreadWatch: {
|
|
146
|
+
findMany: async () => [{ memberId: null }, { memberId: "a@x.com" }],
|
|
147
|
+
},
|
|
148
|
+
pushSubscription: {
|
|
149
|
+
findMany: async ({
|
|
150
|
+
where,
|
|
151
|
+
}: {
|
|
152
|
+
where: { memberId: { in: string[] } };
|
|
153
|
+
}) => {
|
|
154
|
+
expect(where.memberId.in).toEqual(["a@x.com"]);
|
|
155
|
+
return [];
|
|
156
|
+
},
|
|
157
|
+
deleteMany: async () => ({ count: 0 }),
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
161
|
+
expect(await service.resolveLevelsForThread("thr_1")).toEqual([]);
|
|
162
|
+
});
|
|
163
|
+
|
|
120
164
|
it("caps each subscription's detail at the operator ceiling", async () => {
|
|
121
165
|
// Ceiling = generic; a subscription opted into preview still gets generic.
|
|
122
166
|
const { prisma } = fakePrisma(
|
|
@@ -139,12 +183,7 @@ describe("PushService.notifyThreadReply", () => {
|
|
|
139
183
|
bodies.push(String(init.method));
|
|
140
184
|
return new Response(null, { status: 201 });
|
|
141
185
|
}) as unknown as typeof fetch;
|
|
142
|
-
const service =
|
|
143
|
-
prisma as never,
|
|
144
|
-
vapid,
|
|
145
|
-
fetchImpl,
|
|
146
|
-
"generic",
|
|
147
|
-
);
|
|
186
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "generic");
|
|
148
187
|
const levels = await service.resolveLevelsForThread("thr_1");
|
|
149
188
|
expect(levels).toEqual(["generic"]);
|
|
150
189
|
});
|
|
@@ -181,7 +220,7 @@ describe("PushService.sendToUsers", () => {
|
|
|
181
220
|
});
|
|
182
221
|
}) as unknown as typeof fetch;
|
|
183
222
|
|
|
184
|
-
const service =
|
|
223
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
185
224
|
const result = await service.sendToUsers(["a@x.com", "b@x.com"], (level) =>
|
|
186
225
|
JSON.stringify({ level }),
|
|
187
226
|
);
|
|
@@ -199,7 +238,7 @@ describe("PushService.sendToUsers", () => {
|
|
|
199
238
|
called += 1;
|
|
200
239
|
return new Response(null, { status: 201 });
|
|
201
240
|
}) as unknown as typeof fetch;
|
|
202
|
-
const service =
|
|
241
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
203
242
|
const result = await service.sendToUsers(["nobody@x.com"], () => "{}");
|
|
204
243
|
|
|
205
244
|
expect(called).toBe(0);
|
|
@@ -227,7 +266,7 @@ describe("PushService.notifySession", () => {
|
|
|
227
266
|
sent.push(url);
|
|
228
267
|
return new Response(null, { status: 201 });
|
|
229
268
|
}) as unknown as typeof fetch;
|
|
230
|
-
const service =
|
|
269
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
231
270
|
|
|
232
271
|
const result = await service.notifySession(
|
|
233
272
|
{ slug: "sesh_1", emails: ["a@x.com"] },
|
|
@@ -256,7 +295,7 @@ describe("PushService.notifySession", () => {
|
|
|
256
295
|
);
|
|
257
296
|
const fetchImpl = (async () =>
|
|
258
297
|
new Response(null, { status: 201 })) as unknown as typeof fetch;
|
|
259
|
-
const service =
|
|
298
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
260
299
|
|
|
261
300
|
// The encrypted push body isn't inspectable end-to-end, so capture the
|
|
262
301
|
// payload builder notifySession hands to the extracted core — same
|
|
@@ -287,7 +326,7 @@ describe("PushService.notifySession", () => {
|
|
|
287
326
|
called += 1;
|
|
288
327
|
return new Response(null, { status: 201 });
|
|
289
328
|
}) as unknown as typeof fetch;
|
|
290
|
-
const service =
|
|
329
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
291
330
|
|
|
292
331
|
const result = await service.notifySession(
|
|
293
332
|
{ slug: "sesh_1", emails: [] },
|
|
@@ -317,7 +356,7 @@ describe("PushService.notifySession", () => {
|
|
|
317
356
|
new Response(null, { status: 201 })) as unknown as typeof fetch;
|
|
318
357
|
// Operator ceiling = title, so every subscription resolves to at most
|
|
319
358
|
// "title" before the caller's own ceiling is applied.
|
|
320
|
-
const service =
|
|
359
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
321
360
|
|
|
322
361
|
// The encrypted push body isn't inspectable, so capture the payload
|
|
323
362
|
// builder notifySession hands to the extracted core and exercise it with
|
|
@@ -376,7 +415,7 @@ describe("PushService.notifySession", () => {
|
|
|
376
415
|
);
|
|
377
416
|
const fetchImpl = (async () =>
|
|
378
417
|
new Response(null, { status: 201 })) as unknown as typeof fetch;
|
|
379
|
-
const service =
|
|
418
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
380
419
|
|
|
381
420
|
const built: string[] = [];
|
|
382
421
|
const realSendToUsers = service.sendToUsers.bind(service);
|
|
@@ -432,7 +471,7 @@ describe("PushService.notifySession", () => {
|
|
|
432
471
|
};
|
|
433
472
|
const fetchImpl = (async () =>
|
|
434
473
|
new Response(null, { status: 201 })) as unknown as typeof fetch;
|
|
435
|
-
const service =
|
|
474
|
+
const service = makeService(prisma as never, vapid, fetchImpl, "title");
|
|
436
475
|
|
|
437
476
|
await expect(
|
|
438
477
|
service.notifySession(
|
|
@@ -447,9 +486,6 @@ describe("PushService.notifySession", () => {
|
|
|
447
486
|
// A fetchImpl is required by the PushService constructor but unused by the
|
|
448
487
|
// write-path methods under test below (recordWatch/subscribe/unsubscribe
|
|
449
488
|
// never touch PushSender).
|
|
450
|
-
const unusedFetch = (async () =>
|
|
451
|
-
new Response(null, { status: 201 })) as unknown as typeof fetch;
|
|
452
|
-
|
|
453
489
|
describe("PushService.recordWatch", () => {
|
|
454
490
|
it("upserts a ChatThreadWatch row scoped to userEmail+threadId", async () => {
|
|
455
491
|
const calls: unknown[] = [];
|
|
@@ -466,12 +502,7 @@ describe("PushService.recordWatch", () => {
|
|
|
466
502
|
deleteMany: async () => ({ count: 0 }),
|
|
467
503
|
},
|
|
468
504
|
};
|
|
469
|
-
const service =
|
|
470
|
-
prisma as never,
|
|
471
|
-
vapid,
|
|
472
|
-
unusedFetch,
|
|
473
|
-
"title",
|
|
474
|
-
);
|
|
505
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
475
506
|
await service.recordWatch("a@x.com", "agt_1", "thr_1");
|
|
476
507
|
|
|
477
508
|
expect(calls).toEqual([
|
|
@@ -479,8 +510,13 @@ describe("PushService.recordWatch", () => {
|
|
|
479
510
|
where: {
|
|
480
511
|
userEmail_threadId: { userEmail: "a@x.com", threadId: "thr_1" },
|
|
481
512
|
},
|
|
482
|
-
create: {
|
|
483
|
-
|
|
513
|
+
create: {
|
|
514
|
+
userEmail: "a@x.com",
|
|
515
|
+
threadId: "thr_1",
|
|
516
|
+
agentId: "agt_1",
|
|
517
|
+
memberId: "a@x.com",
|
|
518
|
+
},
|
|
519
|
+
update: { agentId: "agt_1", memberId: "a@x.com" },
|
|
484
520
|
},
|
|
485
521
|
]);
|
|
486
522
|
});
|
|
@@ -492,12 +528,7 @@ describe("PushService.recordWatch", () => {
|
|
|
492
528
|
deleteMany: async () => ({ count: 0 }),
|
|
493
529
|
},
|
|
494
530
|
};
|
|
495
|
-
const service =
|
|
496
|
-
prisma as never,
|
|
497
|
-
vapid,
|
|
498
|
-
unusedFetch,
|
|
499
|
-
"title",
|
|
500
|
-
);
|
|
531
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
501
532
|
await expect(
|
|
502
533
|
service.recordWatch("a@x.com", "agt_1", "thr_1"),
|
|
503
534
|
).resolves.toBeUndefined();
|
|
@@ -516,12 +547,7 @@ describe("PushService.recordWatch", () => {
|
|
|
516
547
|
deleteMany: async () => ({ count: 0 }),
|
|
517
548
|
},
|
|
518
549
|
};
|
|
519
|
-
const service =
|
|
520
|
-
prisma as never,
|
|
521
|
-
vapid,
|
|
522
|
-
unusedFetch,
|
|
523
|
-
"title",
|
|
524
|
-
);
|
|
550
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
525
551
|
await expect(
|
|
526
552
|
service.recordWatch("a@x.com", "agt_1", "thr_1"),
|
|
527
553
|
).resolves.toBeUndefined();
|
|
@@ -542,12 +568,7 @@ describe("PushService.subscribe", () => {
|
|
|
542
568
|
},
|
|
543
569
|
},
|
|
544
570
|
};
|
|
545
|
-
const service =
|
|
546
|
-
prisma as never,
|
|
547
|
-
vapid,
|
|
548
|
-
unusedFetch,
|
|
549
|
-
"title",
|
|
550
|
-
);
|
|
571
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
551
572
|
const result = await service.subscribe(
|
|
552
573
|
"a@x.com",
|
|
553
574
|
"https://push/ok",
|
|
@@ -561,11 +582,17 @@ describe("PushService.subscribe", () => {
|
|
|
561
582
|
where: { endpoint: "https://push/ok" },
|
|
562
583
|
create: {
|
|
563
584
|
userEmail: "a@x.com",
|
|
585
|
+
memberId: "a@x.com",
|
|
564
586
|
endpoint: "https://push/ok",
|
|
565
587
|
p256dh: P256DH,
|
|
566
588
|
auth: AUTH,
|
|
567
589
|
},
|
|
568
|
-
update: {
|
|
590
|
+
update: {
|
|
591
|
+
userEmail: "a@x.com",
|
|
592
|
+
memberId: "a@x.com",
|
|
593
|
+
p256dh: P256DH,
|
|
594
|
+
auth: AUTH,
|
|
595
|
+
},
|
|
569
596
|
},
|
|
570
597
|
]);
|
|
571
598
|
});
|
|
@@ -574,12 +601,7 @@ describe("PushService.subscribe", () => {
|
|
|
574
601
|
const prisma = {
|
|
575
602
|
chatThreadWatch: { findMany: async () => [] },
|
|
576
603
|
};
|
|
577
|
-
const service =
|
|
578
|
-
prisma as never,
|
|
579
|
-
vapid,
|
|
580
|
-
unusedFetch,
|
|
581
|
-
"title",
|
|
582
|
-
);
|
|
604
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
583
605
|
const result = await service.subscribe(
|
|
584
606
|
"a@x.com",
|
|
585
607
|
"https://push/ok",
|
|
@@ -600,12 +622,7 @@ describe("PushService.subscribe", () => {
|
|
|
600
622
|
},
|
|
601
623
|
},
|
|
602
624
|
};
|
|
603
|
-
const service =
|
|
604
|
-
prisma as never,
|
|
605
|
-
vapid,
|
|
606
|
-
unusedFetch,
|
|
607
|
-
"title",
|
|
608
|
-
);
|
|
625
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
609
626
|
const result = await service.subscribe(
|
|
610
627
|
"a@x.com",
|
|
611
628
|
"https://push/ok",
|
|
@@ -617,7 +634,7 @@ describe("PushService.subscribe", () => {
|
|
|
617
634
|
});
|
|
618
635
|
|
|
619
636
|
describe("PushService.unsubscribe", () => {
|
|
620
|
-
it("deletes the subscription scoped to endpoint+
|
|
637
|
+
it("deletes the subscription scoped to endpoint+memberId and returns ok:true", async () => {
|
|
621
638
|
const calls: unknown[] = [];
|
|
622
639
|
const prisma = {
|
|
623
640
|
chatThreadWatch: { findMany: async () => [] },
|
|
@@ -629,17 +646,12 @@ describe("PushService.unsubscribe", () => {
|
|
|
629
646
|
},
|
|
630
647
|
},
|
|
631
648
|
};
|
|
632
|
-
const service =
|
|
633
|
-
prisma as never,
|
|
634
|
-
vapid,
|
|
635
|
-
unusedFetch,
|
|
636
|
-
"title",
|
|
637
|
-
);
|
|
649
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
638
650
|
const result = await service.unsubscribe("a@x.com", "https://push/ok");
|
|
639
651
|
|
|
640
652
|
expect(result).toEqual({ ok: true });
|
|
641
653
|
expect(calls).toEqual([
|
|
642
|
-
{ where: { endpoint: "https://push/ok",
|
|
654
|
+
{ where: { endpoint: "https://push/ok", memberId: "a@x.com" } },
|
|
643
655
|
]);
|
|
644
656
|
});
|
|
645
657
|
|
|
@@ -647,12 +659,7 @@ describe("PushService.unsubscribe", () => {
|
|
|
647
659
|
const prisma = {
|
|
648
660
|
chatThreadWatch: { findMany: async () => [] },
|
|
649
661
|
};
|
|
650
|
-
const service =
|
|
651
|
-
prisma as never,
|
|
652
|
-
vapid,
|
|
653
|
-
unusedFetch,
|
|
654
|
-
"title",
|
|
655
|
-
);
|
|
662
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
656
663
|
const result = await service.unsubscribe("a@x.com", "https://push/ok");
|
|
657
664
|
expect(result).toEqual({ ok: false, reason: "unavailable" });
|
|
658
665
|
});
|
|
@@ -667,12 +674,7 @@ describe("PushService.unsubscribe", () => {
|
|
|
667
674
|
},
|
|
668
675
|
},
|
|
669
676
|
};
|
|
670
|
-
const service =
|
|
671
|
-
prisma as never,
|
|
672
|
-
vapid,
|
|
673
|
-
unusedFetch,
|
|
674
|
-
"title",
|
|
675
|
-
);
|
|
677
|
+
const service = makeService(prisma as never, vapid, unusedFetch, "title");
|
|
676
678
|
const result = await service.unsubscribe("a@x.com", "https://push/ok");
|
|
677
679
|
expect(result).toEqual({ ok: true });
|
|
678
680
|
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/user-notification-prefs.integration.test.ts (ACM-4.1)
|
|
3
|
+
* UserNotificationPrefs reads and writes by memberId; the email primary key
|
|
4
|
+
* is resolved through the AccountMember lookup. Null-memberId rows are skipped.
|
|
5
|
+
*
|
|
6
|
+
* Requires DATABASE_URL_ADMIN_TEST; skips otherwise.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
|
+
import { resetMemberIdFixture } from "./member-id-test-support.ts";
|
|
12
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
13
|
+
import { UserNotificationPrefsService } from "./user-notification-prefs.ts";
|
|
14
|
+
|
|
15
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
16
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
17
|
+
|
|
18
|
+
describeOrSkip("UserNotificationPrefsService by memberId (integration)", () => {
|
|
19
|
+
let prisma: PrismaClient;
|
|
20
|
+
let service: UserNotificationPrefsService;
|
|
21
|
+
let aliceId: string;
|
|
22
|
+
let bobId: string;
|
|
23
|
+
|
|
24
|
+
beforeEach(async () => {
|
|
25
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
26
|
+
const f = await resetMemberIdFixture(prisma);
|
|
27
|
+
aliceId = f.alice.id;
|
|
28
|
+
bobId = f.bob.id;
|
|
29
|
+
service = new UserNotificationPrefsService(prisma);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
afterEach(async () => {
|
|
33
|
+
await prisma.$disconnect();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("get returns null when the member has no row", async () => {
|
|
37
|
+
expect(await service.getByMemberId(aliceId)).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("upsert creates the row stamped with memberId and the member's email", async () => {
|
|
41
|
+
const row = await service.upsertByMemberId(aliceId, {
|
|
42
|
+
reminderHourLocal: 7,
|
|
43
|
+
});
|
|
44
|
+
expect(row?.memberId).toBe(aliceId);
|
|
45
|
+
expect(row?.userEmail).toBe("alice@acme.com");
|
|
46
|
+
expect(row?.reminderHourLocal).toBe(7);
|
|
47
|
+
expect((await service.getByMemberId(aliceId))?.reminderHourLocal).toBe(7);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("upsert updates the existing row instead of duplicating", async () => {
|
|
51
|
+
await service.upsertByMemberId(aliceId, { reminderHourLocal: 7 });
|
|
52
|
+
await service.upsertByMemberId(aliceId, { autoFollowSessions: false });
|
|
53
|
+
expect(await prisma.userNotificationPrefs.count()).toBe(1);
|
|
54
|
+
const row = await service.getByMemberId(aliceId);
|
|
55
|
+
expect(row?.autoFollowSessions).toBe(false);
|
|
56
|
+
expect(row?.reminderHourLocal).toBe(7);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("upsert adopts an email-only legacy row for the same member", async () => {
|
|
60
|
+
await prisma.userNotificationPrefs.create({
|
|
61
|
+
data: { userEmail: "alice@acme.com", reminderHourLocal: 5 },
|
|
62
|
+
});
|
|
63
|
+
const row = await service.upsertByMemberId(aliceId, {});
|
|
64
|
+
expect(row?.memberId).toBe(aliceId);
|
|
65
|
+
expect(row?.reminderHourLocal).toBe(5);
|
|
66
|
+
expect(await prisma.userNotificationPrefs.count()).toBe(1);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("upsert for an unknown member id returns null and writes nothing", async () => {
|
|
70
|
+
expect(await service.upsertByMemberId("nope", {})).toBeNull();
|
|
71
|
+
expect(await prisma.userNotificationPrefs.count()).toBe(0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("listAutoFollowers skips null-memberId rows and resolves email via the lookup", async () => {
|
|
75
|
+
await service.upsertByMemberId(aliceId, {});
|
|
76
|
+
await service.upsertByMemberId(bobId, { autoFollowSessions: false });
|
|
77
|
+
await prisma.userNotificationPrefs.create({
|
|
78
|
+
data: { userEmail: "legacy@old.com" },
|
|
79
|
+
});
|
|
80
|
+
const followers = await service.listAutoFollowers();
|
|
81
|
+
expect(followers.map((f) => f.email)).toEqual(["alice@acme.com"]);
|
|
82
|
+
expect(followers[0]?.memberId).toBe(aliceId);
|
|
83
|
+
});
|
|
84
|
+
});
|