@app-vitals/agent-admin 1.106.0 → 1.106.2

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,68 @@
1
+ /**
2
+ * admin/src/member-id-stamp.ts
3
+ * Fills memberId on the email-keyed rows that were written before the email had
4
+ * an AccountMember (AgentMember, ChatThreadWatch, PushSubscription,
5
+ * UserNotificationPrefs). Every path that creates an AccountMember calls this
6
+ * with its own transaction client so the record and the stamping commit together.
7
+ */
8
+
9
+ import { normalizeEmail } from "./account-email.ts";
10
+ import type { PrismaTransactionClient } from "./prisma-tx.ts";
11
+
12
+ /** Sets memberId on the email's null rows in all four tables; returns the rows changed. */
13
+ export async function stampMemberId(
14
+ client: PrismaTransactionClient,
15
+ rawEmail: string,
16
+ memberId: string,
17
+ ): Promise<number> {
18
+ const email = {
19
+ equals: normalizeEmail(rawEmail),
20
+ mode: "insensitive",
21
+ } as const;
22
+ const data = { memberId };
23
+ const results = await Promise.all([
24
+ client.agentMember.updateMany({
25
+ where: { email, memberId: null },
26
+ data,
27
+ }),
28
+ client.chatThreadWatch.updateMany({
29
+ where: { userEmail: email, memberId: null },
30
+ data,
31
+ }),
32
+ client.pushSubscription.updateMany({
33
+ where: { userEmail: email, memberId: null },
34
+ data,
35
+ }),
36
+ client.userNotificationPrefs.updateMany({
37
+ where: { userEmail: email, memberId: null },
38
+ data,
39
+ }),
40
+ ]);
41
+ return results.reduce((n, r) => n + r.count, 0);
42
+ }
43
+
44
+ /**
45
+ * Deletes the member's rows in all four tables: those with this memberId, plus
46
+ * those matching the email whose memberId is still null. Rows stamped with a
47
+ * different memberId are left alone. Returns the rows deleted.
48
+ */
49
+ export async function deleteMemberRows(
50
+ client: PrismaTransactionClient,
51
+ rawEmail: string,
52
+ memberId: string,
53
+ ): Promise<number> {
54
+ const email = {
55
+ equals: normalizeEmail(rawEmail),
56
+ mode: "insensitive",
57
+ } as const;
58
+ const match = (emailField: "email" | "userEmail") => ({
59
+ OR: [{ memberId }, { [emailField]: email, memberId: null }],
60
+ });
61
+ const results = await Promise.all([
62
+ client.agentMember.deleteMany({ where: match("email") }),
63
+ client.chatThreadWatch.deleteMany({ where: match("userEmail") }),
64
+ client.pushSubscription.deleteMany({ where: match("userEmail") }),
65
+ client.userNotificationPrefs.deleteMany({ where: match("userEmail") }),
66
+ ]);
67
+ return results.reduce((n, r) => n + r.count, 0);
68
+ }
@@ -120,6 +120,18 @@ describeOrSkip("PushService by memberId (integration)", () => {
120
120
  expect(row.memberId).toBe(bobId);
121
121
  });
122
122
 
123
+ it("subscribe for an email with no member record stores nothing", async () => {
124
+ expect(
125
+ await service.subscribe(
126
+ "ghost@nowhere.com",
127
+ "https://p/g",
128
+ P256DH,
129
+ AUTH,
130
+ ),
131
+ ).toEqual({ ok: false, reason: "no_member" });
132
+ expect(await prisma.pushSubscription.count()).toBe(0);
133
+ });
134
+
123
135
  it("sendToUsers resolves emails to members and skips null-memberId subscriptions", async () => {
124
136
  await service.subscribe(
125
137
  "alice@acme.com",
@@ -84,12 +84,12 @@ interface PushPrismaLike {
84
84
  };
85
85
  }
86
86
 
87
- /** Outcome of PushService.subscribe — distinguishes the three response shapes
88
- * the route handler maps to HTTP status (503 unavailable / 500 store_failed /
89
- * 200 ok). */
87
+ /** Outcome of PushService.subscribe — distinguishes the response shapes the
88
+ * route handler maps to HTTP status (503 unavailable / 409 no_member /
89
+ * 500 store_failed / 200 ok). */
90
90
  type PushSubscribeResult =
91
91
  | { ok: true }
92
- | { ok: false; reason: "unavailable" | "store_failed" };
92
+ | { ok: false; reason: "unavailable" | "store_failed" | "no_member" };
93
93
 
94
94
  /** Outcome of PushService.unsubscribe — delete failures are swallowed/logged
95
95
  * inside the service (best-effort), so only "unavailable" (model missing) is
@@ -344,6 +344,9 @@ export class PushService {
344
344
  return { ok: false, reason: "unavailable" };
345
345
  try {
346
346
  const memberId = await this.memberIdForEmail(userEmail);
347
+ // No account member record: nothing to attach the subscription to, so
348
+ // store nothing and let the caller say why.
349
+ if (!memberId) return { ok: false, reason: "no_member" };
347
350
  await this.prisma.pushSubscription.upsert({
348
351
  where: { endpoint },
349
352
  create: { userEmail, memberId, endpoint, p256dh, auth },
@@ -597,6 +597,30 @@ describe("PushService.subscribe", () => {
597
597
  ]);
598
598
  });
599
599
 
600
+ it("returns reason:no_member and stores nothing when the email has no member record", async () => {
601
+ const calls: unknown[] = [];
602
+ const prisma = {
603
+ chatThreadWatch: { findMany: async () => [] },
604
+ pushSubscription: {
605
+ findMany: async () => [],
606
+ deleteMany: async () => ({ count: 0 }),
607
+ upsert: async (args: unknown) => {
608
+ calls.push(args);
609
+ return { id: "sub_1" };
610
+ },
611
+ },
612
+ };
613
+ const service = makeService(prisma as never, vapid, unusedFetch, "title");
614
+ const result = await service.subscribe(
615
+ "nobody@x.com",
616
+ "https://push/ok",
617
+ P256DH,
618
+ AUTH,
619
+ );
620
+ expect(result).toEqual({ ok: false, reason: "no_member" });
621
+ expect(calls).toEqual([]);
622
+ });
623
+
600
624
  it("returns reason:unavailable when the pushSubscription model is absent", async () => {
601
625
  const prisma = {
602
626
  chatThreadWatch: { findMany: async () => [] },
@@ -126,6 +126,16 @@ export function renderPushToggle(args: PushToggleArgs): string {
126
126
  window.location.href = '/admin/login?returnTo=' + encodeURIComponent(location.pathname + location.search);
127
127
  return;
128
128
  }
129
+ if (res && res.status === 409) {
130
+ btn.disabled = false;
131
+ setStatus("Notifications can't be enabled: your account has no member record. Ask an owner or admin to add you.");
132
+ return;
133
+ }
134
+ if (res && !res.ok) {
135
+ btn.disabled = false;
136
+ setStatus("Notifications couldn't be enabled. Please try again.");
137
+ return;
138
+ }
129
139
  setStatus('Notifications enabled.');
130
140
  btn.textContent = 'Notifications on';
131
141
  });