fix: use real settings data boundaries
This commit is contained in:
228
modules/core/server/settings.ts
Normal file
228
modules/core/server/settings.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
import { desc, eq, sql } from "drizzle-orm";
|
||||
|
||||
import { getDatabase } from "@/modules/core/server/db";
|
||||
import {
|
||||
accounts,
|
||||
beds,
|
||||
joinRequests,
|
||||
memberships,
|
||||
organizationInvitations,
|
||||
organizations,
|
||||
roles,
|
||||
systemIncidents,
|
||||
} from "@/modules/core/server/schema";
|
||||
import type {
|
||||
Account,
|
||||
JoinRequest,
|
||||
Membership,
|
||||
Organization,
|
||||
OrganizationInvitation,
|
||||
SystemIncident,
|
||||
} from "@/modules/core/types";
|
||||
|
||||
function iso(value: Date): string {
|
||||
return value.toISOString();
|
||||
}
|
||||
|
||||
function toOrganization(row: typeof organizations.$inferSelect): Organization {
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
slug: row.slug,
|
||||
status: row.status,
|
||||
registrationEnabled: row.registrationEnabled,
|
||||
oidcEnabled: row.oidcEnabled,
|
||||
oidcIssuerUrl: row.oidcIssuerUrl,
|
||||
oidcClientId: row.oidcClientId,
|
||||
oidcHasClientSecret: row.oidcClientSecret.length > 0,
|
||||
oidcScopes: row.oidcScopes,
|
||||
oidcRedirectUri: row.oidcRedirectUri,
|
||||
oidcAvatarClaim: row.oidcAvatarClaim,
|
||||
oidcAutoProvision: row.oidcAutoProvision,
|
||||
createdAt: iso(row.createdAt),
|
||||
updatedAt: iso(row.updatedAt),
|
||||
};
|
||||
}
|
||||
|
||||
function toAccount(row: typeof accounts.$inferSelect): Account {
|
||||
return {
|
||||
id: row.id,
|
||||
platformRoleId: row.platformRoleId ?? undefined,
|
||||
name: row.name,
|
||||
email: row.email,
|
||||
avatarUrl: row.avatarUrl,
|
||||
role: row.platformRoleId ?? "viewer",
|
||||
passwordHash: row.passwordHash,
|
||||
passwordSalt: row.passwordSalt,
|
||||
status: row.status,
|
||||
createdAt: iso(row.createdAt),
|
||||
updatedAt: iso(row.updatedAt),
|
||||
};
|
||||
}
|
||||
|
||||
function toMembership(row: typeof memberships.$inferSelect, role: typeof roles.$inferSelect): Membership {
|
||||
return {
|
||||
id: row.id,
|
||||
accountId: row.accountId,
|
||||
organizationId: row.organizationId,
|
||||
roleId: row.roleId,
|
||||
roleKey: role.key,
|
||||
roleLabel: role.label,
|
||||
status: row.status,
|
||||
createdAt: iso(row.createdAt),
|
||||
updatedAt: iso(row.updatedAt),
|
||||
};
|
||||
}
|
||||
|
||||
function toJoinRequest(row: {
|
||||
accountEmail: string;
|
||||
accountName: string;
|
||||
organizationName: string;
|
||||
request: typeof joinRequests.$inferSelect;
|
||||
}): JoinRequest {
|
||||
return {
|
||||
id: row.request.id,
|
||||
accountId: row.request.accountId,
|
||||
accountName: row.accountName,
|
||||
accountEmail: row.accountEmail,
|
||||
organizationId: row.request.organizationId,
|
||||
organizationName: row.organizationName,
|
||||
status: row.request.status,
|
||||
reason: row.request.reason,
|
||||
reviewedByAccountId: row.request.reviewedByAccountId ?? undefined,
|
||||
reviewedAt: row.request.reviewedAt ? iso(row.request.reviewedAt) : undefined,
|
||||
createdAt: iso(row.request.createdAt),
|
||||
};
|
||||
}
|
||||
|
||||
function toOrganizationInvitation(row: {
|
||||
invitation: typeof organizationInvitations.$inferSelect;
|
||||
organizationName: string;
|
||||
roleLabel: string;
|
||||
}): OrganizationInvitation {
|
||||
return {
|
||||
id: row.invitation.id,
|
||||
organizationId: row.invitation.organizationId,
|
||||
organizationName: row.organizationName,
|
||||
roleId: row.invitation.roleId,
|
||||
roleLabel: row.roleLabel,
|
||||
email: row.invitation.email,
|
||||
token: row.invitation.token,
|
||||
status: row.invitation.status as OrganizationInvitation["status"],
|
||||
createdByAccountId: row.invitation.createdByAccountId ?? undefined,
|
||||
acceptedByAccountId: row.invitation.acceptedByAccountId ?? undefined,
|
||||
acceptedAt: row.invitation.acceptedAt ? iso(row.invitation.acceptedAt) : undefined,
|
||||
expiresAt: iso(row.invitation.expiresAt),
|
||||
createdAt: iso(row.invitation.createdAt),
|
||||
updatedAt: iso(row.invitation.updatedAt),
|
||||
};
|
||||
}
|
||||
|
||||
function toSystemIncident(row: typeof systemIncidents.$inferSelect): SystemIncident {
|
||||
return {
|
||||
id: row.id,
|
||||
organizationId: row.organizationId ?? undefined,
|
||||
severity: row.severity,
|
||||
status: row.status,
|
||||
title: row.title,
|
||||
description: row.description,
|
||||
source: row.source,
|
||||
createdAt: iso(row.createdAt),
|
||||
updatedAt: iso(row.updatedAt),
|
||||
};
|
||||
}
|
||||
|
||||
export async function listSettingsOrganizations(params: { organizationId?: string } = {}): Promise<Organization[]> {
|
||||
const database = getDatabase();
|
||||
const rows = await database
|
||||
.select()
|
||||
.from(organizations)
|
||||
.where(params.organizationId ? eq(organizations.id, params.organizationId) : sql`true`)
|
||||
.orderBy(desc(organizations.createdAt));
|
||||
|
||||
return rows.map(toOrganization);
|
||||
}
|
||||
|
||||
export async function listSettingsAccounts(params: { organizationId?: string } = {}): Promise<Account[]> {
|
||||
const database = getDatabase();
|
||||
const rows = params.organizationId
|
||||
? await database
|
||||
.select({ account: accounts })
|
||||
.from(accounts)
|
||||
.innerJoin(memberships, eq(memberships.accountId, accounts.id))
|
||||
.where(eq(memberships.organizationId, params.organizationId))
|
||||
.orderBy(desc(accounts.createdAt))
|
||||
: await database.select({ account: accounts }).from(accounts).orderBy(desc(accounts.createdAt));
|
||||
|
||||
return rows.map((row) => toAccount(row.account));
|
||||
}
|
||||
|
||||
export async function listSettingsMemberships(params: { organizationId?: string } = {}): Promise<Membership[]> {
|
||||
const database = getDatabase();
|
||||
const rows = await database
|
||||
.select({ membership: memberships, role: roles })
|
||||
.from(memberships)
|
||||
.innerJoin(roles, eq(roles.id, memberships.roleId))
|
||||
.where(params.organizationId ? eq(memberships.organizationId, params.organizationId) : sql`true`)
|
||||
.orderBy(desc(memberships.createdAt));
|
||||
|
||||
return rows.map((row) => toMembership(row.membership, row.role));
|
||||
}
|
||||
|
||||
export async function listSettingsJoinRequests(params: { organizationId?: string } = {}): Promise<JoinRequest[]> {
|
||||
const database = getDatabase();
|
||||
const rows = await database
|
||||
.select({
|
||||
request: joinRequests,
|
||||
accountName: accounts.name,
|
||||
accountEmail: accounts.email,
|
||||
organizationName: organizations.name,
|
||||
})
|
||||
.from(joinRequests)
|
||||
.innerJoin(accounts, eq(accounts.id, joinRequests.accountId))
|
||||
.innerJoin(organizations, eq(organizations.id, joinRequests.organizationId))
|
||||
.where(params.organizationId ? eq(joinRequests.organizationId, params.organizationId) : sql`true`)
|
||||
.orderBy(desc(joinRequests.createdAt));
|
||||
|
||||
return rows.map(toJoinRequest);
|
||||
}
|
||||
|
||||
export async function listSettingsOrganizationInvitations(
|
||||
params: { organizationId?: string } = {},
|
||||
): Promise<OrganizationInvitation[]> {
|
||||
const database = getDatabase();
|
||||
const rows = await database
|
||||
.select({
|
||||
invitation: organizationInvitations,
|
||||
organizationName: organizations.name,
|
||||
roleLabel: roles.label,
|
||||
})
|
||||
.from(organizationInvitations)
|
||||
.innerJoin(organizations, eq(organizations.id, organizationInvitations.organizationId))
|
||||
.innerJoin(roles, eq(roles.id, organizationInvitations.roleId))
|
||||
.where(params.organizationId ? eq(organizationInvitations.organizationId, params.organizationId) : sql`true`)
|
||||
.orderBy(desc(organizationInvitations.createdAt));
|
||||
|
||||
return rows.map(toOrganizationInvitation);
|
||||
}
|
||||
|
||||
export async function listSettingsIncidents(params: { organizationId?: string } = {}): Promise<SystemIncident[]> {
|
||||
const database = getDatabase();
|
||||
const rows = await database
|
||||
.select()
|
||||
.from(systemIncidents)
|
||||
.where(params.organizationId ? eq(systemIncidents.organizationId, params.organizationId) : sql`true`)
|
||||
.orderBy(desc(systemIncidents.createdAt));
|
||||
|
||||
return rows.map(toSystemIncident);
|
||||
}
|
||||
|
||||
export async function countSettingsBeds(params: { organizationId?: string } = {}): Promise<number> {
|
||||
const database = getDatabase();
|
||||
const rows = await database
|
||||
.select({ count: sql<number>`count(*)::int` })
|
||||
.from(beds)
|
||||
.where(params.organizationId ? eq(beds.organizationId, params.organizationId) : sql`true`);
|
||||
|
||||
return rows[0]?.count ?? 0;
|
||||
}
|
||||
@@ -73,7 +73,13 @@ export function OrganizationManagementClient(): React.ReactElement {
|
||||
>
|
||||
<form className="grid gap-4" onSubmit={handleSubmit}>
|
||||
<Input label="机构名称" name="name" placeholder="机构名称" required />
|
||||
<Input label="机构标识" name="slug" placeholder="可留空" />
|
||||
<Input
|
||||
label="机构标识"
|
||||
name="slug"
|
||||
pattern="[a-z0-9][a-z0-9-]{1,47}"
|
||||
placeholder="sunrise-care"
|
||||
required
|
||||
/>
|
||||
{message ? (
|
||||
<p className="rounded-md bg-secondary px-3 py-2 text-sm text-secondary-foreground" role="status">
|
||||
{message}
|
||||
@@ -196,7 +202,13 @@ export function OrganizationRowActions({
|
||||
>
|
||||
<form className="grid gap-4" onSubmit={handleSubmit}>
|
||||
<Input defaultValue={organization.name} label="机构名称" name="name" required />
|
||||
<Input defaultValue={organization.slug} label="机构标识" name="slug" required />
|
||||
<Input
|
||||
defaultValue={organization.slug}
|
||||
label="机构标识"
|
||||
name="slug"
|
||||
pattern="[a-z0-9][a-z0-9-]{1,47}"
|
||||
required
|
||||
/>
|
||||
<Select
|
||||
defaultValue={organization.status}
|
||||
label="状态"
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import type { LucideIcon } from "lucide-react";
|
||||
import { Database, ExternalLink } from "lucide-react";
|
||||
import { Database } from "lucide-react";
|
||||
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { LinkButton } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
|
||||
type ModulePageProps = {
|
||||
@@ -13,12 +12,6 @@ type ModulePageProps = {
|
||||
phase: "待接入" | "二期预留" | "三期预留";
|
||||
};
|
||||
|
||||
const realWorkspaceLinks = [
|
||||
{ href: "/app/dashboard", label: "运营看板" },
|
||||
{ href: "/app/elders", label: "老人档案" },
|
||||
{ href: "/app/beds", label: "床位房间" },
|
||||
];
|
||||
|
||||
export function ModulePage({ title, eyebrow, description, icon: Icon, phase }: ModulePageProps): React.ReactElement {
|
||||
return (
|
||||
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6">
|
||||
@@ -38,45 +31,19 @@ export function ModulePage({ title, eyebrow, description, icon: Icon, phase }: M
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_24rem]">
|
||||
<Card>
|
||||
<section>
|
||||
<Card className="max-w-3xl">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<Database className="size-5 text-primary" aria-hidden="true" />
|
||||
数据状态
|
||||
未接入真实数据源
|
||||
</CardTitle>
|
||||
<CardDescription>当前模块未接入真实业务数据源。</CardDescription>
|
||||
<CardDescription>当前模块暂不展示业务记录、统计指标或流程入口。</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<dl className="grid gap-3 text-sm">
|
||||
<div className="flex items-center justify-between gap-4 border-b pb-3">
|
||||
<dt className="text-muted-foreground">业务记录</dt>
|
||||
<dd className="font-medium">暂不展示</dd>
|
||||
</div>
|
||||
<div className="flex items-center justify-between gap-4 border-b pb-3">
|
||||
<dt className="text-muted-foreground">统计指标</dt>
|
||||
<dd className="font-medium">暂不展示</dd>
|
||||
</div>
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<dt className="text-muted-foreground">待办流程</dt>
|
||||
<dd className="font-medium">待真实流程接入</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">已接入工作区</CardTitle>
|
||||
<CardDescription>以下页面展示真实系统数据。</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="grid gap-2">
|
||||
{realWorkspaceLinks.map((link) => (
|
||||
<LinkButton className="w-full justify-between" href={link.href} key={link.href} variant="outline">
|
||||
{link.label}
|
||||
<ExternalLink className="size-4" aria-hidden="true" />
|
||||
</LinkButton>
|
||||
))}
|
||||
<p className="text-sm text-muted-foreground">
|
||||
接入对应的 Drizzle 查询和操作 API 前,这里只保留模块位置,不渲染未落库记录或伪造待办。
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user