migration.ts86 lines · main
| 1 | /** |
| 2 | * User import into Doltgres briven-engine. |
| 3 | */ |
| 4 | |
| 5 | import { isAuthCoreInitialized } from './engine.js'; |
| 6 | import { signUpEmailPassword } from './emailpassword.js'; |
| 7 | import { log } from '../../lib/logger.js'; |
| 8 | |
| 9 | export type ImportUserInput = { |
| 10 | email?: string; |
| 11 | phoneNumber?: string; |
| 12 | passwordHash?: string; |
| 13 | hashingAlgorithm?: string; |
| 14 | passwordPlaintext?: string; |
| 15 | userId?: string; |
| 16 | tenantId?: string; |
| 17 | }; |
| 18 | |
| 19 | export type ImportUsersResult = { |
| 20 | engine: 'briven-engine'; |
| 21 | storage: 'doltgres'; |
| 22 | ok: boolean; |
| 23 | imported: number; |
| 24 | failed: number; |
| 25 | errors: Array<{ index: number; message: string }>; |
| 26 | message?: string; |
| 27 | }; |
| 28 | |
| 29 | export async function importBrivenEngineUsers( |
| 30 | users: ImportUserInput[], |
| 31 | ): Promise<ImportUsersResult> { |
| 32 | const base = { |
| 33 | engine: 'briven-engine' as const, |
| 34 | storage: 'doltgres' as const, |
| 35 | imported: 0, |
| 36 | failed: 0, |
| 37 | errors: [] as Array<{ index: number; message: string }>, |
| 38 | }; |
| 39 | |
| 40 | if (!isAuthCoreInitialized()) { |
| 41 | return { |
| 42 | ...base, |
| 43 | ok: false, |
| 44 | message: 'briven-engine not ready on Doltgres', |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | for (let i = 0; i < users.length; i++) { |
| 49 | const u = users[i]!; |
| 50 | try { |
| 51 | if (!u.email || !u.passwordPlaintext) { |
| 52 | base.failed++; |
| 53 | base.errors.push({ |
| 54 | index: i, |
| 55 | message: 'email + passwordPlaintext required (hash import later)', |
| 56 | }); |
| 57 | continue; |
| 58 | } |
| 59 | const res = await signUpEmailPassword({ |
| 60 | email: u.email, |
| 61 | password: u.passwordPlaintext, |
| 62 | tenantId: u.tenantId ?? 'public', |
| 63 | }); |
| 64 | if (res.status !== 'OK') { |
| 65 | base.failed++; |
| 66 | base.errors.push({ index: i, message: res.status }); |
| 67 | continue; |
| 68 | } |
| 69 | base.imported++; |
| 70 | } catch (err) { |
| 71 | base.failed++; |
| 72 | base.errors.push({ |
| 73 | index: i, |
| 74 | message: err instanceof Error ? err.message : String(err), |
| 75 | }); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | log.info('briven_engine_import_users', { |
| 80 | imported: base.imported, |
| 81 | failed: base.failed, |
| 82 | storage: 'doltgres', |
| 83 | }); |
| 84 | |
| 85 | return { ...base, ok: base.failed === 0 && base.imported > 0 }; |
| 86 | } |