(
input: MintOAuthConnectionInput,
)
| 2542 | // by the OAuth service) + produce the connection's tools. Mirrors |
| 2543 | // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns. |
| 2544 | const mintOAuthConnection = ( |
| 2545 | input: MintOAuthConnectionInput, |
| 2546 | ): Effect.Effect<Connection, StorageFailure> => |
| 2547 | Effect.gen(function* () { |
| 2548 | const name = connectionIdentifier(String(input.name)); |
| 2549 | yield* requireUserSubject(input.owner); |
| 2550 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2551 | if (!integrationRow) { |
| 2552 | return yield* new StorageError({ |
| 2553 | message: `Integration not found: ${input.integration}`, |
| 2554 | cause: undefined, |
| 2555 | }); |
| 2556 | } |
| 2557 | const keys = yield* Effect.try({ |
| 2558 | try: () => ownedKeys(input.owner), |
| 2559 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2560 | }); |
| 2561 | const now = new Date(); |
| 2562 | const ref: ConnectionRef = { |
| 2563 | owner: input.owner, |
| 2564 | integration: input.integration, |
| 2565 | name, |
| 2566 | }; |
| 2567 | // Label precedence: an explicit (user-chosen) label always wins; a |
| 2568 | // derived label (OIDC claims) only FILLS an empty slot. Like |
| 2569 | // `description` below, a reconnect or token refresh must not erase a |
| 2570 | // label the user curated. Resolved once, used by every write below. |
| 2571 | let identityLabel: string | null = null; |
| 2572 | yield* transaction( |
| 2573 | Effect.gen(function* () { |
| 2574 | const existing = yield* findConnectionRow(ref); |
| 2575 | const existingLabel = existing?.identity_label?.trim() ? existing.identity_label : null; |
| 2576 | identityLabel = |
| 2577 | input.identityLabel ?? existingLabel ?? input.derivedIdentityLabel ?? null; |
| 2578 | const set: Record<string, unknown> = { |
| 2579 | template: String(input.template), |
| 2580 | provider: input.provider, |
| 2581 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 2582 | identity_label: identityLabel, |
| 2583 | oauth_client: String(input.oauthClient), |
| 2584 | oauth_client_owner: input.oauthClientOwner, |
| 2585 | refresh_item_id: input.refreshItemId, |
| 2586 | expires_at: input.expiresAt, |
| 2587 | oauth_scope: input.oauthScope, |
| 2588 | oauth_token_url: input.oauthTokenUrl ?? null, |
| 2589 | provider_state: |
| 2590 | input.missingOAuthScopes && input.missingOAuthScopes.length > 0 |
| 2591 | ? { missingOAuthScopes: input.missingOAuthScopes } |
| 2592 | : null, |
| 2593 | updated_at: now, |
| 2594 | }; |
| 2595 | if (existing) { |
| 2596 | yield* core.updateMany("connection", { |
| 2597 | where: (b: AnyCb) => |
| 2598 | b.and( |
| 2599 | byOwner(input.owner)(b), |
| 2600 | b("integration", "=", String(input.integration)), |
| 2601 | b("name", "=", String(name)), |
no test coverage detected