(
input: MintOAuthConnectionInput,
)
| 2900 | // by the OAuth service) + produce the connection's tools. Mirrors |
| 2901 | // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns. |
| 2902 | const mintOAuthConnection = ( |
| 2903 | input: MintOAuthConnectionInput, |
| 2904 | ): Effect.Effect<Connection, StorageFailure> => |
| 2905 | Effect.gen(function* () { |
| 2906 | const name = connectionIdentifier(String(input.name)); |
| 2907 | yield* requireUserSubject(input.owner); |
| 2908 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2909 | if (!integrationRow) { |
| 2910 | return yield* new StorageError({ |
| 2911 | message: `Integration not found: ${input.integration}`, |
| 2912 | cause: undefined, |
| 2913 | }); |
| 2914 | } |
| 2915 | const keys = yield* Effect.try({ |
| 2916 | try: () => ownedKeys(input.owner), |
| 2917 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2918 | }); |
| 2919 | const now = new Date(); |
| 2920 | const ref: ConnectionRef = { |
| 2921 | owner: input.owner, |
| 2922 | integration: input.integration, |
| 2923 | name, |
| 2924 | }; |
| 2925 | // Label precedence: an explicit (user-chosen) label always wins; a |
| 2926 | // derived label (OIDC claims) only FILLS an empty slot. Like |
| 2927 | // `description` below, a reconnect or token refresh must not erase a |
| 2928 | // label the user curated. Resolved once, used by every write below. |
| 2929 | let identityLabel: string | null = null; |
| 2930 | yield* transaction( |
| 2931 | Effect.gen(function* () { |
| 2932 | const existing = yield* findConnectionRow(ref); |
| 2933 | const existingLabel = existing?.identity_label?.trim() ? existing.identity_label : null; |
| 2934 | identityLabel = |
| 2935 | input.identityLabel ?? existingLabel ?? input.derivedIdentityLabel ?? null; |
| 2936 | const set: Record<string, unknown> = { |
| 2937 | template: String(input.template), |
| 2938 | provider: input.provider, |
| 2939 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 2940 | identity_label: identityLabel, |
| 2941 | oauth_client: String(input.oauthClient), |
| 2942 | oauth_client_owner: input.oauthClientOwner, |
| 2943 | refresh_item_id: input.refreshItemId, |
| 2944 | expires_at: input.expiresAt, |
| 2945 | oauth_scope: input.oauthScope, |
| 2946 | oauth_token_url: input.oauthTokenUrl ?? null, |
| 2947 | provider_state: |
| 2948 | input.missingOAuthScopes && input.missingOAuthScopes.length > 0 |
| 2949 | ? { missingOAuthScopes: input.missingOAuthScopes } |
| 2950 | : null, |
| 2951 | updated_at: now, |
| 2952 | }; |
| 2953 | if (existing) { |
| 2954 | yield* core.updateMany("connection", { |
| 2955 | where: (b: AnyCb) => |
| 2956 | b.and( |
| 2957 | byOwner(input.owner)(b), |
| 2958 | b("integration", "=", String(input.integration)), |
| 2959 | b("name", "=", String(name)), |
no test coverage detected