@app-vitals/agent-admin 1.110.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.
- package/openapi.json +0 -193
- package/openapi.platform.json +1 -238
- 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 +6 -5
- package/src/admin-ui-github-identity-pages.ts +35 -139
- package/src/admin-ui-github-identity-pages.unit.test.ts +50 -173
- package/src/admin-ui-github-identity.smoke.test.ts +103 -63
- package/src/admin-ui-installation-repos-pages.ts +2 -23
- package/src/admin-ui-installation-repos-pages.unit.test.ts +41 -36
- package/src/admin-ui-installation-repos.smoke.test.ts +9 -8
- package/src/admin-ui-pages.ts +4 -5
- package/src/admin-ui.ts +51 -36
- package/src/agents-api.smoke.test.ts +26 -315
- package/src/agents-api.ts +1 -107
- package/src/drop-email-key-columns-migration.integration.test.ts +0 -4
- package/src/github-access-preview.ts +2 -2
- package/src/github-enterprise-api.smoke.test.ts +26 -0
- package/src/github-enterprise-api.ts +18 -2
- package/src/github-enterprise-client.integration.test.ts +0 -41
- package/src/github-enterprise-client.ts +0 -25
- package/src/github-enterprise-client.unit.test.ts +0 -18
- package/src/github-installation-wiring.ts +18 -12
- package/src/github-sync-all-repos.unit.test.ts +116 -0
- package/src/github-sync-snapshot-refresh.integration.test.ts +168 -0
- package/src/github-sync-snapshot-refresh.unit.test.ts +144 -0
- package/src/github-sync.ts +103 -137
- package/src/github-sync.unit.test.ts +82 -1
- package/src/installation-ports.ts +1 -40
- package/src/principals-api.integration.test.ts +3 -3
- package/src/reconcile-open-work.integration.test.ts +0 -1
- 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
- package/src/github-access-preview.integration.test.ts +0 -399
- package/src/github-sync-switch.integration.test.ts +0 -297
- package/src/github-sync-switch.unit.test.ts +0 -467
|
@@ -279,7 +279,9 @@ describe("planGithubSync (archived repos)", () => {
|
|
|
279
279
|
}),
|
|
280
280
|
);
|
|
281
281
|
expect(p.skippedOrgs).toEqual([]);
|
|
282
|
-
expect(p.notApplied).toEqual([
|
|
282
|
+
expect(p.notApplied).toEqual([
|
|
283
|
+
{ repo: "other/old", reason: ARCHIVED_REASON },
|
|
284
|
+
]);
|
|
283
285
|
});
|
|
284
286
|
|
|
285
287
|
test("an installed archived repo in an unchecked org is kept, not removed", () => {
|
|
@@ -863,3 +865,82 @@ describe("skippedOrgs detail", () => {
|
|
|
863
865
|
});
|
|
864
866
|
});
|
|
865
867
|
});
|
|
868
|
+
|
|
869
|
+
describe("GithubSyncService.installRepoChoices", () => {
|
|
870
|
+
const svc = (username: string | undefined, calls: string[]) =>
|
|
871
|
+
new GithubSyncService({
|
|
872
|
+
agents: {
|
|
873
|
+
get: async () => ({
|
|
874
|
+
id: "ag",
|
|
875
|
+
githubUsername: username ?? null,
|
|
876
|
+
appId: 7,
|
|
877
|
+
repos: [],
|
|
878
|
+
}),
|
|
879
|
+
setRepos: async () => {},
|
|
880
|
+
},
|
|
881
|
+
access: async () => {
|
|
882
|
+
throw new Error("live resolver must not be used");
|
|
883
|
+
},
|
|
884
|
+
installation: {} as never,
|
|
885
|
+
audit: {} as never,
|
|
886
|
+
cache: {
|
|
887
|
+
listAgentSnapshots: async () => [],
|
|
888
|
+
getAccessRows: async (q: { repos: string[] }) => {
|
|
889
|
+
calls.push(q.repos.join(","));
|
|
890
|
+
return q.repos.map((r) => ({
|
|
891
|
+
repo: r,
|
|
892
|
+
status: "ok",
|
|
893
|
+
canPush: r !== "ro",
|
|
894
|
+
}));
|
|
895
|
+
},
|
|
896
|
+
} as never,
|
|
897
|
+
lookupRepos: async () => [
|
|
898
|
+
{
|
|
899
|
+
org: "acme",
|
|
900
|
+
completedAt: new Date(),
|
|
901
|
+
repos: ["acme/web", "acme/ro", "acme/api"],
|
|
902
|
+
},
|
|
903
|
+
{ org: "beta", completedAt: null, repos: ["beta/x"] },
|
|
904
|
+
],
|
|
905
|
+
});
|
|
906
|
+
|
|
907
|
+
it("returns sorted pushable repo names; skips incomplete orgs", async () => {
|
|
908
|
+
const calls: string[] = [];
|
|
909
|
+
expect(
|
|
910
|
+
await svc("octo", calls).installRepoChoices("ag", ["acme", "beta"]),
|
|
911
|
+
).toEqual({
|
|
912
|
+
acme: ["api", "web"],
|
|
913
|
+
});
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
it("returns nothing without a bound user", async () => {
|
|
917
|
+
expect(await svc(undefined, []).installRepoChoices("ag", ["acme"])).toEqual(
|
|
918
|
+
{},
|
|
919
|
+
);
|
|
920
|
+
});
|
|
921
|
+
});
|
|
922
|
+
|
|
923
|
+
describe("GithubSyncService.preview with an incomplete apps state", () => {
|
|
924
|
+
it("skips with the apps-state reason and plans nothing", async () => {
|
|
925
|
+
const svc = new GithubSyncService({
|
|
926
|
+
agents: {
|
|
927
|
+
get: async () => ({
|
|
928
|
+
id: "ag",
|
|
929
|
+
accountId: "acct",
|
|
930
|
+
githubUsername: "octo",
|
|
931
|
+
appId: 7,
|
|
932
|
+
repos: [],
|
|
933
|
+
}),
|
|
934
|
+
setRepos: async () => {},
|
|
935
|
+
},
|
|
936
|
+
access: async () => {
|
|
937
|
+
throw new Error("live resolver must not be used");
|
|
938
|
+
},
|
|
939
|
+
installation: {} as never,
|
|
940
|
+
audit: {} as never,
|
|
941
|
+
appsState: async () => "lookup_only",
|
|
942
|
+
} as never);
|
|
943
|
+
const r = await svc.preview("ag");
|
|
944
|
+
expect(r).toMatchObject({ status: "skipped", appsState: "lookup_only" });
|
|
945
|
+
});
|
|
946
|
+
});
|
|
@@ -83,17 +83,6 @@ export interface AppInstallationPort {
|
|
|
83
83
|
repos: readonly string[],
|
|
84
84
|
identity?: AppIdentity,
|
|
85
85
|
): Promise<void>;
|
|
86
|
-
/**
|
|
87
|
-
* Switch the installation from "all" to "selected" with exactly `repos`
|
|
88
|
-
* (owner/repo, all in `org`, at most 50). Optional so existing
|
|
89
|
-
* implementations stay valid.
|
|
90
|
-
*/
|
|
91
|
-
setRepositorySelection?(
|
|
92
|
-
appId: number,
|
|
93
|
-
org: string,
|
|
94
|
-
repos: readonly string[],
|
|
95
|
-
identity?: AppIdentity,
|
|
96
|
-
): Promise<void>;
|
|
97
86
|
}
|
|
98
87
|
|
|
99
88
|
/** Repos changed by one audited change, on the installation and in the agent's config. */
|
|
@@ -102,7 +91,7 @@ export interface InstallationAuditDiff {
|
|
|
102
91
|
installationRemoves: string[];
|
|
103
92
|
configAdds: string[];
|
|
104
93
|
configRemoves: string[];
|
|
105
|
-
/**
|
|
94
|
+
/** Legacy: old audit rows only; Sync no longer switches installations. Keep optional so they still read. */
|
|
106
95
|
selectionSwitches?: { org: string; repos: string[] }[];
|
|
107
96
|
}
|
|
108
97
|
|
|
@@ -227,13 +216,6 @@ export interface InstallationRepoClient {
|
|
|
227
216
|
names: readonly string[],
|
|
228
217
|
accountId?: string | null,
|
|
229
218
|
): Promise<unknown>;
|
|
230
|
-
/** Optional: needed only to switch an "all" installation to "selected". */
|
|
231
|
-
setRepositorySelection?(
|
|
232
|
-
org: string,
|
|
233
|
-
installationId: number,
|
|
234
|
-
input: { repositorySelection: "selected"; repositories: readonly string[] },
|
|
235
|
-
accountId?: string | null,
|
|
236
|
-
): Promise<unknown>;
|
|
237
219
|
}
|
|
238
220
|
|
|
239
221
|
/** The agent App has no installation on the org (distinct from a GitHub failure). */
|
|
@@ -294,7 +276,6 @@ export function createInstallationPort(
|
|
|
294
276
|
}
|
|
295
277
|
return found.id;
|
|
296
278
|
};
|
|
297
|
-
const setSelection = client.setRepositorySelection?.bind(client);
|
|
298
279
|
return {
|
|
299
280
|
async listRepos(appId, orgs, identity) {
|
|
300
281
|
const out: Record<string, string[]> = {};
|
|
@@ -324,25 +305,5 @@ export function createInstallationPort(
|
|
|
324
305
|
identity?.accountId,
|
|
325
306
|
);
|
|
326
307
|
},
|
|
327
|
-
...(setSelection
|
|
328
|
-
? {
|
|
329
|
-
async setRepositorySelection(
|
|
330
|
-
appId: number,
|
|
331
|
-
org: string,
|
|
332
|
-
repos: readonly string[],
|
|
333
|
-
identity?: AppIdentity,
|
|
334
|
-
) {
|
|
335
|
-
await setSelection(
|
|
336
|
-
org,
|
|
337
|
-
await installationId(appId, org, identity),
|
|
338
|
-
{
|
|
339
|
-
repositorySelection: "selected",
|
|
340
|
-
repositories: repos.map(baseName),
|
|
341
|
-
},
|
|
342
|
-
identity?.accountId,
|
|
343
|
-
);
|
|
344
|
-
},
|
|
345
|
-
}
|
|
346
|
-
: {}),
|
|
347
308
|
};
|
|
348
309
|
}
|
|
@@ -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 ('
|
|
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
|
-
"
|
|
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 ('
|
|
113
|
+
AND tc.table_name IN ('UserPrefs','ChatThreadWatch','PushSubscription','AgentMember')`;
|
|
114
114
|
expect(Number(fks[0]?.n)).toBe(0);
|
|
115
115
|
});
|
|
116
116
|
});
|
|
@@ -137,7 +137,6 @@ describeOrSkip("reconcile-open-work (integration)", () => {
|
|
|
137
137
|
calls.push("removeRepos");
|
|
138
138
|
if (removeFails) throw new Error("installation PATCH failed");
|
|
139
139
|
},
|
|
140
|
-
setRepositorySelection: async () => {},
|
|
141
140
|
},
|
|
142
141
|
audit: new PrismaInstallationAudit(prisma),
|
|
143
142
|
...(openWork ? { openWork } : {}),
|
|
@@ -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
|
+
}
|