@app-vitals/agent-admin 1.111.0 → 1.112.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.
@@ -3351,6 +3351,7 @@ export type ChatThreadWatchScalarFieldEnum = (typeof ChatThreadWatchScalarFieldE
3351
3351
 
3352
3352
  export const UserNotificationPrefsScalarFieldEnum = {
3353
3353
  memberId: 'memberId',
3354
+ timezone: 'timezone',
3354
3355
  autoFollowSessions: 'autoFollowSessions',
3355
3356
  reminderHourLocal: 'reminderHourLocal',
3356
3357
  autoFollowSince: 'autoFollowSince',
@@ -523,6 +523,7 @@ export type ChatThreadWatchScalarFieldEnum = (typeof ChatThreadWatchScalarFieldE
523
523
 
524
524
  export const UserNotificationPrefsScalarFieldEnum = {
525
525
  memberId: 'memberId',
526
+ timezone: 'timezone',
526
527
  autoFollowSessions: 'autoFollowSessions',
527
528
  reminderHourLocal: 'reminderHourLocal',
528
529
  autoFollowSince: 'autoFollowSince',
@@ -36,6 +36,7 @@ export type UserNotificationPrefsSumAggregateOutputType = {
36
36
 
37
37
  export type UserNotificationPrefsMinAggregateOutputType = {
38
38
  memberId: string | null
39
+ timezone: string | null
39
40
  autoFollowSessions: boolean | null
40
41
  reminderHourLocal: number | null
41
42
  autoFollowSince: Date | null
@@ -45,6 +46,7 @@ export type UserNotificationPrefsMinAggregateOutputType = {
45
46
 
46
47
  export type UserNotificationPrefsMaxAggregateOutputType = {
47
48
  memberId: string | null
49
+ timezone: string | null
48
50
  autoFollowSessions: boolean | null
49
51
  reminderHourLocal: number | null
50
52
  autoFollowSince: Date | null
@@ -54,6 +56,7 @@ export type UserNotificationPrefsMaxAggregateOutputType = {
54
56
 
55
57
  export type UserNotificationPrefsCountAggregateOutputType = {
56
58
  memberId: number
59
+ timezone: number
57
60
  autoFollowSessions: number
58
61
  reminderHourLocal: number
59
62
  autoFollowSince: number
@@ -73,6 +76,7 @@ export type UserNotificationPrefsSumAggregateInputType = {
73
76
 
74
77
  export type UserNotificationPrefsMinAggregateInputType = {
75
78
  memberId?: true
79
+ timezone?: true
76
80
  autoFollowSessions?: true
77
81
  reminderHourLocal?: true
78
82
  autoFollowSince?: true
@@ -82,6 +86,7 @@ export type UserNotificationPrefsMinAggregateInputType = {
82
86
 
83
87
  export type UserNotificationPrefsMaxAggregateInputType = {
84
88
  memberId?: true
89
+ timezone?: true
85
90
  autoFollowSessions?: true
86
91
  reminderHourLocal?: true
87
92
  autoFollowSince?: true
@@ -91,6 +96,7 @@ export type UserNotificationPrefsMaxAggregateInputType = {
91
96
 
92
97
  export type UserNotificationPrefsCountAggregateInputType = {
93
98
  memberId?: true
99
+ timezone?: true
94
100
  autoFollowSessions?: true
95
101
  reminderHourLocal?: true
96
102
  autoFollowSince?: true
@@ -187,6 +193,7 @@ export type UserNotificationPrefsGroupByArgs<ExtArgs extends runtime.Types.Exten
187
193
 
188
194
  export type UserNotificationPrefsGroupByOutputType = {
189
195
  memberId: string
196
+ timezone: string | null
190
197
  autoFollowSessions: boolean
191
198
  reminderHourLocal: number
192
199
  autoFollowSince: Date | null
@@ -219,6 +226,7 @@ export type UserNotificationPrefsWhereInput = {
219
226
  OR?: Prisma.UserNotificationPrefsWhereInput[]
220
227
  NOT?: Prisma.UserNotificationPrefsWhereInput | Prisma.UserNotificationPrefsWhereInput[]
221
228
  memberId?: Prisma.StringFilter<"UserNotificationPrefs"> | string
229
+ timezone?: Prisma.StringNullableFilter<"UserNotificationPrefs"> | string | null
222
230
  autoFollowSessions?: Prisma.BoolFilter<"UserNotificationPrefs"> | boolean
223
231
  reminderHourLocal?: Prisma.IntFilter<"UserNotificationPrefs"> | number
224
232
  autoFollowSince?: Prisma.DateTimeNullableFilter<"UserNotificationPrefs"> | Date | string | null
@@ -228,6 +236,7 @@ export type UserNotificationPrefsWhereInput = {
228
236
 
229
237
  export type UserNotificationPrefsOrderByWithRelationInput = {
230
238
  memberId?: Prisma.SortOrder
239
+ timezone?: Prisma.SortOrderInput | Prisma.SortOrder
231
240
  autoFollowSessions?: Prisma.SortOrder
232
241
  reminderHourLocal?: Prisma.SortOrder
233
242
  autoFollowSince?: Prisma.SortOrderInput | Prisma.SortOrder
@@ -240,6 +249,7 @@ export type UserNotificationPrefsWhereUniqueInput = Prisma.AtLeast<{
240
249
  AND?: Prisma.UserNotificationPrefsWhereInput | Prisma.UserNotificationPrefsWhereInput[]
241
250
  OR?: Prisma.UserNotificationPrefsWhereInput[]
242
251
  NOT?: Prisma.UserNotificationPrefsWhereInput | Prisma.UserNotificationPrefsWhereInput[]
252
+ timezone?: Prisma.StringNullableFilter<"UserNotificationPrefs"> | string | null
243
253
  autoFollowSessions?: Prisma.BoolFilter<"UserNotificationPrefs"> | boolean
244
254
  reminderHourLocal?: Prisma.IntFilter<"UserNotificationPrefs"> | number
245
255
  autoFollowSince?: Prisma.DateTimeNullableFilter<"UserNotificationPrefs"> | Date | string | null
@@ -249,6 +259,7 @@ export type UserNotificationPrefsWhereUniqueInput = Prisma.AtLeast<{
249
259
 
250
260
  export type UserNotificationPrefsOrderByWithAggregationInput = {
251
261
  memberId?: Prisma.SortOrder
262
+ timezone?: Prisma.SortOrderInput | Prisma.SortOrder
252
263
  autoFollowSessions?: Prisma.SortOrder
253
264
  reminderHourLocal?: Prisma.SortOrder
254
265
  autoFollowSince?: Prisma.SortOrderInput | Prisma.SortOrder
@@ -266,6 +277,7 @@ export type UserNotificationPrefsScalarWhereWithAggregatesInput = {
266
277
  OR?: Prisma.UserNotificationPrefsScalarWhereWithAggregatesInput[]
267
278
  NOT?: Prisma.UserNotificationPrefsScalarWhereWithAggregatesInput | Prisma.UserNotificationPrefsScalarWhereWithAggregatesInput[]
268
279
  memberId?: Prisma.StringWithAggregatesFilter<"UserNotificationPrefs"> | string
280
+ timezone?: Prisma.StringNullableWithAggregatesFilter<"UserNotificationPrefs"> | string | null
269
281
  autoFollowSessions?: Prisma.BoolWithAggregatesFilter<"UserNotificationPrefs"> | boolean
270
282
  reminderHourLocal?: Prisma.IntWithAggregatesFilter<"UserNotificationPrefs"> | number
271
283
  autoFollowSince?: Prisma.DateTimeNullableWithAggregatesFilter<"UserNotificationPrefs"> | Date | string | null
@@ -275,6 +287,7 @@ export type UserNotificationPrefsScalarWhereWithAggregatesInput = {
275
287
 
276
288
  export type UserNotificationPrefsCreateInput = {
277
289
  memberId: string
290
+ timezone?: string | null
278
291
  autoFollowSessions?: boolean
279
292
  reminderHourLocal?: number
280
293
  autoFollowSince?: Date | string | null
@@ -284,6 +297,7 @@ export type UserNotificationPrefsCreateInput = {
284
297
 
285
298
  export type UserNotificationPrefsUncheckedCreateInput = {
286
299
  memberId: string
300
+ timezone?: string | null
287
301
  autoFollowSessions?: boolean
288
302
  reminderHourLocal?: number
289
303
  autoFollowSince?: Date | string | null
@@ -293,6 +307,7 @@ export type UserNotificationPrefsUncheckedCreateInput = {
293
307
 
294
308
  export type UserNotificationPrefsUpdateInput = {
295
309
  memberId?: Prisma.StringFieldUpdateOperationsInput | string
310
+ timezone?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
296
311
  autoFollowSessions?: Prisma.BoolFieldUpdateOperationsInput | boolean
297
312
  reminderHourLocal?: Prisma.IntFieldUpdateOperationsInput | number
298
313
  autoFollowSince?: Prisma.NullableDateTimeFieldUpdateOperationsInput | Date | string | null
@@ -302,6 +317,7 @@ export type UserNotificationPrefsUpdateInput = {
302
317
 
303
318
  export type UserNotificationPrefsUncheckedUpdateInput = {
304
319
  memberId?: Prisma.StringFieldUpdateOperationsInput | string
320
+ timezone?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
305
321
  autoFollowSessions?: Prisma.BoolFieldUpdateOperationsInput | boolean
306
322
  reminderHourLocal?: Prisma.IntFieldUpdateOperationsInput | number
307
323
  autoFollowSince?: Prisma.NullableDateTimeFieldUpdateOperationsInput | Date | string | null
@@ -311,6 +327,7 @@ export type UserNotificationPrefsUncheckedUpdateInput = {
311
327
 
312
328
  export type UserNotificationPrefsCreateManyInput = {
313
329
  memberId: string
330
+ timezone?: string | null
314
331
  autoFollowSessions?: boolean
315
332
  reminderHourLocal?: number
316
333
  autoFollowSince?: Date | string | null
@@ -320,6 +337,7 @@ export type UserNotificationPrefsCreateManyInput = {
320
337
 
321
338
  export type UserNotificationPrefsUpdateManyMutationInput = {
322
339
  memberId?: Prisma.StringFieldUpdateOperationsInput | string
340
+ timezone?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
323
341
  autoFollowSessions?: Prisma.BoolFieldUpdateOperationsInput | boolean
324
342
  reminderHourLocal?: Prisma.IntFieldUpdateOperationsInput | number
325
343
  autoFollowSince?: Prisma.NullableDateTimeFieldUpdateOperationsInput | Date | string | null
@@ -329,6 +347,7 @@ export type UserNotificationPrefsUpdateManyMutationInput = {
329
347
 
330
348
  export type UserNotificationPrefsUncheckedUpdateManyInput = {
331
349
  memberId?: Prisma.StringFieldUpdateOperationsInput | string
350
+ timezone?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
332
351
  autoFollowSessions?: Prisma.BoolFieldUpdateOperationsInput | boolean
333
352
  reminderHourLocal?: Prisma.IntFieldUpdateOperationsInput | number
334
353
  autoFollowSince?: Prisma.NullableDateTimeFieldUpdateOperationsInput | Date | string | null
@@ -338,6 +357,7 @@ export type UserNotificationPrefsUncheckedUpdateManyInput = {
338
357
 
339
358
  export type UserNotificationPrefsCountOrderByAggregateInput = {
340
359
  memberId?: Prisma.SortOrder
360
+ timezone?: Prisma.SortOrder
341
361
  autoFollowSessions?: Prisma.SortOrder
342
362
  reminderHourLocal?: Prisma.SortOrder
343
363
  autoFollowSince?: Prisma.SortOrder
@@ -351,6 +371,7 @@ export type UserNotificationPrefsAvgOrderByAggregateInput = {
351
371
 
352
372
  export type UserNotificationPrefsMaxOrderByAggregateInput = {
353
373
  memberId?: Prisma.SortOrder
374
+ timezone?: Prisma.SortOrder
354
375
  autoFollowSessions?: Prisma.SortOrder
355
376
  reminderHourLocal?: Prisma.SortOrder
356
377
  autoFollowSince?: Prisma.SortOrder
@@ -360,6 +381,7 @@ export type UserNotificationPrefsMaxOrderByAggregateInput = {
360
381
 
361
382
  export type UserNotificationPrefsMinOrderByAggregateInput = {
362
383
  memberId?: Prisma.SortOrder
384
+ timezone?: Prisma.SortOrder
363
385
  autoFollowSessions?: Prisma.SortOrder
364
386
  reminderHourLocal?: Prisma.SortOrder
365
387
  autoFollowSince?: Prisma.SortOrder
@@ -375,6 +397,7 @@ export type UserNotificationPrefsSumOrderByAggregateInput = {
375
397
 
376
398
  export type UserNotificationPrefsSelect<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetSelect<{
377
399
  memberId?: boolean
400
+ timezone?: boolean
378
401
  autoFollowSessions?: boolean
379
402
  reminderHourLocal?: boolean
380
403
  autoFollowSince?: boolean
@@ -384,6 +407,7 @@ export type UserNotificationPrefsSelect<ExtArgs extends runtime.Types.Extensions
384
407
 
385
408
  export type UserNotificationPrefsSelectCreateManyAndReturn<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetSelect<{
386
409
  memberId?: boolean
410
+ timezone?: boolean
387
411
  autoFollowSessions?: boolean
388
412
  reminderHourLocal?: boolean
389
413
  autoFollowSince?: boolean
@@ -393,6 +417,7 @@ export type UserNotificationPrefsSelectCreateManyAndReturn<ExtArgs extends runti
393
417
 
394
418
  export type UserNotificationPrefsSelectUpdateManyAndReturn<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetSelect<{
395
419
  memberId?: boolean
420
+ timezone?: boolean
396
421
  autoFollowSessions?: boolean
397
422
  reminderHourLocal?: boolean
398
423
  autoFollowSince?: boolean
@@ -402,6 +427,7 @@ export type UserNotificationPrefsSelectUpdateManyAndReturn<ExtArgs extends runti
402
427
 
403
428
  export type UserNotificationPrefsSelectScalar = {
404
429
  memberId?: boolean
430
+ timezone?: boolean
405
431
  autoFollowSessions?: boolean
406
432
  reminderHourLocal?: boolean
407
433
  autoFollowSince?: boolean
@@ -409,13 +435,14 @@ export type UserNotificationPrefsSelectScalar = {
409
435
  updatedAt?: boolean
410
436
  }
411
437
 
412
- export type UserNotificationPrefsOmit<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetOmit<"memberId" | "autoFollowSessions" | "reminderHourLocal" | "autoFollowSince" | "createdAt" | "updatedAt", ExtArgs["result"]["userNotificationPrefs"]>
438
+ export type UserNotificationPrefsOmit<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetOmit<"memberId" | "timezone" | "autoFollowSessions" | "reminderHourLocal" | "autoFollowSince" | "createdAt" | "updatedAt", ExtArgs["result"]["userNotificationPrefs"]>
413
439
 
414
440
  export type $UserNotificationPrefsPayload<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = {
415
441
  name: "UserNotificationPrefs"
416
442
  objects: {}
417
443
  scalars: runtime.Types.Extensions.GetPayloadResult<{
418
444
  memberId: string
445
+ timezone: string | null
419
446
  autoFollowSessions: boolean
420
447
  reminderHourLocal: number
421
448
  autoFollowSince: Date | null
@@ -845,6 +872,7 @@ export interface Prisma__UserNotificationPrefsClient<T, Null = never, ExtArgs ex
845
872
  */
846
873
  export interface UserNotificationPrefsFieldRefs {
847
874
  readonly memberId: Prisma.FieldRef<"UserNotificationPrefs", 'String'>
875
+ readonly timezone: Prisma.FieldRef<"UserNotificationPrefs", 'String'>
848
876
  readonly autoFollowSessions: Prisma.FieldRef<"UserNotificationPrefs", 'Boolean'>
849
877
  readonly reminderHourLocal: Prisma.FieldRef<"UserNotificationPrefs", 'Int'>
850
878
  readonly autoFollowSince: Prisma.FieldRef<"UserNotificationPrefs", 'DateTime'>
@@ -0,0 +1,4 @@
1
+ -- Rename in place (no data copy) and add a nullable IANA timezone column.
2
+ ALTER TABLE "UserNotificationPrefs" RENAME TO "UserPrefs";
3
+ ALTER TABLE "UserPrefs" RENAME CONSTRAINT "UserNotificationPrefs_pkey" TO "UserPrefs_pkey";
4
+ ALTER TABLE "UserPrefs" ADD COLUMN "timezone" TEXT;
@@ -665,11 +665,14 @@ model ChatThreadWatch {
665
665
  // has no native CHECK constraint support in this schema style.
666
666
  model UserNotificationPrefs {
667
667
  memberId String @id // AccountMember.id
668
+ timezone String? // IANA zone name, validated in UserPrefsService
668
669
  autoFollowSessions Boolean @default(true)
669
670
  reminderHourLocal Int @default(9)
670
671
  autoFollowSince DateTime?
671
672
  createdAt DateTime @default(now())
672
673
  updatedAt DateTime @updatedAt
674
+
675
+ @@map("UserPrefs") // model rename to UserPrefs is deferred to ACM 4.1
673
676
  }
674
677
 
675
678
 
@@ -85,10 +85,6 @@ describeOrSkip("drop_email_key_columns migration (ACM-5.1)", () => {
85
85
  await client.end();
86
86
  });
87
87
 
88
- it("is the last migration in apply order", () => {
89
- expect(ALL_MIGRATIONS.at(-1)).toBe(DROP);
90
- });
91
-
92
88
  it("aborts, changing nothing, while any row has a NULL memberId", async () => {
93
89
  await seed(null);
94
90
  await expect(client.query(migrationSql(DROP))).rejects.toThrow(
@@ -93,13 +93,13 @@ describeOrSkip("/principals API (integration)", () => {
93
93
  }[]
94
94
  >`SELECT table_name, is_nullable, column_default FROM information_schema.columns
95
95
  WHERE column_name = 'memberId'
96
- AND table_name IN ('UserNotificationPrefs','ChatThreadWatch','PushSubscription','AgentMember')
96
+ AND table_name IN ('UserPrefs','ChatThreadWatch','PushSubscription','AgentMember')
97
97
  ORDER BY table_name`;
98
98
  expect(cols.map((c) => c.table_name)).toEqual([
99
99
  "AgentMember",
100
100
  "ChatThreadWatch",
101
101
  "PushSubscription",
102
- "UserNotificationPrefs",
102
+ "UserPrefs",
103
103
  ]);
104
104
  for (const c of cols) {
105
105
  expect(c.is_nullable).toBe("NO");
@@ -110,7 +110,7 @@ describeOrSkip("/principals API (integration)", () => {
110
110
  JOIN information_schema.constraint_column_usage ccu USING (constraint_name)
111
111
  WHERE ccu.column_name = 'memberId'
112
112
  AND tc.constraint_type = 'FOREIGN KEY'
113
- AND tc.table_name IN ('UserNotificationPrefs','ChatThreadWatch','PushSubscription','AgentMember')`;
113
+ AND tc.table_name IN ('UserPrefs','ChatThreadWatch','PushSubscription','AgentMember')`;
114
114
  expect(Number(fks[0]?.n)).toBe(0);
115
115
  });
116
116
  });
@@ -1,71 +1,16 @@
1
1
  /**
2
2
  * admin/src/user-notification-prefs.ts
3
- * UserNotificationPrefsService (ACM-4.1): per-user notification preferences
4
- * read and written by memberId (AccountMember.id), which is the row's primary
5
- * key (ACM-5.1 dropped the userEmail column).
3
+ * @deprecated Renamed to ./user-prefs.ts. These aliases keep existing
4
+ * consumers compiling; remove once none remain.
6
5
  */
7
6
 
8
- import type {
9
- PrismaClient,
10
- UserNotificationPrefs,
11
- } from "../prisma/client/client.ts";
12
- import { PrincipalLookupService } from "./principal-lookup.ts";
7
+ import { UserPrefsService, type UserPrefsPatch } from "./user-prefs.ts";
13
8
 
14
- export type { UserNotificationPrefs };
9
+ export type { UserNotificationPrefs } from "./user-prefs.ts";
15
10
 
16
- export interface UserNotificationPrefsPatch {
17
- autoFollowSessions?: boolean;
18
- reminderHourLocal?: number;
19
- autoFollowSince?: Date | null;
20
- }
21
-
22
- export class UserNotificationPrefsService {
23
- private readonly lookup: PrincipalLookupService;
24
-
25
- constructor(private prisma: PrismaClient) {
26
- this.lookup = new PrincipalLookupService(prisma);
27
- }
28
-
29
- async getByMemberId(memberId: string): Promise<UserNotificationPrefs | null> {
30
- return this.prisma.userNotificationPrefs.findFirst({
31
- where: { memberId },
32
- });
33
- }
34
-
35
- /**
36
- * Creates or updates the member's row. Returns null when `memberId` does not
37
- * resolve to a user.
38
- */
39
- async upsertByMemberId(
40
- memberId: string,
41
- patch: UserNotificationPrefsPatch,
42
- ): Promise<UserNotificationPrefs | null> {
43
- const { principals } = await this.lookup.resolveMany([memberId]);
44
- if (!principals.some((p) => p.type === "user")) return null;
45
- return this.prisma.userNotificationPrefs.upsert({
46
- where: { memberId },
47
- create: { memberId, ...patch },
48
- update: patch,
49
- });
50
- }
51
-
52
- /**
53
- * Members with auto-follow on, each with their email resolved through the
54
- * lookup. Members that no longer resolve are skipped.
55
- */
56
- async listAutoFollowers(): Promise<
57
- Array<{ memberId: string; email: string; prefs: UserNotificationPrefs }>
58
- > {
59
- const rows = await this.prisma.userNotificationPrefs.findMany({
60
- where: { autoFollowSessions: true },
61
- });
62
- const { principals } = await this.lookup.resolveMany(
63
- rows.map((r) => r.memberId),
64
- );
65
- const emails = new Map(principals.map((p) => [p.id, p.email]));
66
- return rows.flatMap((prefs) => {
67
- const email = emails.get(prefs.memberId);
68
- return email ? [{ memberId: prefs.memberId, email, prefs }] : [];
69
- });
70
- }
71
- }
11
+ /** @deprecated Use UserPrefsService. */
12
+ export const UserNotificationPrefsService = UserPrefsService;
13
+ /** @deprecated Use UserPrefsService. */
14
+ export type UserNotificationPrefsService = UserPrefsService;
15
+ /** @deprecated Use UserPrefsPatch. */
16
+ export type UserNotificationPrefsPatch = UserPrefsPatch;
@@ -0,0 +1,9 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import * as legacy from "./user-notification-prefs.ts";
3
+ import * as current from "./user-prefs.ts";
4
+
5
+ describe("user-notification-prefs deprecated aliases", () => {
6
+ it("UserNotificationPrefsService is UserPrefsService", () => {
7
+ expect(legacy.UserNotificationPrefsService).toBe(current.UserPrefsService);
8
+ });
9
+ });
@@ -0,0 +1,71 @@
1
+ /**
2
+ * admin/src/user-prefs-rename-migration.integration.test.ts
3
+ * Migration test for 20261201000000_rename_user_notification_prefs_to_user_prefs:
4
+ * the table is renamed in place (rows and memberId preserved) and a nullable
5
+ * timezone column is added.
6
+ *
7
+ * Requires DATABASE_URL_ADMIN_TEST; skips otherwise.
8
+ */
9
+
10
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
11
+ import { readdirSync, readFileSync } from "node:fs";
12
+ import { Client } from "pg";
13
+
14
+ const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
15
+ const describeOrSkip = TEST_DB ? describe : describe.skip;
16
+
17
+ const MIGRATIONS_DIR = new URL("../prisma/migrations/", import.meta.url);
18
+ const RENAME = "20261201000000_rename_user_notification_prefs_to_user_prefs";
19
+
20
+ const ALL_MIGRATIONS = readdirSync(MIGRATIONS_DIR, { withFileTypes: true })
21
+ .filter((d) => d.isDirectory())
22
+ .map((d) => d.name)
23
+ .sort();
24
+
25
+ const migrationSql = (dir: string): string =>
26
+ readFileSync(new URL(`${dir}/migration.sql`, MIGRATIONS_DIR), "utf8");
27
+
28
+ describeOrSkip("rename UserNotificationPrefs to UserPrefs migration", () => {
29
+ const schema = `user_prefs_rename_${process.pid}_${Date.now()}`;
30
+ let client: Client;
31
+
32
+ beforeEach(async () => {
33
+ client = new Client({ connectionString: TEST_DB as string });
34
+ await client.connect();
35
+ await client.query(`CREATE SCHEMA "${schema}"`);
36
+ await client.query(`SET search_path TO "${schema}"`);
37
+ for (const dir of ALL_MIGRATIONS.slice(0, ALL_MIGRATIONS.indexOf(RENAME))) {
38
+ await client.query(migrationSql(dir));
39
+ }
40
+ });
41
+
42
+ afterEach(async () => {
43
+ await client.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
44
+ await client.end();
45
+ });
46
+
47
+ it("renames in place, preserving rows, and adds a nullable timezone", async () => {
48
+ await client.query(
49
+ `INSERT INTO "UserNotificationPrefs" ("memberId","reminderHourLocal","updatedAt")
50
+ VALUES ('m1',7,now()),('m2',9,now())`,
51
+ );
52
+ await client.query(migrationSql(RENAME));
53
+
54
+ const old = await client.query(
55
+ `SELECT to_regclass('"UserNotificationPrefs"') AS t`,
56
+ );
57
+ expect(old.rows[0].t).toBeNull();
58
+ const { rows } = await client.query(
59
+ `SELECT "memberId","reminderHourLocal","timezone" FROM "UserPrefs" ORDER BY "memberId"`,
60
+ );
61
+ expect(rows).toEqual([
62
+ { memberId: "m1", reminderHourLocal: 7, timezone: null },
63
+ { memberId: "m2", reminderHourLocal: 9, timezone: null },
64
+ ]);
65
+ await expect(
66
+ client.query(
67
+ `INSERT INTO "UserPrefs" ("memberId","updatedAt") VALUES ('m1',now())`,
68
+ ),
69
+ ).rejects.toThrow(/UserPrefs_pkey/);
70
+ });
71
+ });
@@ -1,5 +1,5 @@
1
1
  /**
2
- * admin/src/user-notification-prefs.integration.test.ts (ACM-4.1)
2
+ * admin/src/user-prefs.integration.test.ts (ACM-4.1)
3
3
  * UserNotificationPrefs reads and writes by memberId; memberId is the primary key;
4
4
  * email is resolved through the AccountMember lookup. Rows whose member no longer resolves are skipped.
5
5
  *
@@ -10,14 +10,14 @@ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
10
10
  import type { PrismaClient } from "../prisma/client/client.ts";
11
11
  import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
12
12
  import { createAdminPrismaClient } from "./prisma-client.ts";
13
- import { UserNotificationPrefsService } from "./user-notification-prefs.ts";
13
+ import { InvalidTimezoneError, UserPrefsService } from "./user-prefs.ts";
14
14
 
15
15
  const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
16
16
  const describeOrSkip = TEST_DB ? describe : describe.skip;
17
17
 
18
- describeOrSkip("UserNotificationPrefsService by memberId (integration)", () => {
18
+ describeOrSkip("UserPrefsService by memberId (integration)", () => {
19
19
  let prisma: PrismaClient;
20
- let service: UserNotificationPrefsService;
20
+ let service: UserPrefsService;
21
21
  let aliceId: string;
22
22
  let bobId: string;
23
23
 
@@ -26,7 +26,7 @@ describeOrSkip("UserNotificationPrefsService by memberId (integration)", () => {
26
26
  const f = await resetMemberIdFixture(prisma);
27
27
  aliceId = f.alice.id;
28
28
  bobId = f.bob.id;
29
- service = new UserNotificationPrefsService(prisma);
29
+ service = new UserPrefsService(prisma);
30
30
  });
31
31
 
32
32
  afterEach(async () => {
@@ -70,4 +70,21 @@ describeOrSkip("UserNotificationPrefsService by memberId (integration)", () => {
70
70
  expect(followers.map((f) => f.email)).toEqual(["alice@acme.com"]);
71
71
  expect(followers[0]?.memberId).toBe(aliceId);
72
72
  });
73
+
74
+ it("reads and writes timezone by memberId", async () => {
75
+ expect(await service.getTimezone(aliceId)).toBeNull();
76
+ await service.upsertByMemberId(aliceId, { timezone: "America/New_York" });
77
+ expect(await service.getTimezone(aliceId)).toBe("America/New_York");
78
+ await service.upsertByMemberId(aliceId, { timezone: null });
79
+ expect(await service.getTimezone(aliceId)).toBeNull();
80
+ });
81
+
82
+ it("rejects invalid IANA timezones without writing", async () => {
83
+ for (const tz of ["Mars/Olympus", "", " UTC", "EST5EDT5"]) {
84
+ await expect(
85
+ service.upsertByMemberId(aliceId, { timezone: tz }),
86
+ ).rejects.toBeInstanceOf(InvalidTimezoneError);
87
+ }
88
+ expect(await prisma.userNotificationPrefs.count()).toBe(0);
89
+ });
73
90
  });
@@ -0,0 +1,98 @@
1
+ /**
2
+ * admin/src/user-prefs.ts
3
+ * UserPrefsService (formerly UserPrefsService, ACM-4.1): per-user preferences
4
+ * read and written by memberId (AccountMember.id), which is the row's primary
5
+ * key (ACM-5.1 dropped the userEmail column).
6
+ */
7
+
8
+ import type {
9
+ PrismaClient,
10
+ UserNotificationPrefs,
11
+ } from "../prisma/client/client.ts";
12
+ import { PrincipalLookupService } from "./principal-lookup.ts";
13
+
14
+ export type { UserNotificationPrefs };
15
+
16
+ /** Thrown when a timezone is not a valid IANA zone name. */
17
+ export class InvalidTimezoneError extends Error {
18
+ constructor(public readonly timezone: string) {
19
+ super(`Invalid IANA timezone: ${timezone}`);
20
+ this.name = "InvalidTimezoneError";
21
+ }
22
+ }
23
+
24
+ export function isValidIanaTimezone(tz: string): boolean {
25
+ if (!tz || tz.trim() !== tz) return false;
26
+ try {
27
+ new Intl.DateTimeFormat("en-US", { timeZone: tz });
28
+ return true;
29
+ } catch {
30
+ return false;
31
+ }
32
+ }
33
+
34
+ export interface UserPrefsPatch {
35
+ autoFollowSessions?: boolean;
36
+ reminderHourLocal?: number;
37
+ autoFollowSince?: Date | null;
38
+ /** IANA zone name, or null to clear. */
39
+ timezone?: string | null;
40
+ }
41
+
42
+ export class UserPrefsService {
43
+ private readonly lookup: PrincipalLookupService;
44
+
45
+ constructor(private prisma: PrismaClient) {
46
+ this.lookup = new PrincipalLookupService(prisma);
47
+ }
48
+
49
+ async getByMemberId(memberId: string): Promise<UserNotificationPrefs | null> {
50
+ return this.prisma.userNotificationPrefs.findFirst({
51
+ where: { memberId },
52
+ });
53
+ }
54
+
55
+ async getTimezone(memberId: string): Promise<string | null> {
56
+ return (await this.getByMemberId(memberId))?.timezone ?? null;
57
+ }
58
+
59
+ /**
60
+ * Creates or updates the member's row. Returns null when `memberId` does not
61
+ * resolve to a user.
62
+ */
63
+ async upsertByMemberId(
64
+ memberId: string,
65
+ patch: UserPrefsPatch,
66
+ ): Promise<UserNotificationPrefs | null> {
67
+ if (patch.timezone != null && !isValidIanaTimezone(patch.timezone)) {
68
+ throw new InvalidTimezoneError(patch.timezone);
69
+ }
70
+ const { principals } = await this.lookup.resolveMany([memberId]);
71
+ if (!principals.some((p) => p.type === "user")) return null;
72
+ return this.prisma.userNotificationPrefs.upsert({
73
+ where: { memberId },
74
+ create: { memberId, ...patch },
75
+ update: patch,
76
+ });
77
+ }
78
+
79
+ /**
80
+ * Members with auto-follow on, each with their email resolved through the
81
+ * lookup. Members that no longer resolve are skipped.
82
+ */
83
+ async listAutoFollowers(): Promise<
84
+ Array<{ memberId: string; email: string; prefs: UserNotificationPrefs }>
85
+ > {
86
+ const rows = await this.prisma.userNotificationPrefs.findMany({
87
+ where: { autoFollowSessions: true },
88
+ });
89
+ const { principals } = await this.lookup.resolveMany(
90
+ rows.map((r) => r.memberId),
91
+ );
92
+ const emails = new Map(principals.map((p) => [p.id, p.email]));
93
+ return rows.flatMap((prefs) => {
94
+ const email = emails.get(prefs.memberId);
95
+ return email ? [{ memberId: prefs.memberId, email, prefs }] : [];
96
+ });
97
+ }
98
+ }