Landing.utils.ts63 lines · main
| 1 | import { parseSchemaComment } from 'stripe-experiment-sync/supabase' |
| 2 | |
| 3 | import { type WrapperMeta } from '../Wrappers/Wrappers.types' |
| 4 | import { wrapperMetaComparator } from '../Wrappers/Wrappers.utils' |
| 5 | import { type IntegrationDefinition } from './Integrations.constants' |
| 6 | import { |
| 7 | isInstalled as checkIsInstalled, |
| 8 | findStripeSchema, |
| 9 | } from '@/components/interfaces/Integrations/templates/StripeSyncEngine/stripe-sync-status' |
| 10 | import { type APIKey } from '@/data/api-keys/api-keys-query' |
| 11 | import { type DatabaseExtension } from '@/data/database-extensions/database-extensions-query' |
| 12 | import { type Schema } from '@/data/database/schemas-query' |
| 13 | import { type FDW } from '@/data/fdw/fdws-query' |
| 14 | import { type ProjectSecret } from '@/data/secrets/secrets-query' |
| 15 | |
| 16 | export const isStripeSyncEngineInstalled = (schemas: Schema[]) => { |
| 17 | const stripeSchema = findStripeSchema(schemas) |
| 18 | const parsedSchema = parseSchemaComment(stripeSchema?.comment) |
| 19 | return checkIsInstalled(parsedSchema.status) |
| 20 | } |
| 21 | |
| 22 | export const isOAuthInstalled = ({ |
| 23 | integration, |
| 24 | apiKeys, |
| 25 | secrets, |
| 26 | }: { |
| 27 | integration: IntegrationDefinition |
| 28 | apiKeys: APIKey[] |
| 29 | secrets: ProjectSecret[] |
| 30 | }) => { |
| 31 | if (integration.installIdentificationMethod === 'secret_key_prefix') { |
| 32 | const prefix = integration.secretKeyPrefix |
| 33 | if (!prefix) return false |
| 34 | |
| 35 | return apiKeys.some((key) => key.type === 'secret' && key.name.startsWith(prefix)) |
| 36 | } |
| 37 | |
| 38 | if (integration.installIdentificationMethod === 'edge_function_secret_name') { |
| 39 | const secretName = integration.edgeFunctionSecretName |
| 40 | if (!secretName) return false |
| 41 | |
| 42 | return secrets.some((secret) => secret.name === secretName) |
| 43 | } |
| 44 | |
| 45 | return false |
| 46 | } |
| 47 | |
| 48 | export const hasMatchingWrapper = ({ meta, wrappers }: { meta: WrapperMeta; wrappers: FDW[] }) => { |
| 49 | return wrappers.find((w) => wrapperMetaComparator(meta, w)) |
| 50 | } |
| 51 | |
| 52 | export const hasRequiredExtensions = ({ |
| 53 | integration, |
| 54 | extensions, |
| 55 | }: { |
| 56 | integration: IntegrationDefinition |
| 57 | extensions: DatabaseExtension[] |
| 58 | }) => { |
| 59 | return integration.requiredExtensions.every((extName) => { |
| 60 | const foundExtension = extensions.find((ext) => ext.name === extName) |
| 61 | return !!foundExtension?.installed_version |
| 62 | }) |
| 63 | } |