(
input: MintOAuthConnectionInput,
)
| 2960 | // by the OAuth service) + produce the connection's tools. Mirrors |
| 2961 | // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns. |
| 2962 | const mintOAuthConnection = ( |
| 2963 | input: MintOAuthConnectionInput, |
| 2964 | ): Effect.Effect<Connection, StorageFailure> => |
| 2965 | Effect.gen(function* () { |
| 2966 | const name = connectionIdentifier(String(input.name)); |
| 2967 | yield* requireUserSubject(input.owner); |
| 2968 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2969 | if (!integrationRow) { |
| 2970 | return yield* new StorageError({ |
| 2971 | message: `Integration not found: ${input.integration}`, |
| 2972 | cause: undefined, |
| 2973 | }); |
| 2974 | } |
| 2975 | const keys = yield* Effect.try({ |
| 2976 | try: () => ownedKeys(input.owner), |
| 2977 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2978 | }); |
| 2979 | const now = new Date(); |
| 2980 | const ref: ConnectionRef = { |
| 2981 | owner: input.owner, |
| 2982 | integration: input.integration, |
| 2983 | name, |
| 2984 | }; |
| 2985 | // Label precedence: an explicit (user-chosen) label always wins; a |
| 2986 | // derived label (OIDC claims) only FILLS an empty slot. Like |
| 2987 | // `description` below, a reconnect or token refresh must not erase a |
| 2988 | // label the user curated. Resolved once, used by every write below. |
| 2989 | let identityLabel: string | null = null; |
| 2990 | yield* transaction( |
| 2991 | Effect.gen(function* () { |
| 2992 | const existing = yield* findConnectionRow(ref); |
| 2993 | const existingLabel = existing?.identity_label?.trim() ? existing.identity_label : null; |
| 2994 | identityLabel = |
| 2995 | input.identityLabel ?? existingLabel ?? input.derivedIdentityLabel ?? null; |
| 2996 | const set: Record<string, unknown> = { |
| 2997 | template: String(input.template), |
| 2998 | provider: input.provider, |
| 2999 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 3000 | identity_label: identityLabel, |
| 3001 | oauth_client: String(input.oauthClient), |
| 3002 | oauth_client_owner: input.oauthClientOwner, |
| 3003 | refresh_item_id: input.refreshItemId, |
| 3004 | expires_at: input.expiresAt, |
| 3005 | oauth_scope: input.oauthScope, |
| 3006 | oauth_token_url: input.oauthTokenUrl ?? null, |
| 3007 | provider_state: |
| 3008 | input.missingOAuthScopes && input.missingOAuthScopes.length > 0 |
| 3009 | ? { missingOAuthScopes: input.missingOAuthScopes } |
| 3010 | : null, |
| 3011 | // A re-mint replaces the grant, so any persisted verdict describes |
| 3012 | // a credential that no longer exists. Clear it rather than let a |
| 3013 | // pre-reconnect "expired" outlive the reconnect; the next health |
| 3014 | // check writes the verdict for the new grant. |
| 3015 | last_health: null, |
| 3016 | updated_at: now, |
| 3017 | }; |
| 3018 | if (existing) { |
| 3019 | yield* core.updateMany("connection", { |
no test coverage detected