@app-vitals/agent-admin 1.111.0 → 1.113.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.
- package/e2e/real/member-agents.real.e2e.ts +8 -4
- package/package.json +2 -2
- package/prisma/client/internal/class.ts +4 -4
- package/prisma/client/internal/prismaNamespace.ts +1 -0
- package/prisma/client/internal/prismaNamespaceBrowser.ts +1 -0
- package/prisma/client/models/UserNotificationPrefs.ts +29 -1
- package/prisma/migrations/20261201000000_rename_user_notification_prefs_to_user_prefs/migration.sql +4 -0
- package/prisma/schema/schema.prisma +3 -0
- package/src/admin-boot.ts +15 -6
- package/src/admin-extension.ts +10 -1
- package/src/admin-ui-layout.ts +2 -0
- package/src/admin-ui-preferences-pages.ts +79 -0
- package/src/admin-ui-preferences.smoke.test.ts +168 -0
- package/src/admin-ui-preferences.ts +103 -0
- package/src/admin-ui-settings-layout.ts +12 -1
- package/src/admin-ui.ts +78 -12
- package/src/drop-email-key-columns-migration.integration.test.ts +0 -4
- package/src/principals-api.integration.test.ts +3 -3
- package/src/timezone-resolver.ts +88 -0
- package/src/timezone-resolver.unit.test.ts +127 -0
- package/src/user-notification-prefs.ts +10 -65
- package/src/user-notification-prefs.unit.test.ts +9 -0
- package/src/user-prefs-rename-migration.integration.test.ts +71 -0
- package/src/{user-notification-prefs.integration.test.ts → user-prefs.integration.test.ts} +22 -5
- package/src/user-prefs.ts +98 -0
|
@@ -1,71 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/user-notification-prefs.ts
|
|
3
|
-
*
|
|
4
|
-
*
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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-
|
|
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 {
|
|
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("
|
|
18
|
+
describeOrSkip("UserPrefsService by memberId (integration)", () => {
|
|
19
19
|
let prisma: PrismaClient;
|
|
20
|
-
let service:
|
|
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
|
|
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
|
+
}
|