| 274 | * If a duplicate is detected (unique constraint violation), logs a warning and returns success. |
| 275 | */ |
| 276 | export async function safeAccountInsert( |
| 277 | data: AccountInsertData, |
| 278 | context: { provider: string; identifier?: string } |
| 279 | ): Promise<void> { |
| 280 | try { |
| 281 | await db.insert(account).values(data) |
| 282 | logger.info(`Created new ${context.provider} account for user`, { userId: data.userId }) |
| 283 | } catch (error: any) { |
| 284 | if (error?.code === '23505') { |
| 285 | logger.error(`Duplicate ${context.provider} account detected, credential already exists`, { |
| 286 | userId: data.userId, |
| 287 | identifier: context.identifier, |
| 288 | }) |
| 289 | } else { |
| 290 | throw error |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get a credential by resolved account ID and verify it belongs to the user. |