Landing.utils.ts 2.1 KB

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