@app-vitals/agent-admin 1.112.0 → 1.113.1
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/browser.ts +2 -2
- package/prisma/client/client.ts +2 -2
- package/prisma/client/internal/class.ts +7 -7
- package/prisma/client/internal/prismaNamespace.ts +39 -39
- package/prisma/client/internal/prismaNamespaceBrowser.ts +3 -3
- package/prisma/client/models/UserPrefs.ts +1249 -0
- package/prisma/client/models.ts +1 -1
- package/prisma/schema/schema.prisma +2 -2
- package/src/account-member-removal.integration.test.ts +5 -5
- package/src/account-members.ts +2 -2
- package/src/account-move.integration.test.ts +4 -4
- package/src/account-move.ts +1 -1
- package/src/admin-boot.ts +15 -6
- package/src/admin-extension.ts +10 -1
- package/src/admin-ui-agent-members.smoke.test.ts +1 -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/ensure-account-member.integration.test.ts +1 -1
- package/src/member-rows.ts +2 -2
- package/src/member-rows.unit.test.ts +2 -2
- package/src/test-support/member-id-test-support.ts +1 -1
- package/src/timezone-resolver.ts +88 -0
- package/src/timezone-resolver.unit.test.ts +127 -0
- package/src/user-prefs.integration.test.ts +5 -5
- package/src/user-prefs.ts +9 -12
- package/prisma/client/models/UserNotificationPrefs.ts +0 -1249
- package/src/user-notification-prefs.ts +0 -16
- package/src/user-notification-prefs.unit.test.ts +0 -9
package/prisma/client/models.ts
CHANGED
|
@@ -35,7 +35,7 @@ export type * from './models/AgentCronRunRequest.ts'
|
|
|
35
35
|
export type * from './models/AgentGitHubInstallationsSnapshot.ts'
|
|
36
36
|
export type * from './models/PushSubscription.ts'
|
|
37
37
|
export type * from './models/ChatThreadWatch.ts'
|
|
38
|
-
export type * from './models/
|
|
38
|
+
export type * from './models/UserPrefs.ts'
|
|
39
39
|
export type * from './models/AgentMarketplace.ts'
|
|
40
40
|
export type * from './models/GitHubReconcileAudit.ts'
|
|
41
41
|
export type * from './models/GitHubRepoSnapshot.ts'
|
|
@@ -663,7 +663,7 @@ model ChatThreadWatch {
|
|
|
663
663
|
// `reminderHourLocal` is validated to be an integer in
|
|
664
664
|
// [0, 23] at the service layer (session-follow-service.ts), not here — Prisma
|
|
665
665
|
// has no native CHECK constraint support in this schema style.
|
|
666
|
-
model
|
|
666
|
+
model UserPrefs {
|
|
667
667
|
memberId String @id // AccountMember.id
|
|
668
668
|
timezone String? // IANA zone name, validated in UserPrefsService
|
|
669
669
|
autoFollowSessions Boolean @default(true)
|
|
@@ -672,7 +672,7 @@ model UserNotificationPrefs {
|
|
|
672
672
|
createdAt DateTime @default(now())
|
|
673
673
|
updatedAt DateTime @updatedAt
|
|
674
674
|
|
|
675
|
-
@@map("UserPrefs")
|
|
675
|
+
@@map("UserPrefs")
|
|
676
676
|
}
|
|
677
677
|
|
|
678
678
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* admin/src/account-member-removal.integration.test.ts
|
|
3
3
|
* AccountMemberService.remove deletes the removed person's access and
|
|
4
4
|
* notification rows (AgentMember, ChatThreadWatch, PushSubscription,
|
|
5
|
-
*
|
|
5
|
+
* UserPrefs), atomically with the removal. Real Postgres.
|
|
6
6
|
*
|
|
7
7
|
* Requires DATABASE_URL_ADMIN_TEST; skips otherwise.
|
|
8
8
|
*/
|
|
@@ -34,7 +34,7 @@ describeOrSkip("AccountMemberService.remove cleanup (integration)", () => {
|
|
|
34
34
|
await prisma.pushSubscription.create({
|
|
35
35
|
data: { endpoint: `ep-${label}`, p256dh: "p", auth: "a", memberId },
|
|
36
36
|
});
|
|
37
|
-
await prisma.
|
|
37
|
+
await prisma.userPrefs.create({ data: { memberId } });
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
async function rowCounts(memberId: string) {
|
|
@@ -42,7 +42,7 @@ describeOrSkip("AccountMemberService.remove cleanup (integration)", () => {
|
|
|
42
42
|
prisma.agentMember.count({ where: { memberId } }),
|
|
43
43
|
prisma.chatThreadWatch.count({ where: { memberId } }),
|
|
44
44
|
prisma.pushSubscription.count({ where: { memberId } }),
|
|
45
|
-
prisma.
|
|
45
|
+
prisma.userPrefs.count({ where: { memberId } }),
|
|
46
46
|
]);
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -55,7 +55,7 @@ describeOrSkip("AccountMemberService.remove cleanup (integration)", () => {
|
|
|
55
55
|
beforeEach(async () => {
|
|
56
56
|
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
57
57
|
service = new AccountMemberService(prisma);
|
|
58
|
-
await prisma.
|
|
58
|
+
await prisma.userPrefs.deleteMany();
|
|
59
59
|
await prisma.chatThreadWatch.deleteMany();
|
|
60
60
|
await prisma.pushSubscription.deleteMany();
|
|
61
61
|
await prisma.agentMember.deleteMany();
|
|
@@ -172,6 +172,6 @@ describeOrSkip("AccountMemberService.remove cleanup (integration)", () => {
|
|
|
172
172
|
).toBe(0);
|
|
173
173
|
expect(await prisma.chatThreadWatch.count()).toBe(0);
|
|
174
174
|
expect(await prisma.pushSubscription.count()).toBe(0);
|
|
175
|
-
expect(await prisma.
|
|
175
|
+
expect(await prisma.userPrefs.count()).toBe(0);
|
|
176
176
|
});
|
|
177
177
|
});
|
package/src/account-members.ts
CHANGED
|
@@ -50,7 +50,7 @@ export class AccountMemberService {
|
|
|
50
50
|
* belongs to any account.
|
|
51
51
|
*
|
|
52
52
|
* Null memberId rows for the email (AgentMember, ChatThreadWatch,
|
|
53
|
-
* PushSubscription,
|
|
53
|
+
* PushSubscription, UserPrefs) get the new id in the same
|
|
54
54
|
* transaction.
|
|
55
55
|
*
|
|
56
56
|
* @param client - pass a `tx` from `prisma.$transaction` to participate in it.
|
|
@@ -74,7 +74,7 @@ export class AccountMemberService {
|
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
76
|
* Remove a member and their access and notification rows (AgentMember,
|
|
77
|
-
* ChatThreadWatch, PushSubscription,
|
|
77
|
+
* ChatThreadWatch, PushSubscription, UserPrefs), matched by
|
|
78
78
|
* memberId, in one transaction.
|
|
79
79
|
*/
|
|
80
80
|
async remove(accountId: string, email: string): Promise<void> {
|
|
@@ -101,7 +101,7 @@ describeOrSkip("admin account move (integration)", () => {
|
|
|
101
101
|
|
|
102
102
|
beforeEach(async () => {
|
|
103
103
|
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
104
|
-
await prisma.
|
|
104
|
+
await prisma.userPrefs.deleteMany();
|
|
105
105
|
await prisma.chatThreadWatch.deleteMany();
|
|
106
106
|
await prisma.pushSubscription.deleteMany();
|
|
107
107
|
await prisma.agentMember.deleteMany();
|
|
@@ -175,7 +175,7 @@ describeOrSkip("admin account move (integration)", () => {
|
|
|
175
175
|
await prisma.pushSubscription.create({
|
|
176
176
|
data: { endpoint: "ep-1", p256dh: "p", auth: "a", memberId: ownerId },
|
|
177
177
|
});
|
|
178
|
-
await prisma.
|
|
178
|
+
await prisma.userPrefs.create({ data: { memberId: ownerId } });
|
|
179
179
|
});
|
|
180
180
|
|
|
181
181
|
afterEach(async () => {
|
|
@@ -239,7 +239,7 @@ describeOrSkip("admin account move (integration)", () => {
|
|
|
239
239
|
await prisma.pushSubscription.count({ where: { memberId: ownerId } }),
|
|
240
240
|
).toBe(1);
|
|
241
241
|
expect(
|
|
242
|
-
await prisma.
|
|
242
|
+
await prisma.userPrefs.count({
|
|
243
243
|
where: { memberId: ownerId },
|
|
244
244
|
}),
|
|
245
245
|
).toBe(1);
|
|
@@ -324,7 +324,7 @@ describeOrSkip("admin account move (integration)", () => {
|
|
|
324
324
|
expect(await snapshot(prisma)).toEqual(before);
|
|
325
325
|
// The member rows still point at the same member.
|
|
326
326
|
expect(
|
|
327
|
-
await prisma.
|
|
327
|
+
await prisma.userPrefs.count({
|
|
328
328
|
where: { memberId: ownerId },
|
|
329
329
|
}),
|
|
330
330
|
).toBe(1);
|
package/src/account-move.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* between accounts in the admin database, driven by a mapping file kept
|
|
6
6
|
* outside the repo. Members move in place (same AccountMember.id), so every
|
|
7
7
|
* row keyed by the member id (AgentMember, ChatThreadWatch, PushSubscription,
|
|
8
|
-
*
|
|
8
|
+
* UserPrefs) keeps pointing at them.
|
|
9
9
|
*
|
|
10
10
|
* planMove() reads only. applyMove() re-plans inside one serializable
|
|
11
11
|
* transaction and refuses (MoveRefusedError, nothing written) on any unclear
|
package/src/admin-boot.ts
CHANGED
|
@@ -47,8 +47,8 @@ import { createAccountCreatedNotifier } from "./account-created-notifier.ts";
|
|
|
47
47
|
import { AccountInviteService } from "./account-invites.ts";
|
|
48
48
|
import { AccountLifecycle } from "./account-lifecycle.ts";
|
|
49
49
|
import { AccountMemberService } from "./account-members.ts";
|
|
50
|
-
import { ensureAccountMember } from "./ensure-account-member.ts";
|
|
51
50
|
import { AccountOnboardingService } from "./account-onboarding.ts";
|
|
51
|
+
import { AccountRepoLookupService } from "./account-repo-lookup.ts";
|
|
52
52
|
import { AccountTrialExpirySweeper } from "./account-trial-expiry-sweeper.ts";
|
|
53
53
|
import { AccountTrialWarningSweeper } from "./account-trial-warning-sweeper.ts";
|
|
54
54
|
import { AccountService } from "./accounts.ts";
|
|
@@ -98,13 +98,10 @@ import {
|
|
|
98
98
|
import { parseChatCredentialConfig } from "./chat-credential-config.ts";
|
|
99
99
|
import { HttpChatServiceProvisioningClient } from "./chat-service-provisioning-client.ts";
|
|
100
100
|
import { SystemClock } from "./clock.ts";
|
|
101
|
-
import {
|
|
102
|
-
PrismaInstallationAgents,
|
|
103
|
-
PrismaInstallationAudit,
|
|
104
|
-
} from "./installation-ports.ts";
|
|
105
101
|
import { CronRunNowService } from "./cron-run-now.ts";
|
|
106
102
|
import { parseCronRunNowConfig } from "./cron-run-now-config.ts";
|
|
107
103
|
import { isDevAuthAllowed } from "./dev-auth-guard.ts";
|
|
104
|
+
import { ensureAccountMember } from "./ensure-account-member.ts";
|
|
108
105
|
import { HttpGithubAppProvisioningClient } from "./github-app-provisioning-client.ts";
|
|
109
106
|
import {
|
|
110
107
|
createGithubAccessPreview,
|
|
@@ -124,10 +121,13 @@ import {
|
|
|
124
121
|
} from "./github-repo-refresher.ts";
|
|
125
122
|
import { HttpGoogleAuthClient } from "./google-auth-client.ts";
|
|
126
123
|
import { HttpChatClient } from "./http-chat-client.ts";
|
|
124
|
+
import {
|
|
125
|
+
PrismaInstallationAgents,
|
|
126
|
+
PrismaInstallationAudit,
|
|
127
|
+
} from "./installation-ports.ts";
|
|
127
128
|
import { HttpOktaAuthClient } from "./okta-auth-client.ts";
|
|
128
129
|
import { mountPlatformApis } from "./platform-routes.ts";
|
|
129
130
|
import { parsePodServiceAccountConfig } from "./pod-service-account-config.ts";
|
|
130
|
-
import { AccountRepoLookupService } from "./account-repo-lookup.ts";
|
|
131
131
|
import { PrincipalLookupService } from "./principal-lookup.ts";
|
|
132
132
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
133
133
|
import { isPushEnabled } from "./push-sender.ts";
|
|
@@ -139,12 +139,14 @@ import { HttpSlackProvisioningClient } from "./slack-provisioning-client.ts";
|
|
|
139
139
|
import { SpecialtyTypes } from "./specialty-types.ts";
|
|
140
140
|
import { runStartupPolicyReconcile } from "./startup-policy-reconcile.ts";
|
|
141
141
|
import { runTemplateSeed } from "./template-seed.ts";
|
|
142
|
+
import { createTimezoneResolver } from "./timezone-resolver.ts";
|
|
142
143
|
import { makeStrictTokenCrypto, makeTokenCrypto } from "./token-crypto.ts";
|
|
143
144
|
import {
|
|
144
145
|
resolveTrialExpirySweepIntervalMs,
|
|
145
146
|
TrialExpirySweeper,
|
|
146
147
|
} from "./trial-expiry-sweeper.ts";
|
|
147
148
|
import { TrialExpiryWarningSweeper } from "./trial-expiry-warning-sweeper.ts";
|
|
149
|
+
import { UserPrefsService } from "./user-prefs.ts";
|
|
148
150
|
|
|
149
151
|
// startAdmin() is the process boot: it runs the real migration preflight,
|
|
150
152
|
// constructs a real PrismaClient (live DB connection), calls Bun.serve to bind
|
|
@@ -394,7 +396,13 @@ export async function startAdmin(
|
|
|
394
396
|
// may build on exists and before any route is mounted. With no extensions
|
|
395
397
|
// (or hookless ones) every contribution below is empty and the admin is the
|
|
396
398
|
// bare platform.
|
|
399
|
+
const resolveTimezone = createTimezoneResolver({
|
|
400
|
+
userPrefs: new UserPrefsService(prisma),
|
|
401
|
+
members: new AccountMemberService(prisma),
|
|
402
|
+
deployment: process.env.SHIPWRIGHT_ADMIN_TZ,
|
|
403
|
+
});
|
|
397
404
|
const extensionContext: AdminExtensionContext = {
|
|
405
|
+
resolveTimezone,
|
|
398
406
|
env: process.env,
|
|
399
407
|
prisma,
|
|
400
408
|
sessionSecret,
|
|
@@ -737,6 +745,7 @@ export async function startAdmin(
|
|
|
737
745
|
lifecycle: accountLifecycle,
|
|
738
746
|
},
|
|
739
747
|
timezone: adminTz,
|
|
748
|
+
userPrefs: new UserPrefsService(prisma),
|
|
740
749
|
...(chatClient ? { chatClient } : {}),
|
|
741
750
|
...(pushService
|
|
742
751
|
? {
|
package/src/admin-extension.ts
CHANGED
|
@@ -113,6 +113,15 @@ export interface AdminExtensionContext {
|
|
|
113
113
|
appBaseUrl: string;
|
|
114
114
|
/** SHIPWRIGHT_ADMIN_TZ, when set: the admin UI's display timezone. */
|
|
115
115
|
timezone?: string;
|
|
116
|
+
/**
|
|
117
|
+
* Per-request display timezone: the signed-in user's saved setting, then
|
|
118
|
+
* the browser `tz` cookie, then the deployment default. Prefer this over
|
|
119
|
+
* the boot-time `timezone` when rendering a page for a request.
|
|
120
|
+
*/
|
|
121
|
+
resolveTimezone?: (input: {
|
|
122
|
+
email?: string | undefined;
|
|
123
|
+
cookie?: string | undefined;
|
|
124
|
+
}) => Promise<string>;
|
|
116
125
|
/** Web Push, present only when VAPID is fully configured. */
|
|
117
126
|
push?: { service: PushService; maxDetail: string; vapidPublicKey: string };
|
|
118
127
|
/** Sentry, present only when SENTRY_DSN is set (as the admin API gets it). */
|
|
@@ -157,7 +166,7 @@ export interface AgentDetailCardContext {
|
|
|
157
166
|
isAdmin: boolean;
|
|
158
167
|
/** The agent's owning account; null for unassigned agents. */
|
|
159
168
|
accountId?: string | null;
|
|
160
|
-
/** The
|
|
169
|
+
/** The display timezone (IANA name), resolved for this request. */
|
|
161
170
|
timezone?: string;
|
|
162
171
|
}
|
|
163
172
|
|
|
@@ -58,7 +58,7 @@ function setup(opts: {
|
|
|
58
58
|
agentMember: { updateMany: async () => ({ count: 0 }) },
|
|
59
59
|
chatThreadWatch: { updateMany: async () => ({ count: 0 }) },
|
|
60
60
|
pushSubscription: { updateMany: async () => ({ count: 0 }) },
|
|
61
|
-
|
|
61
|
+
userPrefs: { updateMany: async () => ({ count: 0 }) },
|
|
62
62
|
} as unknown as PrismaTransactionClient;
|
|
63
63
|
|
|
64
64
|
const base = makeMockDeps();
|
package/src/admin-ui-layout.ts
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
import { themeBootScript } from "@app-vitals/agent-lib/web/toolbar.ts";
|
|
18
18
|
import { baseStyles, escapeHtml } from "./admin-ui-styles.ts";
|
|
19
|
+
import { tzCookieScript } from "./timezone-resolver.ts";
|
|
19
20
|
|
|
20
21
|
interface RenderAdminPageOptions {
|
|
21
22
|
/** Raw, unescaped page title — HTML-escaped internally before rendering. */
|
|
@@ -54,6 +55,7 @@ export function renderAdminPage(opts: RenderAdminPageOptions): string {
|
|
|
54
55
|
<title>${escapeHtml(opts.title)}</title>
|
|
55
56
|
<meta name="color-scheme" content="light dark" />
|
|
56
57
|
${themeBootScript()}
|
|
58
|
+
${tzCookieScript()}
|
|
57
59
|
<style>${baseStyles()}${opts.extraStyles ?? ""}</style>${opts.headExtra ? `\n ${opts.headExtra}` : ""}
|
|
58
60
|
</head>
|
|
59
61
|
${bodyTag}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/admin-ui-preferences-pages.ts
|
|
3
|
+
* Render function for Settings > Preferences (per-user display timezone).
|
|
4
|
+
* Pure string -> string; routes live in admin-ui-preferences.ts.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { renderAdminPage } from "./admin-ui-layout.ts";
|
|
8
|
+
import {
|
|
9
|
+
SETTINGS_PREFERENCES_PATH as BASE,
|
|
10
|
+
buildSettingsNav,
|
|
11
|
+
renderSettingsLayout,
|
|
12
|
+
type SettingsNav,
|
|
13
|
+
} from "./admin-ui-settings-layout.ts";
|
|
14
|
+
import { escapeHtml } from "./admin-ui-styles.ts";
|
|
15
|
+
|
|
16
|
+
export interface PreferencesPageOpts {
|
|
17
|
+
userEmail: string;
|
|
18
|
+
isAdmin?: boolean;
|
|
19
|
+
isOwner?: boolean;
|
|
20
|
+
nav?: SettingsNav;
|
|
21
|
+
/** Saved zone, or null when the browser timezone is used. */
|
|
22
|
+
savedTimezone: string | null;
|
|
23
|
+
/** The zone in effect for this request. */
|
|
24
|
+
effectiveTimezone: string;
|
|
25
|
+
/** Whether the viewer has a member profile to save onto. */
|
|
26
|
+
canSave: boolean;
|
|
27
|
+
/** IANA zone names for the picker. */
|
|
28
|
+
zones: readonly string[];
|
|
29
|
+
error?: string;
|
|
30
|
+
notice?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The IANA zones the runtime knows, always including UTC. */
|
|
34
|
+
export function supportedTimezones(): string[] {
|
|
35
|
+
const zones = Intl.supportedValuesOf("timeZone");
|
|
36
|
+
return zones.includes("UTC") ? zones : ["UTC", ...zones];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function renderPreferencesPage(o: PreferencesPageOpts): string {
|
|
40
|
+
const error = o.error
|
|
41
|
+
? `<div class="alert alert-error">${escapeHtml(o.error)}</div>`
|
|
42
|
+
: "";
|
|
43
|
+
const notice = o.notice
|
|
44
|
+
? `<div class="alert alert-success">${escapeHtml(o.notice)}</div>`
|
|
45
|
+
: "";
|
|
46
|
+
const options = [
|
|
47
|
+
`<option value=""${o.savedTimezone === null ? " selected" : ""}>Use browser timezone</option>`,
|
|
48
|
+
...o.zones.map(
|
|
49
|
+
(z) =>
|
|
50
|
+
`<option value="${escapeHtml(z)}"${z === o.savedTimezone ? " selected" : ""}>${escapeHtml(z)}</option>`,
|
|
51
|
+
),
|
|
52
|
+
].join("");
|
|
53
|
+
const form = o.canSave
|
|
54
|
+
? `<form method="POST" action="${BASE}/save" style="display:flex;gap:8px;align-items:end;flex-wrap:wrap">
|
|
55
|
+
<label>Timezone <select name="timezone" class="form-input">${options}</select></label>
|
|
56
|
+
<button type="submit" class="btn btn-primary">Save</button>
|
|
57
|
+
</form>
|
|
58
|
+
<form method="POST" action="${BASE}/clear" style="margin-top:8px">
|
|
59
|
+
<button type="submit" class="btn btn-secondary">Use browser timezone</button>
|
|
60
|
+
</form>`
|
|
61
|
+
: `<p class="help-text">Your sign-in has no member profile, so a timezone cannot be saved. The browser timezone is used.</p>`;
|
|
62
|
+
return renderAdminPage({
|
|
63
|
+
title: "Preferences — Settings — Shipwright Admin",
|
|
64
|
+
body: renderSettingsLayout({
|
|
65
|
+
userName: o.userEmail,
|
|
66
|
+
title: "Preferences",
|
|
67
|
+
nav: o.nav ?? buildSettingsNav({ preferences: true }),
|
|
68
|
+
active: BASE,
|
|
69
|
+
isAdmin: o.isAdmin ?? false,
|
|
70
|
+
isOwner: o.isOwner ?? false,
|
|
71
|
+
body: `${error}${notice}
|
|
72
|
+
<div class="card">
|
|
73
|
+
<div class="card-title">Display timezone</div>
|
|
74
|
+
<p class="help-text">Dates and times in the admin UI use this timezone. Leave it unset to follow your browser. Currently showing: ${escapeHtml(o.effectiveTimezone)}.</p>
|
|
75
|
+
${form}
|
|
76
|
+
</div>`,
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/admin-ui-preferences.smoke.test.ts
|
|
3
|
+
* Settings > Preferences: per-user timezone save/clear, and the per-request
|
|
4
|
+
* resolution chain (user setting, tz cookie, deployment default) through the
|
|
5
|
+
* full admin UI app.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, it } from "bun:test";
|
|
9
|
+
import { makeMockDeps, makeSessionCookie } from "./admin-ui.fixtures.ts";
|
|
10
|
+
import { createAdminUIApp } from "./admin-ui.ts";
|
|
11
|
+
import { InvalidTimezoneError, isValidIanaTimezone } from "./user-prefs.ts";
|
|
12
|
+
|
|
13
|
+
const EMAIL = "m@acme.com";
|
|
14
|
+
|
|
15
|
+
function setup(opts: { deployment?: string; member?: boolean } = {}) {
|
|
16
|
+
const store = new Map<string, string | null>();
|
|
17
|
+
const userPrefs = {
|
|
18
|
+
getTimezone: async (id: string) => store.get(id) ?? null,
|
|
19
|
+
upsertByMemberId: async (
|
|
20
|
+
id: string,
|
|
21
|
+
patch: { timezone?: string | null },
|
|
22
|
+
) => {
|
|
23
|
+
if (patch.timezone != null && !isValidIanaTimezone(patch.timezone)) {
|
|
24
|
+
throw new InvalidTimezoneError(patch.timezone);
|
|
25
|
+
}
|
|
26
|
+
store.set(id, patch.timezone ?? null);
|
|
27
|
+
return null;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
const accountServices = {
|
|
31
|
+
accounts: { getByMemberEmail: async () => ({ id: "a1", name: "Acme" }) },
|
|
32
|
+
members: {
|
|
33
|
+
getByEmail: async (e: string) =>
|
|
34
|
+
e === EMAIL && opts.member !== false
|
|
35
|
+
? { id: "mem-1", role: "member", accountId: "a1" }
|
|
36
|
+
: null,
|
|
37
|
+
},
|
|
38
|
+
} as never;
|
|
39
|
+
const app = createAdminUIApp(
|
|
40
|
+
makeMockDeps({
|
|
41
|
+
userPrefs,
|
|
42
|
+
accountServices,
|
|
43
|
+
...(opts.deployment ? { timezone: opts.deployment } : {}),
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
return { app, store };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const session = async () =>
|
|
50
|
+
`admin_session=${await makeSessionCookie(undefined, "u2", EMAIL, false)}`;
|
|
51
|
+
|
|
52
|
+
async function get(
|
|
53
|
+
app: ReturnType<typeof createAdminUIApp>,
|
|
54
|
+
path: string,
|
|
55
|
+
extraCookie = "",
|
|
56
|
+
) {
|
|
57
|
+
return app.request(path, {
|
|
58
|
+
headers: { cookie: `${await session()}${extraCookie}` },
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function post(
|
|
63
|
+
app: ReturnType<typeof createAdminUIApp>,
|
|
64
|
+
path: string,
|
|
65
|
+
fields: Record<string, string>,
|
|
66
|
+
) {
|
|
67
|
+
return app.request(path, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
body: new URLSearchParams(fields).toString(),
|
|
70
|
+
headers: {
|
|
71
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
72
|
+
cookie: await session(),
|
|
73
|
+
},
|
|
74
|
+
redirect: "manual",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The mock agent was created at 2024-01-01T00:00Z.
|
|
79
|
+
const LA_DATE = "12/31/2023";
|
|
80
|
+
const TOKYO_DATE = "1/1/2024";
|
|
81
|
+
|
|
82
|
+
describe("Settings > Preferences", () => {
|
|
83
|
+
it("renders the picker with the browser option and a nav entry", async () => {
|
|
84
|
+
const { app } = setup();
|
|
85
|
+
const res = await get(app, "/admin/settings/preferences");
|
|
86
|
+
expect(res.status).toBe(200);
|
|
87
|
+
const body = await res.text();
|
|
88
|
+
expect(body).toContain("Use browser timezone");
|
|
89
|
+
expect(body).toContain('<option value="Asia/Tokyo"');
|
|
90
|
+
expect(body).toContain('href="/admin/settings/preferences"');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("saves, shows, and clears the user timezone", async () => {
|
|
94
|
+
const { app, store } = setup();
|
|
95
|
+
const saved = await post(app, "/admin/settings/preferences/save", {
|
|
96
|
+
timezone: "Asia/Tokyo",
|
|
97
|
+
});
|
|
98
|
+
expect(saved.status).toBe(302);
|
|
99
|
+
expect(store.get("mem-1")).toBe("Asia/Tokyo");
|
|
100
|
+
const page = await (await get(app, "/admin/settings/preferences")).text();
|
|
101
|
+
expect(page).toContain('<option value="Asia/Tokyo" selected>');
|
|
102
|
+
|
|
103
|
+
const cleared = await post(app, "/admin/settings/preferences/clear", {});
|
|
104
|
+
expect(cleared.status).toBe(302);
|
|
105
|
+
expect(store.get("mem-1")).toBeNull();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("treats an empty selection as clear", async () => {
|
|
109
|
+
const { app, store } = setup();
|
|
110
|
+
store.set("mem-1", "Asia/Tokyo");
|
|
111
|
+
await post(app, "/admin/settings/preferences/save", { timezone: "" });
|
|
112
|
+
expect(store.get("mem-1")).toBeNull();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("rejects an invalid timezone with 422 and saves nothing", async () => {
|
|
116
|
+
const { app, store } = setup();
|
|
117
|
+
const res = await post(app, "/admin/settings/preferences/save", {
|
|
118
|
+
timezone: "Not/AZone",
|
|
119
|
+
});
|
|
120
|
+
expect(res.status).toBe(422);
|
|
121
|
+
expect(store.has("mem-1")).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("403s a save from a viewer with no member profile", async () => {
|
|
125
|
+
const { app } = setup({ member: false });
|
|
126
|
+
const res = await post(app, "/admin/settings/preferences/save", {
|
|
127
|
+
timezone: "UTC",
|
|
128
|
+
});
|
|
129
|
+
expect(res.status).toBe(403);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("includes the browser cookie script on pages", async () => {
|
|
133
|
+
const { app } = setup();
|
|
134
|
+
const body = await (await get(app, "/admin/agents")).text();
|
|
135
|
+
expect(body).toContain("resolvedOptions().timeZone");
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("per-request timezone resolution", () => {
|
|
140
|
+
it("uses the deployment default with no setting and no cookie", async () => {
|
|
141
|
+
const { app } = setup({ deployment: "America/Los_Angeles" });
|
|
142
|
+
expect(await (await get(app, "/admin/agents")).text()).toContain(LA_DATE);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("uses the browser cookie when nothing is saved", async () => {
|
|
146
|
+
const { app } = setup();
|
|
147
|
+
const body = await (
|
|
148
|
+
await get(app, "/admin/agents", "; tz=Asia%2FTokyo")
|
|
149
|
+
).text();
|
|
150
|
+
expect(body).toContain(TOKYO_DATE);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("ignores an invalid cookie", async () => {
|
|
154
|
+
const { app } = setup();
|
|
155
|
+
const res = await get(app, "/admin/agents", "; tz=Bogus%2FZone");
|
|
156
|
+
expect(res.status).toBe(200);
|
|
157
|
+
expect(await res.text()).toContain(LA_DATE);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("prefers the saved setting over the cookie", async () => {
|
|
161
|
+
const { app, store } = setup();
|
|
162
|
+
store.set("mem-1", "America/Los_Angeles");
|
|
163
|
+
const body = await (
|
|
164
|
+
await get(app, "/admin/agents", "; tz=Asia%2FTokyo")
|
|
165
|
+
).text();
|
|
166
|
+
expect(body).toContain(LA_DATE);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
@@ -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 };
|