users.ts94 lines · main
| 1 | /** |
| 2 | * briven-engine users on Doltgres. |
| 3 | */ |
| 4 | |
| 5 | import { getEnginePool } from './db.js'; |
| 6 | import { isAuthCoreInitialized } from './engine.js'; |
| 7 | |
| 8 | export type BrivenEngineUserSummary = { |
| 9 | id: string; |
| 10 | emails: string[]; |
| 11 | phoneNumbers: string[]; |
| 12 | tenantId: string; |
| 13 | timeJoined: number; |
| 14 | engine: 'briven-engine'; |
| 15 | storage: 'doltgres'; |
| 16 | }; |
| 17 | |
| 18 | export async function listBrivenEngineUsers(opts?: { |
| 19 | limit?: number; |
| 20 | paginationToken?: string; |
| 21 | tenantId?: string; |
| 22 | }): Promise<{ |
| 23 | users: BrivenEngineUserSummary[]; |
| 24 | nextPaginationToken?: string; |
| 25 | engine: 'briven-engine'; |
| 26 | storage: 'doltgres'; |
| 27 | }> { |
| 28 | if (!isAuthCoreInitialized()) { |
| 29 | return { users: [], engine: 'briven-engine', storage: 'doltgres' }; |
| 30 | } |
| 31 | const limit = opts?.limit ?? 50; |
| 32 | const pool = getEnginePool(); |
| 33 | const res = opts?.tenantId |
| 34 | ? await pool.query( |
| 35 | `SELECT id, email, phone, tenant_id, time_joined FROM be_users |
| 36 | WHERE tenant_id = $1 |
| 37 | ORDER BY time_joined DESC LIMIT $2`, |
| 38 | [opts.tenantId, limit], |
| 39 | ) |
| 40 | : await pool.query( |
| 41 | `SELECT id, email, phone, tenant_id, time_joined FROM be_users |
| 42 | ORDER BY time_joined DESC LIMIT $1`, |
| 43 | [limit], |
| 44 | ); |
| 45 | const users: BrivenEngineUserSummary[] = res.rows.map( |
| 46 | (u: { |
| 47 | id: string; |
| 48 | email: string | null; |
| 49 | phone: string | null; |
| 50 | tenant_id: string; |
| 51 | time_joined: Date | string; |
| 52 | }) => ({ |
| 53 | id: u.id, |
| 54 | emails: u.email ? [u.email] : [], |
| 55 | phoneNumbers: u.phone ? [u.phone] : [], |
| 56 | tenantId: u.tenant_id, |
| 57 | timeJoined: new Date(u.time_joined).getTime(), |
| 58 | engine: 'briven-engine' as const, |
| 59 | storage: 'doltgres' as const, |
| 60 | }), |
| 61 | ); |
| 62 | return { users, engine: 'briven-engine', storage: 'doltgres' }; |
| 63 | } |
| 64 | |
| 65 | export async function getBrivenEngineUserMetadata( |
| 66 | userId: string, |
| 67 | ): Promise<Record<string, unknown> | null> { |
| 68 | if (!isAuthCoreInitialized()) return null; |
| 69 | const pool = getEnginePool(); |
| 70 | const res = await pool.query( |
| 71 | `SELECT metadata_json FROM be_users WHERE id = $1 LIMIT 1`, |
| 72 | [userId], |
| 73 | ); |
| 74 | const row = res.rows[0] as { metadata_json: string } | undefined; |
| 75 | if (!row) return {}; |
| 76 | try { |
| 77 | return JSON.parse(row.metadata_json) as Record<string, unknown>; |
| 78 | } catch { |
| 79 | return {}; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | export async function updateBrivenEngineUserMetadata( |
| 84 | userId: string, |
| 85 | metadata: Record<string, unknown>, |
| 86 | ): Promise<boolean> { |
| 87 | if (!isAuthCoreInitialized()) return false; |
| 88 | const pool = getEnginePool(); |
| 89 | const res = await pool.query( |
| 90 | `UPDATE be_users SET metadata_json = $2 WHERE id = $1`, |
| 91 | [userId, JSON.stringify(metadata)], |
| 92 | ); |
| 93 | return (res.rowCount ?? 0) > 0; |
| 94 | } |