@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.
@@ -0,0 +1,77 @@
1
+ /**
2
+ * admin/src/user-notification-prefs.ts
3
+ * UserNotificationPrefsService (ACM-4.1): per-user notification preferences
4
+ * read and written by memberId (AccountMember.id). The row's primary key stays
5
+ * userEmail for now, so writes resolve the member's email through the
6
+ * AccountMember lookup. Rows with a null memberId are skipped on reads.
7
+ */
8
+
9
+ import type {
10
+ PrismaClient,
11
+ UserNotificationPrefs,
12
+ } from "../prisma/client/client.ts";
13
+ import { PrincipalLookupService } from "./principal-lookup.ts";
14
+
15
+ export type { UserNotificationPrefs };
16
+
17
+ export interface UserNotificationPrefsPatch {
18
+ autoFollowSessions?: boolean;
19
+ reminderHourLocal?: number;
20
+ autoFollowSince?: Date | null;
21
+ }
22
+
23
+ export class UserNotificationPrefsService {
24
+ private readonly lookup: PrincipalLookupService;
25
+
26
+ constructor(private prisma: PrismaClient) {
27
+ this.lookup = new PrincipalLookupService(prisma);
28
+ }
29
+
30
+ async getByMemberId(memberId: string): Promise<UserNotificationPrefs | null> {
31
+ return this.prisma.userNotificationPrefs.findFirst({
32
+ where: { memberId },
33
+ });
34
+ }
35
+
36
+ /**
37
+ * Creates or updates the member's row. Returns null when `memberId` does not
38
+ * resolve to a user (no email to key the row on). An email-only legacy row
39
+ * for the same member is adopted rather than duplicated.
40
+ */
41
+ async upsertByMemberId(
42
+ memberId: string,
43
+ patch: UserNotificationPrefsPatch,
44
+ ): Promise<UserNotificationPrefs | null> {
45
+ const { principals } = await this.lookup.resolveMany([memberId]);
46
+ const email = principals.find((p) => p.type === "user")?.email;
47
+ if (!email) return null;
48
+ const existing = await this.getByMemberId(memberId);
49
+ return this.prisma.userNotificationPrefs.upsert({
50
+ where: { userEmail: existing?.userEmail ?? email },
51
+ create: { userEmail: email, memberId, ...patch },
52
+ update: { memberId, ...patch },
53
+ });
54
+ }
55
+
56
+ /**
57
+ * Members with auto-follow on, each with their email resolved through the
58
+ * lookup. Null-memberId rows and members that no longer resolve are skipped.
59
+ */
60
+ async listAutoFollowers(): Promise<
61
+ Array<{ memberId: string; email: string; prefs: UserNotificationPrefs }>
62
+ > {
63
+ const rows = await this.prisma.userNotificationPrefs.findMany({
64
+ where: { autoFollowSessions: true, memberId: { not: null } },
65
+ });
66
+ const { principals } = await this.lookup.resolveMany(
67
+ rows.flatMap((r) => (r.memberId ? [r.memberId] : [])),
68
+ );
69
+ const emails = new Map(principals.map((p) => [p.id, p.email]));
70
+ return rows.flatMap((prefs) => {
71
+ const email = prefs.memberId ? emails.get(prefs.memberId) : null;
72
+ return prefs.memberId && email
73
+ ? [{ memberId: prefs.memberId, email, prefs }]
74
+ : [];
75
+ });
76
+ }
77
+ }
@@ -1,255 +0,0 @@
1
- /**
2
- * agent/src/agent-members.unit.test.ts
3
- * Unit tests for AgentMemberService (admin/src/agent-members.ts) — pure logic
4
- * against an injected prisma test double. No real DB — see docs/testing.md
5
- * for the unit-layer contract.
6
- */
7
-
8
- import { describe, expect, it } from "bun:test";
9
- import { AgentMemberService } from "./agent-members.ts";
10
-
11
- // ─── In-memory prisma.agentMember test double ──────────────────────────────
12
-
13
- interface FakeAgentMemberRow {
14
- id: string;
15
- agentId: string;
16
- email: string;
17
- memberId: string | null;
18
- createdAt: Date;
19
- }
20
-
21
- function makeFakePrisma(seed: FakeAgentMemberRow[] = []) {
22
- const rows = new Map<string, FakeAgentMemberRow>(seed.map((r) => [r.id, r]));
23
- let nextId = 1;
24
-
25
- const agentMember = {
26
- async findMany({
27
- where,
28
- }: {
29
- where?: { agentId?: string; email?: string };
30
- } = {}): Promise<FakeAgentMemberRow[]> {
31
- let all = Array.from(rows.values());
32
- if (where?.agentId !== undefined) {
33
- all = all.filter((r) => r.agentId === where.agentId);
34
- }
35
- if (where?.email !== undefined) {
36
- all = all.filter((r) => r.email === where.email);
37
- }
38
- return all;
39
- },
40
- async findUnique({
41
- where,
42
- }: {
43
- where: { agentId_email: { agentId: string; email: string } };
44
- }): Promise<FakeAgentMemberRow | null> {
45
- const { agentId, email } = where.agentId_email;
46
- const row = Array.from(rows.values()).find(
47
- (r) => r.agentId === agentId && r.email === email,
48
- );
49
- return row ?? null;
50
- },
51
- async create({
52
- data,
53
- }: {
54
- data: { agentId: string; email: string };
55
- }): Promise<FakeAgentMemberRow> {
56
- const existing = Array.from(rows.values()).find(
57
- (r) => r.agentId === data.agentId && r.email === data.email,
58
- );
59
- if (existing) {
60
- throw new Error(
61
- "Unique constraint failed on the fields: (`agentId`,`email`)",
62
- );
63
- }
64
- const row: FakeAgentMemberRow = {
65
- id: `member-${nextId++}`,
66
- agentId: data.agentId,
67
- email: data.email,
68
- memberId: null,
69
- createdAt: new Date("2024-01-01"),
70
- };
71
- rows.set(row.id, row);
72
- return row;
73
- },
74
- async deleteMany({
75
- where,
76
- }: {
77
- where: { id: string; agentId: string };
78
- }): Promise<{ count: number }> {
79
- const row = rows.get(where.id);
80
- if (row && row.agentId === where.agentId) {
81
- rows.delete(where.id);
82
- return { count: 1 };
83
- }
84
- return { count: 0 };
85
- },
86
- };
87
-
88
- return { agentMember, __rows: rows };
89
- }
90
-
91
- type FakePrisma = ReturnType<typeof makeFakePrisma>;
92
-
93
- function seedRow(
94
- overrides: Partial<FakeAgentMemberRow> = {},
95
- ): FakeAgentMemberRow {
96
- return {
97
- id: "member-existing",
98
- agentId: "agent-1",
99
- email: "person@example.com",
100
- memberId: null,
101
- createdAt: new Date("2024-01-01"),
102
- ...overrides,
103
- };
104
- }
105
-
106
- // ─── listByAgentId ──────────────────────────────────────────────────────────
107
-
108
- describe("AgentMemberService.listByAgentId", () => {
109
- it("returns all memberships for the given agentId", async () => {
110
- const prisma = makeFakePrisma([
111
- seedRow({ id: "m1", agentId: "agent-1", email: "a@example.com" }),
112
- seedRow({ id: "m2", agentId: "agent-1", email: "b@example.com" }),
113
- seedRow({ id: "m3", agentId: "agent-2", email: "c@example.com" }),
114
- ]) as unknown as FakePrisma;
115
- const service = new AgentMemberService(prisma as never);
116
-
117
- const result = await service.listByAgentId("agent-1");
118
-
119
- expect(result.map((r) => r.email).sort()).toEqual([
120
- "a@example.com",
121
- "b@example.com",
122
- ]);
123
- });
124
-
125
- it("returns an empty array when the agent has no memberships", async () => {
126
- const prisma = makeFakePrisma() as unknown as FakePrisma;
127
- const service = new AgentMemberService(prisma as never);
128
-
129
- expect(await service.listByAgentId("agent-1")).toEqual([]);
130
- });
131
- });
132
-
133
- // ─── listByEmail ────────────────────────────────────────────────────────────
134
-
135
- describe("AgentMemberService.listByEmail", () => {
136
- it("returns all memberships for the given email, case-sensitive as stored", async () => {
137
- const prisma = makeFakePrisma([
138
- seedRow({ id: "m1", agentId: "agent-1", email: "person@example.com" }),
139
- seedRow({ id: "m2", agentId: "agent-2", email: "person@example.com" }),
140
- seedRow({ id: "m3", agentId: "agent-3", email: "other@example.com" }),
141
- ]) as unknown as FakePrisma;
142
- const service = new AgentMemberService(prisma as never);
143
-
144
- const result = await service.listByEmail("person@example.com");
145
-
146
- expect(result.map((r) => r.agentId).sort()).toEqual(["agent-1", "agent-2"]);
147
- });
148
-
149
- it("does not match on a differently-cased email", async () => {
150
- const prisma = makeFakePrisma([
151
- seedRow({ id: "m1", agentId: "agent-1", email: "person@example.com" }),
152
- ]) as unknown as FakePrisma;
153
- const service = new AgentMemberService(prisma as never);
154
-
155
- expect(await service.listByEmail("Person@example.com")).toEqual([]);
156
- });
157
-
158
- it("returns an empty array when no memberships match", async () => {
159
- const prisma = makeFakePrisma() as unknown as FakePrisma;
160
- const service = new AgentMemberService(prisma as never);
161
-
162
- expect(await service.listByEmail("nobody@example.com")).toEqual([]);
163
- });
164
- });
165
-
166
- // ─── exists ─────────────────────────────────────────────────────────────────
167
-
168
- describe("AgentMemberService.exists", () => {
169
- it("returns true when a membership row exists for the agentId+email pair", async () => {
170
- const prisma = makeFakePrisma([
171
- seedRow({ agentId: "agent-1", email: "person@example.com" }),
172
- ]) as unknown as FakePrisma;
173
- const service = new AgentMemberService(prisma as never);
174
-
175
- expect(await service.exists("agent-1", "person@example.com")).toBe(true);
176
- });
177
-
178
- it("returns false when no membership row exists for the pair", async () => {
179
- const prisma = makeFakePrisma([
180
- seedRow({ agentId: "agent-1", email: "person@example.com" }),
181
- ]) as unknown as FakePrisma;
182
- const service = new AgentMemberService(prisma as never);
183
-
184
- expect(await service.exists("agent-1", "someone-else@example.com")).toBe(
185
- false,
186
- );
187
- expect(await service.exists("agent-2", "person@example.com")).toBe(false);
188
- });
189
-
190
- it("returns false when there are no memberships at all", async () => {
191
- const prisma = makeFakePrisma() as unknown as FakePrisma;
192
- const service = new AgentMemberService(prisma as never);
193
-
194
- expect(await service.exists("agent-1", "person@example.com")).toBe(false);
195
- });
196
- });
197
-
198
- // ─── add ────────────────────────────────────────────────────────────────────
199
-
200
- describe("AgentMemberService.add", () => {
201
- it("creates a membership row for the given agentId and email", async () => {
202
- const prisma = makeFakePrisma() as unknown as FakePrisma;
203
- const service = new AgentMemberService(prisma as never);
204
-
205
- const row = await service.add("agent-1", "person@example.com");
206
-
207
- expect(row.agentId).toBe("agent-1");
208
- expect(row.email).toBe("person@example.com");
209
- expect(row.id).toBeDefined();
210
- });
211
-
212
- it("propagates a unique-constraint error when the membership already exists", async () => {
213
- const prisma = makeFakePrisma([
214
- seedRow({ agentId: "agent-1", email: "person@example.com" }),
215
- ]) as unknown as FakePrisma;
216
- const service = new AgentMemberService(prisma as never);
217
-
218
- await expect(
219
- (async () => service.add("agent-1", "person@example.com"))(),
220
- ).rejects.toThrow();
221
- });
222
- });
223
-
224
- // ─── remove ─────────────────────────────────────────────────────────────────
225
-
226
- describe("AgentMemberService.remove", () => {
227
- it("deletes a membership by id when scoped to the correct agentId", async () => {
228
- const row = seedRow({ id: "m1", agentId: "agent-1" });
229
- const prisma = makeFakePrisma([row]) as unknown as FakePrisma;
230
- const service = new AgentMemberService(prisma as never);
231
-
232
- await service.remove("agent-1", "m1");
233
-
234
- expect(await service.listByAgentId("agent-1")).toEqual([]);
235
- });
236
-
237
- it("no-ops when the memberId does not exist", async () => {
238
- const prisma = makeFakePrisma() as unknown as FakePrisma;
239
- const service = new AgentMemberService(prisma as never);
240
-
241
- await service.remove("agent-1", "missing");
242
-
243
- expect(await service.listByAgentId("agent-1")).toEqual([]);
244
- });
245
-
246
- it("no-ops when the memberId exists but belongs to a different agentId", async () => {
247
- const row = seedRow({ id: "m1", agentId: "agent-1" });
248
- const prisma = makeFakePrisma([row]) as unknown as FakePrisma;
249
- const service = new AgentMemberService(prisma as never);
250
-
251
- await service.remove("agent-2", "m1");
252
-
253
- expect(await service.listByAgentId("agent-1")).toEqual([row]);
254
- });
255
- });