import { eq } from 'drizzle-orm' /** * 用户状态(`users.status`)的唯一口径。 * * ⚠️ 这里踩过一次真实的坑:小程序端一直发**大写** `ACTIVE`, * 而服务端与 DB 用的是**小写** `active` / `withdrawn` / `graduate`(登录、2FA、WebAuthn * 等地方都按小写判断)。于是管理员在用户管理里**编辑任何用户**都会被 * 「用户状态只能是 active, withdrawn 或 graduate」拒绝 —— 看起来像"编辑功能坏了"。 * * 所以状态统一走这里的 `normalizeUserStatus()`:**大小写不敏感**, * 认不出来的值返回 null,由调用方决定是报错还是忽略(创建用户时忽略、编辑时可报错)。 */ export const USER_STATUSES = ['active', 'withdrawn', 'graduate'] as const export type UserStatus = (typeof USER_STATUSES)[number] /** 归一化:`ACTIVE` / `active` / ` Active ` → `active`;其它 → null */ export function normalizeUserStatus(value: unknown): UserStatus | null { const s = String(value ?? '') .trim() .toLowerCase() return (USER_STATUSES as readonly string[]).includes(s) ? (s as UserStatus) : null } /** 统一的错误文案(列出允许值,别让调用方各写各的) */ export const USER_STATUS_RULE_MESSAGE = '用户状态只能是 active、withdrawn 或 graduate'