(
input: MintOAuthConnectionInput,
)
| 2351 | // by the OAuth service) + produce the connection's tools. Mirrors |
| 2352 | // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns. |
| 2353 | const mintOAuthConnection = ( |
| 2354 | input: MintOAuthConnectionInput, |
| 2355 | ): Effect.Effect<Connection, StorageFailure> => |
| 2356 | Effect.gen(function* () { |
| 2357 | const name = connectionIdentifier(String(input.name)); |
| 2358 | yield* requireUserSubject(input.owner); |
| 2359 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2360 | if (!integrationRow) { |
| 2361 | return yield* new StorageError({ |
| 2362 | message: `Integration not found: ${input.integration}`, |
| 2363 | cause: undefined, |
| 2364 | }); |
| 2365 | } |
| 2366 | const keys = yield* Effect.try({ |
| 2367 | try: () => ownedKeys(input.owner), |
| 2368 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2369 | }); |
| 2370 | const now = new Date(); |
| 2371 | const ref: ConnectionRef = { |
| 2372 | owner: input.owner, |
| 2373 | integration: input.integration, |
| 2374 | name, |
| 2375 | }; |
| 2376 | yield* transaction( |
| 2377 | Effect.gen(function* () { |
| 2378 | const existing = yield* findConnectionRow(ref); |
| 2379 | const set: Record<string, unknown> = { |
| 2380 | template: String(input.template), |
| 2381 | provider: input.provider, |
| 2382 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 2383 | identity_label: input.identityLabel ?? null, |
| 2384 | oauth_client: String(input.oauthClient), |
| 2385 | oauth_client_owner: input.oauthClientOwner, |
| 2386 | refresh_item_id: input.refreshItemId, |
| 2387 | expires_at: input.expiresAt, |
| 2388 | oauth_scope: input.oauthScope, |
| 2389 | oauth_token_url: input.oauthTokenUrl ?? null, |
| 2390 | updated_at: now, |
| 2391 | }; |
| 2392 | if (existing) { |
| 2393 | yield* core.updateMany("connection", { |
| 2394 | where: (b: AnyCb) => |
| 2395 | b.and( |
| 2396 | byOwner(input.owner)(b), |
| 2397 | b("integration", "=", String(input.integration)), |
| 2398 | b("name", "=", String(name)), |
| 2399 | ), |
| 2400 | set, |
| 2401 | }); |
| 2402 | } else { |
| 2403 | yield* core.create("connection", { |
| 2404 | tenant: keys.tenant, |
| 2405 | owner: keys.owner, |
| 2406 | subject: keys.subject, |
| 2407 | integration: String(input.integration), |
| 2408 | name: String(name), |
| 2409 | template: String(input.template), |
| 2410 | provider: input.provider, |
no test coverage detected