@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
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/admin-ui-preferences.ts
|
|
3
|
+
* GET/POST /admin/settings/preferences: the signed-in user's own display
|
|
4
|
+
* timezone. Visible to every role; keyed by the viewer's AccountMember id.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Context, Hono, MiddlewareHandler } from "hono";
|
|
8
|
+
import type { AccountMemberService } from "./account-members.ts";
|
|
9
|
+
import type { AdminUIEnv } from "./admin-ui.ts";
|
|
10
|
+
import {
|
|
11
|
+
renderPreferencesPage,
|
|
12
|
+
supportedTimezones,
|
|
13
|
+
} from "./admin-ui-preferences-pages.ts";
|
|
14
|
+
import {
|
|
15
|
+
SETTINGS_PREFERENCES_PATH as BASE,
|
|
16
|
+
type SettingsNav,
|
|
17
|
+
} from "./admin-ui-settings-layout.ts";
|
|
18
|
+
import { InvalidTimezoneError, type UserPrefsService } from "./user-prefs.ts";
|
|
19
|
+
|
|
20
|
+
export interface AdminPreferencesRouteDeps {
|
|
21
|
+
requireAuth: MiddlewareHandler<AdminUIEnv>;
|
|
22
|
+
userPrefs: Pick<UserPrefsService, "getTimezone" | "upsertByMemberId">;
|
|
23
|
+
members: Pick<AccountMemberService, "getByEmail">;
|
|
24
|
+
settingsNav?: SettingsNav;
|
|
25
|
+
/** Zone in effect for the request (set by the per-request resolver). */
|
|
26
|
+
effectiveTimezone: (c: Context<AdminUIEnv>) => string;
|
|
27
|
+
html: (content: string, opts?: { status?: number }) => Response;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const NOTICES: Record<string, string> = {
|
|
31
|
+
saved: "Timezone saved.",
|
|
32
|
+
cleared: "Timezone cleared; your browser timezone is used.",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function registerAdminPreferencesRoutes(
|
|
36
|
+
app: Hono<AdminUIEnv>,
|
|
37
|
+
deps: AdminPreferencesRouteDeps,
|
|
38
|
+
): void {
|
|
39
|
+
const member = (email: string) =>
|
|
40
|
+
deps.members.getByEmail(email).catch(() => null);
|
|
41
|
+
|
|
42
|
+
app.get(BASE, deps.requireAuth, async (c) => {
|
|
43
|
+
const me = await member(c.var.userEmail);
|
|
44
|
+
const saved = me ? await deps.userPrefs.getTimezone(me.id) : null;
|
|
45
|
+
const key = c.req.query("notice");
|
|
46
|
+
return deps.html(
|
|
47
|
+
renderPreferencesPage({
|
|
48
|
+
userEmail: c.var.userEmail,
|
|
49
|
+
isAdmin: c.var.isAdmin === true,
|
|
50
|
+
isOwner: me?.role === "owner",
|
|
51
|
+
...(deps.settingsNav ? { nav: deps.settingsNav } : {}),
|
|
52
|
+
savedTimezone: saved,
|
|
53
|
+
effectiveTimezone: deps.effectiveTimezone(c),
|
|
54
|
+
canSave: me !== null,
|
|
55
|
+
zones: supportedTimezones(),
|
|
56
|
+
...(key && NOTICES[key] ? { notice: NOTICES[key] } : {}),
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
async function save(
|
|
62
|
+
c: Context<AdminUIEnv>,
|
|
63
|
+
timezone: string | null,
|
|
64
|
+
notice: string,
|
|
65
|
+
): Promise<Response> {
|
|
66
|
+
const me = await member(c.var.userEmail);
|
|
67
|
+
if (!me) return new Response("Forbidden", { status: 403 });
|
|
68
|
+
try {
|
|
69
|
+
await deps.userPrefs.upsertByMemberId(me.id, { timezone });
|
|
70
|
+
} catch (err) {
|
|
71
|
+
if (!(err instanceof InvalidTimezoneError)) throw err;
|
|
72
|
+
return deps.html(
|
|
73
|
+
renderPreferencesPage({
|
|
74
|
+
userEmail: c.var.userEmail,
|
|
75
|
+
isAdmin: c.var.isAdmin === true,
|
|
76
|
+
isOwner: me.role === "owner",
|
|
77
|
+
...(deps.settingsNav ? { nav: deps.settingsNav } : {}),
|
|
78
|
+
savedTimezone: await deps.userPrefs.getTimezone(me.id),
|
|
79
|
+
effectiveTimezone: deps.effectiveTimezone(c),
|
|
80
|
+
canSave: true,
|
|
81
|
+
zones: supportedTimezones(),
|
|
82
|
+
error: "That is not a valid timezone.",
|
|
83
|
+
}),
|
|
84
|
+
{ status: 422 },
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return c.redirect(`${BASE}?notice=${notice}`, 302);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
app.post(`${BASE}/save`, deps.requireAuth, async (c) => {
|
|
91
|
+
let form: FormData;
|
|
92
|
+
try {
|
|
93
|
+
form = await c.req.formData();
|
|
94
|
+
} catch {
|
|
95
|
+
form = new FormData();
|
|
96
|
+
}
|
|
97
|
+
const tz = (form.get("timezone")?.toString() ?? "").trim();
|
|
98
|
+
// An empty selection means "use the browser timezone".
|
|
99
|
+
return tz === "" ? save(c, null, "cleared") : save(c, tz, "saved");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
app.post(`${BASE}/clear`, deps.requireAuth, (c) => save(c, null, "cleared"));
|
|
103
|
+
}
|
|
@@ -125,6 +125,7 @@ export const SETTINGS_GHE_PATH = "/admin/settings/github-enterprise";
|
|
|
125
125
|
export const SETTINGS_ACCOUNT_PATH = "/admin/account";
|
|
126
126
|
export const SETTINGS_ACCOUNTS_PATH = "/admin/accounts";
|
|
127
127
|
export const SETTINGS_TEMPLATES_PATH = "/admin/settings/templates";
|
|
128
|
+
export const SETTINGS_PREFERENCES_PATH = "/admin/settings/preferences";
|
|
128
129
|
export const SETTINGS_SLACK_CONFIG_TOKEN_PATH =
|
|
129
130
|
"/admin/settings/slack-config-token";
|
|
130
131
|
|
|
@@ -137,6 +138,8 @@ export interface BuildSettingsNavOptions {
|
|
|
137
138
|
templates?: boolean;
|
|
138
139
|
/** Include the Slack config token item (its routes are registered). */
|
|
139
140
|
slackConfigToken?: boolean;
|
|
141
|
+
/** Include the Preferences item (per-user timezone), visible to every role. */
|
|
142
|
+
preferences?: boolean;
|
|
140
143
|
/** Extension-contributed items (SGL-2.1). */
|
|
141
144
|
extraItems?: readonly SettingsNavContribution[];
|
|
142
145
|
}
|
|
@@ -183,9 +186,17 @@ export function buildSettingsNav(
|
|
|
183
186
|
if (opts.accounts) {
|
|
184
187
|
sections.push({
|
|
185
188
|
label: "Account",
|
|
186
|
-
items: [
|
|
189
|
+
items: [
|
|
190
|
+
{ label: "Account", path: SETTINGS_ACCOUNT_PATH, ownerOnly: true },
|
|
191
|
+
],
|
|
187
192
|
});
|
|
188
193
|
}
|
|
194
|
+
if (opts.preferences) {
|
|
195
|
+
const section = sections.find((s) => s.label === "Account");
|
|
196
|
+
const item = { label: "Preferences", path: SETTINGS_PREFERENCES_PATH };
|
|
197
|
+
if (section) section.items.push(item);
|
|
198
|
+
else sections.push({ label: "Account", items: [item] });
|
|
199
|
+
}
|
|
189
200
|
if (platform.length > 0)
|
|
190
201
|
sections.push({ label: "Platform", items: platform });
|
|
191
202
|
const base: SettingsNav = { sections };
|
package/src/admin-ui.ts
CHANGED
|
@@ -54,8 +54,8 @@ import { registerAdminGithubEnterpriseRoutes } from "./admin-ui-github-enterpris
|
|
|
54
54
|
import type { GheOrgs } from "./admin-ui-github-enterprise-pages.ts";
|
|
55
55
|
import {
|
|
56
56
|
type GithubUninstallPreviewView,
|
|
57
|
-
renderGithubAccessError,
|
|
58
57
|
MAX_INSTALL_REPOSITORIES,
|
|
58
|
+
renderGithubAccessError,
|
|
59
59
|
renderGithubOrgsFragment,
|
|
60
60
|
renderGithubUninstallConfirm,
|
|
61
61
|
} from "./admin-ui-github-identity-pages.ts";
|
|
@@ -98,6 +98,7 @@ import {
|
|
|
98
98
|
renderProvisionXappTokenPage,
|
|
99
99
|
settingsRedirectSection,
|
|
100
100
|
} from "./admin-ui-pages.ts";
|
|
101
|
+
import { registerAdminPreferencesRoutes } from "./admin-ui-preferences.ts";
|
|
101
102
|
import {
|
|
102
103
|
buildSettingsNav,
|
|
103
104
|
filterSettingsNavForRole,
|
|
@@ -208,12 +209,20 @@ import {
|
|
|
208
209
|
type SpecialtyTypeResolver,
|
|
209
210
|
SpecialtyTypes,
|
|
210
211
|
} from "./specialty-types.ts";
|
|
212
|
+
import {
|
|
213
|
+
createTimezoneResolver,
|
|
214
|
+
DEFAULT_TIMEZONE,
|
|
215
|
+
TIMEZONE_COOKIE,
|
|
216
|
+
} from "./timezone-resolver.ts";
|
|
217
|
+
import type { UserPrefsService } from "./user-prefs.ts";
|
|
211
218
|
|
|
212
219
|
export type AdminUIEnv = {
|
|
213
220
|
Variables: {
|
|
214
221
|
userEmail: string;
|
|
215
222
|
isAdmin: boolean;
|
|
216
223
|
settingsNav?: SettingsNav;
|
|
224
|
+
/** Display timezone resolved for this request (user, cookie, default). */
|
|
225
|
+
timezone?: string;
|
|
217
226
|
};
|
|
218
227
|
};
|
|
219
228
|
|
|
@@ -590,6 +599,13 @@ export interface AdminUIDeps {
|
|
|
590
599
|
* Defaults to "America/Los_Angeles" when absent.
|
|
591
600
|
*/
|
|
592
601
|
timezone?: string;
|
|
602
|
+
/**
|
|
603
|
+
* Per-user preferences store. When present (with `accountServices`), the
|
|
604
|
+
* display timezone is resolved per request from the user's saved setting,
|
|
605
|
+
* then the browser `tz` cookie, then `timezone`, and Settings > Preferences
|
|
606
|
+
* is registered. Absent: the fixed `timezone` is used for every request.
|
|
607
|
+
*/
|
|
608
|
+
userPrefs?: Pick<UserPrefsService, "getTimezone" | "upsertByMemberId">;
|
|
593
609
|
/**
|
|
594
610
|
* Extra `?error=` codes (code → message) an extension's routes redirect to
|
|
595
611
|
* the agent detail page with — merged over the platform's own codes.
|
|
@@ -1027,7 +1043,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
1027
1043
|
githubAppClient,
|
|
1028
1044
|
appBaseUrl,
|
|
1029
1045
|
devAuthEnabled = false,
|
|
1030
|
-
timezone
|
|
1046
|
+
timezone: deploymentTimezone,
|
|
1031
1047
|
chatClient,
|
|
1032
1048
|
pwaAssetsDir = PWA_ASSETS_DIR,
|
|
1033
1049
|
appVersion = "0.0.0",
|
|
@@ -1041,6 +1057,10 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
1041
1057
|
// toggle-render gate and the route-503 gate can't drift.
|
|
1042
1058
|
const pushEnabled = Boolean(pushService && vapidPublicKey);
|
|
1043
1059
|
|
|
1060
|
+
// Fixed display timezone for requests the per-request resolver does not
|
|
1061
|
+
// cover (and the deployment default it falls back to).
|
|
1062
|
+
const timezone = deploymentTimezone ?? DEFAULT_TIMEZONE;
|
|
1063
|
+
|
|
1044
1064
|
const app = new Hono<AdminUIEnv>();
|
|
1045
1065
|
|
|
1046
1066
|
const requireAuth = createUIAuthMiddleware(sessionSecret, {
|
|
@@ -1050,12 +1070,19 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
1050
1070
|
});
|
|
1051
1071
|
|
|
1052
1072
|
const accountNavEnabled = accountServices !== undefined;
|
|
1073
|
+
const preferencesEnabled = Boolean(deps.userPrefs && accountServices);
|
|
1074
|
+
const resolveRequestTimezone = createTimezoneResolver({
|
|
1075
|
+
...(deps.userPrefs ? { userPrefs: deps.userPrefs } : {}),
|
|
1076
|
+
...(accountServices ? { members: accountServices.members } : {}),
|
|
1077
|
+
deployment: timezone,
|
|
1078
|
+
});
|
|
1053
1079
|
|
|
1054
1080
|
const settingsNav = buildSettingsNav({
|
|
1055
1081
|
githubEnterprise: Boolean(githubEnterpriseApi),
|
|
1056
1082
|
templates: Boolean(templateService),
|
|
1057
1083
|
slackConfigToken: Boolean(slackConfigToken),
|
|
1058
1084
|
accounts: accountNavEnabled,
|
|
1085
|
+
preferences: preferencesEnabled,
|
|
1059
1086
|
extraItems: deps.settingsNavItems ?? [],
|
|
1060
1087
|
});
|
|
1061
1088
|
|
|
@@ -1092,12 +1119,32 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
1092
1119
|
await next();
|
|
1093
1120
|
});
|
|
1094
1121
|
|
|
1122
|
+
// Per-request display timezone: the user's saved setting, then the tz
|
|
1123
|
+
// cookie, then the deployment default. Only when preferences are wired;
|
|
1124
|
+
// otherwise handlers fall back to the fixed timezone.
|
|
1125
|
+
if (preferencesEnabled) {
|
|
1126
|
+
app.use("/admin/*", async (c, next) => {
|
|
1127
|
+
const token = getCookie(c, SESSION_COOKIE);
|
|
1128
|
+
const user = token ? await getSessionUser(token, sessionSecret) : null;
|
|
1129
|
+
c.set(
|
|
1130
|
+
"timezone",
|
|
1131
|
+
await resolveRequestTimezone({
|
|
1132
|
+
email: user?.email,
|
|
1133
|
+
cookie: getCookie(c, TIMEZONE_COOKIE),
|
|
1134
|
+
}),
|
|
1135
|
+
);
|
|
1136
|
+
await next();
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
const tzOf = (c: Context<AdminUIEnv>): string => c.var.timezone ?? timezone;
|
|
1140
|
+
|
|
1095
1141
|
// GHE-1.4: per-request toolbar audience; TOOLBAR_NAV_ITEMS decides visibility.
|
|
1096
1142
|
if (
|
|
1097
1143
|
githubEnterpriseApi ||
|
|
1098
1144
|
templateService ||
|
|
1099
1145
|
slackConfigToken ||
|
|
1100
|
-
accountNavEnabled
|
|
1146
|
+
accountNavEnabled ||
|
|
1147
|
+
preferencesEnabled
|
|
1101
1148
|
) {
|
|
1102
1149
|
app.use("/admin/*", async (c, next) => {
|
|
1103
1150
|
const token = getCookie(c, SESSION_COOKIE);
|
|
@@ -1652,7 +1699,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
1652
1699
|
);
|
|
1653
1700
|
}
|
|
1654
1701
|
return html(
|
|
1655
|
-
renderAgentsPage(agents, c.var.userEmail, c.var.isAdmin,
|
|
1702
|
+
renderAgentsPage(agents, c.var.userEmail, c.var.isAdmin, tzOf(c), {
|
|
1656
1703
|
accountNames,
|
|
1657
1704
|
queueActivityLinks: deps.queueActivityLinks,
|
|
1658
1705
|
successMsg,
|
|
@@ -2510,7 +2557,12 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
2510
2557
|
}
|
|
2511
2558
|
}
|
|
2512
2559
|
}
|
|
2513
|
-
return html(
|
|
2560
|
+
return html(
|
|
2561
|
+
renderInstallationReposBody(view, {
|
|
2562
|
+
appIdMismatch,
|
|
2563
|
+
timezone: tzOf(c),
|
|
2564
|
+
}),
|
|
2565
|
+
);
|
|
2514
2566
|
});
|
|
2515
2567
|
|
|
2516
2568
|
// IRM-3.2: the single Sync confirm (over the IRM-3.1 preview) and its apply.
|
|
@@ -2528,7 +2580,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
2528
2580
|
renderSyncConfirm(
|
|
2529
2581
|
c.req.param("id") ?? "",
|
|
2530
2582
|
(await res.json()) as GithubSyncPreviewView,
|
|
2531
|
-
|
|
2583
|
+
tzOf(c),
|
|
2532
2584
|
),
|
|
2533
2585
|
);
|
|
2534
2586
|
});
|
|
@@ -2598,7 +2650,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
2598
2650
|
warnings,
|
|
2599
2651
|
)
|
|
2600
2652
|
: [],
|
|
2601
|
-
|
|
2653
|
+
tzOf(c),
|
|
2602
2654
|
),
|
|
2603
2655
|
);
|
|
2604
2656
|
},
|
|
@@ -2785,7 +2837,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
2785
2837
|
userEmail: c.var.userEmail,
|
|
2786
2838
|
isAdmin: c.var.isAdmin,
|
|
2787
2839
|
accountId: agent.accountId ?? null,
|
|
2788
|
-
timezone,
|
|
2840
|
+
timezone: tzOf(c),
|
|
2789
2841
|
},
|
|
2790
2842
|
);
|
|
2791
2843
|
|
|
@@ -2847,7 +2899,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
2847
2899
|
newToken,
|
|
2848
2900
|
successMsg,
|
|
2849
2901
|
warning,
|
|
2850
|
-
timezone,
|
|
2902
|
+
timezone: tzOf(c),
|
|
2851
2903
|
extensionCardsHtml: cards.header,
|
|
2852
2904
|
configurationCardsHtml: cards.configuration,
|
|
2853
2905
|
queueActivityLinks: deps.queueActivityLinks,
|
|
@@ -3859,7 +3911,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
3859
3911
|
userEmail: c.var.userEmail,
|
|
3860
3912
|
isAdmin: c.var.isAdmin,
|
|
3861
3913
|
accountId: agent.accountId ?? null,
|
|
3862
|
-
timezone,
|
|
3914
|
+
timezone: tzOf(c),
|
|
3863
3915
|
},
|
|
3864
3916
|
["configuration"],
|
|
3865
3917
|
);
|
|
@@ -3878,7 +3930,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
3878
3930
|
section: "access",
|
|
3879
3931
|
canManage,
|
|
3880
3932
|
newToken: rawToken,
|
|
3881
|
-
timezone,
|
|
3933
|
+
timezone: tzOf(c),
|
|
3882
3934
|
extensionCardsHtml: cards.header,
|
|
3883
3935
|
configurationCardsHtml: cards.configuration,
|
|
3884
3936
|
queueActivityLinks: deps.queueActivityLinks,
|
|
@@ -4696,7 +4748,8 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
4696
4748
|
githubEnterpriseApi ||
|
|
4697
4749
|
templateService ||
|
|
4698
4750
|
slackConfigToken ||
|
|
4699
|
-
accountNavEnabled
|
|
4751
|
+
accountNavEnabled ||
|
|
4752
|
+
preferencesEnabled
|
|
4700
4753
|
) {
|
|
4701
4754
|
app.get("/admin/settings", requireAuth, async (c) => {
|
|
4702
4755
|
// Admins without an account would 404 on the Account item; skip it.
|
|
@@ -4771,6 +4824,19 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
4771
4824
|
});
|
|
4772
4825
|
}
|
|
4773
4826
|
|
|
4827
|
+
// ─── Preferences (per-user display timezone) ─────────────────────────────
|
|
4828
|
+
|
|
4829
|
+
if (deps.userPrefs && accountServices) {
|
|
4830
|
+
registerAdminPreferencesRoutes(app, {
|
|
4831
|
+
requireAuth,
|
|
4832
|
+
userPrefs: deps.userPrefs,
|
|
4833
|
+
members: accountServices.members,
|
|
4834
|
+
settingsNav,
|
|
4835
|
+
effectiveTimezone: (c) => c.var.timezone ?? timezone,
|
|
4836
|
+
html,
|
|
4837
|
+
});
|
|
4838
|
+
}
|
|
4839
|
+
|
|
4774
4840
|
// ─── Account page (SSP-3.2) ───────────────────────────────────────────────
|
|
4775
4841
|
|
|
4776
4842
|
if (accountServices) {
|
|
@@ -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 ('
|
|
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
|
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/timezone-resolver.ts
|
|
3
|
+
* Per-request display timezone: the signed-in user's saved setting, then the
|
|
4
|
+
* browser-set `tz` cookie, then the deployment default. An invalid value at
|
|
5
|
+
* any level falls through to the next; nothing here throws.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { AccountMemberService } from "./account-members.ts";
|
|
9
|
+
import { isValidIanaTimezone, type UserPrefsService } from "./user-prefs.ts";
|
|
10
|
+
|
|
11
|
+
/** Used when neither the user, the cookie nor the deployment supplies a zone. */
|
|
12
|
+
export const DEFAULT_TIMEZONE = "America/Los_Angeles";
|
|
13
|
+
|
|
14
|
+
export const TIMEZONE_COOKIE = "tz";
|
|
15
|
+
|
|
16
|
+
export interface TimezoneSources {
|
|
17
|
+
/** The user's saved setting. */
|
|
18
|
+
user?: string | null;
|
|
19
|
+
/** The browser-reported zone from the tz cookie. */
|
|
20
|
+
cookie?: string | null;
|
|
21
|
+
/** The deployment default (SHIPWRIGHT_ADMIN_TZ). */
|
|
22
|
+
deployment?: string | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const usable = (tz: string | null | undefined): tz is string =>
|
|
26
|
+
typeof tz === "string" && isValidIanaTimezone(tz);
|
|
27
|
+
|
|
28
|
+
/** First valid of user, cookie, deployment; else the built-in default. */
|
|
29
|
+
export function resolveTimezone(src: TimezoneSources): string {
|
|
30
|
+
for (const tz of [src.user, src.cookie, src.deployment]) {
|
|
31
|
+
if (usable(tz)) return tz;
|
|
32
|
+
}
|
|
33
|
+
return DEFAULT_TIMEZONE;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The decoded tz cookie from a Cookie header, if present and well-formed. */
|
|
37
|
+
export function parseTzCookie(header: string | null | undefined) {
|
|
38
|
+
if (!header) return undefined;
|
|
39
|
+
for (const part of header.split(";")) {
|
|
40
|
+
const eq = part.indexOf("=");
|
|
41
|
+
if (eq < 0 || part.slice(0, eq).trim() !== TIMEZONE_COOKIE) continue;
|
|
42
|
+
try {
|
|
43
|
+
return decodeURIComponent(part.slice(eq + 1).trim());
|
|
44
|
+
} catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface TimezoneResolverDeps {
|
|
52
|
+
userPrefs?: Pick<UserPrefsService, "getTimezone">;
|
|
53
|
+
members?: Pick<AccountMemberService, "getByEmail">;
|
|
54
|
+
/** Deployment default; invalid or absent falls to DEFAULT_TIMEZONE. */
|
|
55
|
+
deployment?: string | null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type TimezoneResolver = (input: {
|
|
59
|
+
email?: string | undefined;
|
|
60
|
+
cookie?: string | undefined;
|
|
61
|
+
}) => Promise<string>;
|
|
62
|
+
|
|
63
|
+
/** Async resolver that looks up the user's saved setting by signed-in email. */
|
|
64
|
+
export function createTimezoneResolver(
|
|
65
|
+
deps: TimezoneResolverDeps,
|
|
66
|
+
): TimezoneResolver {
|
|
67
|
+
return async ({ email, cookie }) => {
|
|
68
|
+
let user: string | null = null;
|
|
69
|
+
if (email && deps.userPrefs && deps.members) {
|
|
70
|
+
try {
|
|
71
|
+
const member = await deps.members.getByEmail(email);
|
|
72
|
+
if (member) user = await deps.userPrefs.getTimezone(member.id);
|
|
73
|
+
} catch {
|
|
74
|
+
// a failed lookup falls through to the cookie
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return resolveTimezone({ user, cookie, deployment: deps.deployment });
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Small inline script: records the browser's IANA zone in the tz cookie (one
|
|
83
|
+
* year, site-wide). The resolver ignores the cookie when the user has a saved
|
|
84
|
+
* setting, so it is safe to set unconditionally.
|
|
85
|
+
*/
|
|
86
|
+
export function tzCookieScript(): string {
|
|
87
|
+
return `<script>(function(){try{var z=Intl.DateTimeFormat().resolvedOptions().timeZone;if(!z)return;var e=encodeURIComponent(z);if(document.cookie.split("; ").indexOf("tz="+e)>=0)return;document.cookie="tz="+e+"; path=/; max-age=31536000; SameSite=Lax"}catch(_){}})();</script>`;
|
|
88
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/timezone-resolver.unit.test.ts
|
|
3
|
+
* Per-request display timezone: user setting, then tz cookie, then deployment
|
|
4
|
+
* default; an invalid value at any level falls through.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import {
|
|
9
|
+
createTimezoneResolver,
|
|
10
|
+
DEFAULT_TIMEZONE,
|
|
11
|
+
parseTzCookie,
|
|
12
|
+
resolveTimezone,
|
|
13
|
+
tzCookieScript,
|
|
14
|
+
} from "./timezone-resolver.ts";
|
|
15
|
+
|
|
16
|
+
describe("resolveTimezone", () => {
|
|
17
|
+
it("prefers the user setting", () => {
|
|
18
|
+
expect(
|
|
19
|
+
resolveTimezone({
|
|
20
|
+
user: "Europe/Paris",
|
|
21
|
+
cookie: "Asia/Tokyo",
|
|
22
|
+
deployment: "UTC",
|
|
23
|
+
}),
|
|
24
|
+
).toBe("Europe/Paris");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("uses the cookie when there is no user setting", () => {
|
|
28
|
+
expect(
|
|
29
|
+
resolveTimezone({ user: null, cookie: "Asia/Tokyo", deployment: "UTC" }),
|
|
30
|
+
).toBe("Asia/Tokyo");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("uses the deployment default when neither is set", () => {
|
|
34
|
+
expect(resolveTimezone({ deployment: "UTC" })).toBe("UTC");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("falls back to the built-in default last", () => {
|
|
38
|
+
expect(resolveTimezone({})).toBe(DEFAULT_TIMEZONE);
|
|
39
|
+
expect(DEFAULT_TIMEZONE).toBe("America/Los_Angeles");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("falls through an invalid user setting to the cookie", () => {
|
|
43
|
+
expect(resolveTimezone({ user: "Not/AZone", cookie: "Asia/Tokyo" })).toBe(
|
|
44
|
+
"Asia/Tokyo",
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("falls through an invalid cookie to the deployment default", () => {
|
|
49
|
+
expect(resolveTimezone({ cookie: "bogus", deployment: "UTC" })).toBe("UTC");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("falls through an invalid deployment default without throwing", () => {
|
|
53
|
+
expect(resolveTimezone({ user: "x", cookie: "y", deployment: "z" })).toBe(
|
|
54
|
+
DEFAULT_TIMEZONE,
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("parseTzCookie", () => {
|
|
60
|
+
it("reads and decodes the tz cookie", () => {
|
|
61
|
+
expect(parseTzCookie("a=1; tz=America%2FChicago; b=2")).toBe(
|
|
62
|
+
"America/Chicago",
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
it("returns undefined when absent or malformed", () => {
|
|
66
|
+
expect(parseTzCookie(undefined)).toBeUndefined();
|
|
67
|
+
expect(parseTzCookie("a=1")).toBeUndefined();
|
|
68
|
+
expect(parseTzCookie("tz=%E0%A4%A")).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("createTimezoneResolver", () => {
|
|
73
|
+
const members = {
|
|
74
|
+
getByEmail: async (e: string) =>
|
|
75
|
+
e === "m@acme.com" ? ({ id: "mem-1" } as never) : null,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
it("resolves the stored setting by the member's id", async () => {
|
|
79
|
+
const resolve = createTimezoneResolver({
|
|
80
|
+
userPrefs: { getTimezone: async (id) => (id === "mem-1" ? "UTC" : null) },
|
|
81
|
+
members,
|
|
82
|
+
deployment: "Asia/Tokyo",
|
|
83
|
+
});
|
|
84
|
+
expect(await resolve({ email: "m@acme.com", cookie: "Europe/Paris" })).toBe(
|
|
85
|
+
"UTC",
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("uses the cookie for a member with nothing saved", async () => {
|
|
90
|
+
const resolve = createTimezoneResolver({
|
|
91
|
+
userPrefs: { getTimezone: async () => null },
|
|
92
|
+
members,
|
|
93
|
+
});
|
|
94
|
+
expect(await resolve({ email: "m@acme.com", cookie: "Europe/Paris" })).toBe(
|
|
95
|
+
"Europe/Paris",
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("falls through when the lookup throws or the email is unknown", async () => {
|
|
100
|
+
const boom = createTimezoneResolver({
|
|
101
|
+
userPrefs: {
|
|
102
|
+
getTimezone: async () => {
|
|
103
|
+
throw new Error("db down");
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
members,
|
|
107
|
+
deployment: "UTC",
|
|
108
|
+
});
|
|
109
|
+
expect(await boom({ email: "m@acme.com" })).toBe("UTC");
|
|
110
|
+
expect(await boom({ email: "nobody@acme.com" })).toBe("UTC");
|
|
111
|
+
expect(await boom({})).toBe("UTC");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("works with no stores at all", async () => {
|
|
115
|
+
const resolve = createTimezoneResolver({});
|
|
116
|
+
expect(await resolve({ cookie: "UTC" })).toBe("UTC");
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("tzCookieScript", () => {
|
|
121
|
+
it("is a script tag that sets the tz cookie from Intl", () => {
|
|
122
|
+
const s = tzCookieScript();
|
|
123
|
+
expect(s.startsWith("<script>")).toBe(true);
|
|
124
|
+
expect(s).toContain("resolvedOptions().timeZone");
|
|
125
|
+
expect(s).toContain("tz=");
|
|
126
|
+
});
|
|
127
|
+
});
|