Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { Effect } from 'effect'; |
| 2 | import { |
| 3 | analyticsIdentityLinkingEnabled, |
| 4 | linkApolloIdentityForAnalytics, |
| 5 | } from 'src/analytics/dispatch'; |
| 6 | import { getSessionInfoByUserApiKey } from 'src/services/composio-account'; |
| 7 | |
| 8 | type KnownOrgIdentity = { |
| 9 | readonly orgId: string; |
| 10 | readonly orgMemberId: string; |
| 11 | }; |
| 12 | |
| 13 | export const linkAnalyticsIdentityForOrg = (params: { |
| 14 | readonly apiKey: string; |
| 15 | readonly orgId: string; |
| 16 | readonly knownIdentity?: KnownOrgIdentity; |
| 17 | }) => |
| 18 | Effect.gen(function* () { |
| 19 | const knownIdentity = |
| 20 | params.knownIdentity?.orgId === params.orgId ? params.knownIdentity : undefined; |
| 21 | if (!knownIdentity && !(yield* analyticsIdentityLinkingEnabled)) { |
| 22 | return; |
| 23 | } |
| 24 | const orgMemberId = knownIdentity |
| 25 | ? knownIdentity.orgMemberId |
| 26 | : yield* getSessionInfoByUserApiKey({ |
| 27 | userApiKey: params.apiKey, |
| 28 | orgId: params.orgId, |
| 29 | }).pipe( |
| 30 | Effect.map(sessionInfo => sessionInfo.org_member.id), |
| 31 | Effect.catch(error => |
| 32 | Effect.logDebug('Failed to resolve analytics identity for selected org:', error).pipe( |
| 33 | Effect.as(undefined) |
| 34 | ) |
| 35 | ) |
| 36 | ); |
| 37 | |
| 38 | if (orgMemberId) { |
| 39 | yield* linkApolloIdentityForAnalytics(orgMemberId, params.apiKey); |
| 40 | } |
| 41 | }); |