(
input: MintOAuthConnectionInput,
)
| 2451 | // by the OAuth service) + produce the connection's tools. Mirrors |
| 2452 | // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns. |
| 2453 | const mintOAuthConnection = ( |
| 2454 | input: MintOAuthConnectionInput, |
| 2455 | ): Effect.Effect<Connection, StorageFailure> => |
| 2456 | Effect.gen(function* () { |
| 2457 | const name = connectionIdentifier(String(input.name)); |
| 2458 | yield* requireUserSubject(input.owner); |
| 2459 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2460 | if (!integrationRow) { |
| 2461 | return yield* new StorageError({ |
| 2462 | message: `Integration not found: ${input.integration}`, |
| 2463 | cause: undefined, |
| 2464 | }); |
| 2465 | } |
| 2466 | const keys = yield* Effect.try({ |
| 2467 | try: () => ownedKeys(input.owner), |
| 2468 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2469 | }); |
| 2470 | const now = new Date(); |
| 2471 | const ref: ConnectionRef = { |
| 2472 | owner: input.owner, |
| 2473 | integration: input.integration, |
| 2474 | name, |
| 2475 | }; |
| 2476 | yield* transaction( |
| 2477 | Effect.gen(function* () { |
| 2478 | const existing = yield* findConnectionRow(ref); |
| 2479 | const set: Record<string, unknown> = { |
| 2480 | template: String(input.template), |
| 2481 | provider: input.provider, |
| 2482 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 2483 | identity_label: input.identityLabel ?? null, |
| 2484 | oauth_client: String(input.oauthClient), |
| 2485 | oauth_client_owner: input.oauthClientOwner, |
| 2486 | refresh_item_id: input.refreshItemId, |
| 2487 | expires_at: input.expiresAt, |
| 2488 | oauth_scope: input.oauthScope, |
| 2489 | oauth_token_url: input.oauthTokenUrl ?? null, |
| 2490 | provider_state: |
| 2491 | input.missingOAuthScopes && input.missingOAuthScopes.length > 0 |
| 2492 | ? { missingOAuthScopes: input.missingOAuthScopes } |
| 2493 | : null, |
| 2494 | updated_at: now, |
| 2495 | }; |
| 2496 | if (existing) { |
| 2497 | yield* core.updateMany("connection", { |
| 2498 | where: (b: AnyCb) => |
| 2499 | b.and( |
| 2500 | byOwner(input.owner)(b), |
| 2501 | b("integration", "=", String(input.integration)), |
| 2502 | b("name", "=", String(name)), |
| 2503 | ), |
| 2504 | set, |
| 2505 | }); |
| 2506 | } else { |
| 2507 | yield* core.create("connection", { |
| 2508 | tenant: keys.tenant, |
| 2509 | owner: keys.owner, |
| 2510 | subject: keys.subject, |
no test coverage detected