| 1 | export type Role = "member" | "admin"; |
| 2 | export type UserStatus = "active" | "suspended" | "deleted"; |
| 3 | |
| 4 | // A full `users` row as stored in D1. |
| 5 | export interface UserRow { |
| 6 | id: number; |
| 7 | handle: string; |
| 8 | email: string; |
| 9 | email_verified: number; |
| 10 | password_hash: string | null; |
| 11 | display_name: string; |
| 12 | avatar_url: string; |
| 13 | bio: string; |
| 14 | role: Role; |
| 15 | status: UserStatus; |
| 16 | created_at: string; |
| 17 | updated_at: string; |
| 18 | } |
| 19 | |
| 20 | // The authenticated owner's view of their own account (login, /me). Camel-cased |
| 21 | // and free of the password hash, so it is safe to serialize directly. |
| 22 | export interface AccountUser { |
| 23 | id: number; |
| 24 | handle: string; |
| 25 | email: string; |
| 26 | emailVerified: boolean; |
| 27 | displayName: string; |
| 28 | avatarUrl: string; |
| 29 | bio: string; |
| 30 | role: Role; |
| 31 | status: UserStatus; |
| 32 | createdAt: string; |
| 33 | } |
| 34 | |
| 35 | // What anyone may see at /u/<handle>. No email, no role, no status. |
| 36 | export interface PublicUser { |
| 37 | handle: string; |
| 38 | displayName: string; |
| 39 | avatarUrl: string; |
| 40 | bio: string; |
| 41 | joinedAt: string; |
| 42 | } |
| 43 | |
| 44 | export function toAccountUser(row: UserRow): AccountUser { |
| 45 | return { |
| 46 | id: row.id, |
| 47 | handle: row.handle, |
| 48 | email: row.email, |
| 49 | emailVerified: row.email_verified === 1, |
| 50 | displayName: row.display_name, |
| 51 | avatarUrl: row.avatar_url, |
| 52 | bio: row.bio, |
| 53 | role: row.role, |
| 54 | status: row.status, |
| 55 | createdAt: row.created_at, |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | export function toPublicUser(row: UserRow): PublicUser { |
| 60 | return { |
| 61 | handle: row.handle, |
| 62 | displayName: row.display_name || row.handle, |
| 63 | avatarUrl: row.avatar_url, |
| 64 | bio: row.bio, |
| 65 | joinedAt: row.created_at, |
| 66 | }; |
| 67 | } |
| 68 |