feat: complete user management workflow
This commit is contained in:
@@ -251,7 +251,7 @@ export async function createRegistration(input: {
|
||||
email: string;
|
||||
password: string;
|
||||
organizationId?: string;
|
||||
}): Promise<{ account: PublicAccount; session: Session }> {
|
||||
}): Promise<{ account: PublicAccount; session?: Session }> {
|
||||
const database = getDatabase();
|
||||
const password = hashPassword(input.password);
|
||||
const expiresAt = new Date(Date.now() + SESSION_MAX_AGE_SECONDS * 1000);
|
||||
@@ -278,18 +278,16 @@ export async function createRegistration(input: {
|
||||
throw new Error("账号创建失败");
|
||||
}
|
||||
|
||||
const sessionRows = await transaction
|
||||
.insert(sessions)
|
||||
.values({
|
||||
accountId: account.id,
|
||||
activeOrganizationId: input.organizationId,
|
||||
expiresAt,
|
||||
})
|
||||
.returning();
|
||||
const sessionRows = input.organizationId
|
||||
? []
|
||||
: await transaction
|
||||
.insert(sessions)
|
||||
.values({
|
||||
accountId: account.id,
|
||||
expiresAt,
|
||||
})
|
||||
.returning();
|
||||
const session = sessionRows[0];
|
||||
if (!session) {
|
||||
throw new Error("会话创建失败");
|
||||
}
|
||||
|
||||
if (input.organizationId) {
|
||||
await transaction.insert(joinRequests).values({
|
||||
@@ -308,7 +306,7 @@ export async function createRegistration(input: {
|
||||
}
|
||||
|
||||
const publicAccount = toPublicAccount(created.account);
|
||||
const session = toSession(created.session);
|
||||
const session = created.session ? toSession(created.session) : undefined;
|
||||
await recordAuditLog({
|
||||
actor: publicAccount,
|
||||
organizationId: input.organizationId,
|
||||
@@ -334,7 +332,7 @@ export async function loginWithPassword(input: {
|
||||
.limit(1);
|
||||
const account = accountRows[0];
|
||||
|
||||
if (!account || account.status === "disabled" || !verifyPassword(input.password, account)) {
|
||||
if (!account || account.status !== "active" || !verifyPassword(input.password, account)) {
|
||||
throw new Error("邮箱或密码错误");
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
admissions,
|
||||
beds,
|
||||
elders,
|
||||
joinRequests,
|
||||
memberships,
|
||||
organizations,
|
||||
roles,
|
||||
@@ -14,7 +15,18 @@ import {
|
||||
sessions,
|
||||
systemIncidents,
|
||||
} from "@/modules/core/server/schema";
|
||||
import type { Account, Admission, AppData, FacilityBed, Membership, Organization, Room, Session, SystemIncident } from "@/modules/core/types";
|
||||
import type {
|
||||
Account,
|
||||
Admission,
|
||||
AppData,
|
||||
FacilityBed,
|
||||
JoinRequest,
|
||||
Membership,
|
||||
Organization,
|
||||
Room,
|
||||
Session,
|
||||
SystemIncident,
|
||||
} from "@/modules/core/types";
|
||||
import type { Elder } from "@/modules/elders/types";
|
||||
|
||||
function iso(value: Date): string {
|
||||
@@ -27,6 +39,7 @@ export async function readData(): Promise<AppData> {
|
||||
organizationRows,
|
||||
accountRows,
|
||||
membershipRows,
|
||||
joinRequestRows,
|
||||
sessionRows,
|
||||
elderRows,
|
||||
roomRows,
|
||||
@@ -38,6 +51,17 @@ export async function readData(): Promise<AppData> {
|
||||
database.select().from(organizations).orderBy(desc(organizations.createdAt)),
|
||||
database.select().from(accounts).orderBy(desc(accounts.createdAt)),
|
||||
database.select({ membership: memberships, role: roles }).from(memberships).innerJoin(roles, eq(roles.id, memberships.roleId)),
|
||||
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))
|
||||
.orderBy(desc(joinRequests.createdAt)),
|
||||
database.select().from(sessions).orderBy(desc(sessions.createdAt)),
|
||||
database.select().from(elders).orderBy(desc(elders.createdAt)),
|
||||
database.select().from(rooms).orderBy(desc(rooms.createdAt)),
|
||||
@@ -120,6 +144,20 @@ export async function readData(): Promise<AppData> {
|
||||
};
|
||||
});
|
||||
|
||||
const joinRequestsData: JoinRequest[] = joinRequestRows.map((row) => ({
|
||||
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),
|
||||
}));
|
||||
|
||||
const sessionsData: Session[] = sessionRows.map((row) => ({
|
||||
id: row.id,
|
||||
accountId: row.accountId,
|
||||
@@ -204,6 +242,7 @@ export async function readData(): Promise<AppData> {
|
||||
organizations: organizationsData,
|
||||
accounts: accountsData,
|
||||
memberships: membershipsData,
|
||||
joinRequests: joinRequestsData,
|
||||
sessions: sessionsData,
|
||||
elders: eldersData,
|
||||
rooms: roomsData,
|
||||
|
||||
@@ -86,6 +86,22 @@ export type Membership = {
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type JoinRequestStatus = "pending" | "approved" | "rejected";
|
||||
|
||||
export type JoinRequest = {
|
||||
id: string;
|
||||
accountId: string;
|
||||
accountName: string;
|
||||
accountEmail: string;
|
||||
organizationId: string;
|
||||
organizationName: string;
|
||||
status: JoinRequestStatus;
|
||||
reason: string;
|
||||
reviewedByAccountId?: string;
|
||||
reviewedAt?: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type Account = {
|
||||
id: string;
|
||||
platformRoleId?: string;
|
||||
@@ -189,6 +205,7 @@ export type AppData = {
|
||||
organizations: Organization[];
|
||||
accounts: Account[];
|
||||
memberships: Membership[];
|
||||
joinRequests: JoinRequest[];
|
||||
sessions: Session[];
|
||||
elders: Elder[];
|
||||
rooms: Room[];
|
||||
|
||||
Reference in New Issue
Block a user