(
input: MintOAuthConnectionInput,
)
| 2819 | // by the OAuth service) + produce the connection's tools. Mirrors |
| 2820 | // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns. |
| 2821 | const mintOAuthConnection = ( |
| 2822 | input: MintOAuthConnectionInput, |
| 2823 | ): Effect.Effect<Connection, StorageFailure> => |
| 2824 | Effect.gen(function* () { |
| 2825 | const name = connectionIdentifier(String(input.name)); |
| 2826 | yield* requireUserSubject(input.owner); |
| 2827 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2828 | if (!integrationRow) { |
| 2829 | return yield* new StorageError({ |
| 2830 | message: `Integration not found: ${input.integration}`, |
| 2831 | cause: undefined, |
| 2832 | }); |
| 2833 | } |
| 2834 | const keys = yield* Effect.try({ |
| 2835 | try: () => ownedKeys(input.owner), |
| 2836 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2837 | }); |
| 2838 | const now = new Date(); |
| 2839 | const ref: ConnectionRef = { |
| 2840 | owner: input.owner, |
| 2841 | integration: input.integration, |
| 2842 | name, |
| 2843 | }; |
| 2844 | // Label precedence: an explicit (user-chosen) label always wins; a |
| 2845 | // derived label (OIDC claims) only FILLS an empty slot. Like |
| 2846 | // `description` below, a reconnect or token refresh must not erase a |
| 2847 | // label the user curated. Resolved once, used by every write below. |
| 2848 | let identityLabel: string | null = null; |
| 2849 | yield* transaction( |
| 2850 | Effect.gen(function* () { |
| 2851 | const existing = yield* findConnectionRow(ref); |
| 2852 | const existingLabel = existing?.identity_label?.trim() ? existing.identity_label : null; |
| 2853 | identityLabel = |
| 2854 | input.identityLabel ?? existingLabel ?? input.derivedIdentityLabel ?? null; |
| 2855 | const set: Record<string, unknown> = { |
| 2856 | template: String(input.template), |
| 2857 | provider: input.provider, |
| 2858 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 2859 | identity_label: identityLabel, |
| 2860 | oauth_client: String(input.oauthClient), |
| 2861 | oauth_client_owner: input.oauthClientOwner, |
| 2862 | refresh_item_id: input.refreshItemId, |
| 2863 | expires_at: input.expiresAt, |
| 2864 | oauth_scope: input.oauthScope, |
| 2865 | oauth_token_url: input.oauthTokenUrl ?? null, |
| 2866 | provider_state: |
| 2867 | input.missingOAuthScopes && input.missingOAuthScopes.length > 0 |
| 2868 | ? { missingOAuthScopes: input.missingOAuthScopes } |
| 2869 | : null, |
| 2870 | updated_at: now, |
| 2871 | }; |
| 2872 | if (existing) { |
| 2873 | yield* core.updateMany("connection", { |
| 2874 | where: (b: AnyCb) => |
| 2875 | b.and( |
| 2876 | byOwner(input.owner)(b), |
| 2877 | b("integration", "=", String(input.integration)), |
| 2878 | b("name", "=", String(name)), |
no test coverage detected