| 313 | const email = (profile as { email?: unknown } | undefined)?.email; |
| 314 | const organizationId = (profile as ExtendedProfile | undefined)?.openAiChatGptOrganizationId; |
| 315 | const owner: OpenAiChatGptOwner = { |
| 316 | kiloUserId: userId, |
| 317 | organizationId: organizationId ?? null, |
| 318 | }; |
| 319 | await saveOpenAiChatGptConnection( |
| 320 | owner, |
| 321 | { |
| 322 | access_token: accessToken, |
| 323 | ...(account.refresh_token ? { refresh_token: account.refresh_token } : {}), |
| 324 | expires_at: account.expires_at ?? Math.floor(Date.now() / 1000) + 3600, |
| 325 | ...(account.scope ? { scope: account.scope } : {}), |
| 326 | ...(account.token_type ? { token_type: account.token_type } : {}), |
| 327 | issuer: OPENAI_ISSUER, |
| 328 | client_id: OPENAI_CLIENT_ID, |
| 329 | subject, |
| 330 | ...(typeof email === 'string' && email ? { email } : {}), |
| 331 | connected_at: new Date().toISOString(), |
| 332 | status: 'connected', |
| 333 | }, |
| 334 | userId |
| 335 | ); |
| 336 | } catch (error) { |
| 337 | captureException(error, { |
| 338 | tags: { operation: 'openai_chatgpt_connection_persist' }, |
| 339 | }); |