Files
OJ2/apps/api/src/services/profile.ts
T
xuyue d25b5dbe6d
Deploy / deploy (push) Canceled after 0s
update
2026-09-27 19:36:28 +08:00

36 lines
1.2 KiB
TypeScript

import type { SessionUser, UserProfile } from "@oj2/contract"
import { and, eq } from "drizzle-orm"
import { db, schema } from "../db"
export async function getUserProfileById(userId: number, showRealName: boolean) {
const [row] = await db
.select({ profile: schema.userProfile, user: schema.user })
.from(schema.userProfile)
.innerJoin(schema.user, eq(schema.userProfile.userId, schema.user.id))
.where(and(eq(schema.user.id, userId), eq(schema.user.isDisabled, false)))
.limit(1)
if (!row) return null
return {
id: row.profile.id,
user: {
id: row.user.id,
username: row.user.username,
email: row.user.email,
adminType: row.user.adminType,
problemPermission: row.user.problemPermission,
createTime: row.user.createTime,
lastLogin: row.user.lastLogin,
isDisabled: row.user.isDisabled,
className: row.user.className,
} satisfies SessionUser,
realName: showRealName ? row.profile.realName : null,
acmProblemsStatus: row.profile.acmProblemsStatus,
avatar: row.profile.avatar,
mood: row.profile.mood,
acceptedNumber: row.profile.acceptedNumber,
submissionNumber: row.profile.submissionNumber,
} satisfies UserProfile
}