Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.4 KB
Raw Blame
import { isNil } from '@activepieces/core-utils'
import { ConsumableFeatureId } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { captureLicenseKeyEvent, LicenseKeyEventPayload } from '../helper/telemetry.utils'
import { billingProvider, TrackAppSumoAiUsageParams, TrackCreditsParams, TrackFeatureParams } from './billing-provider'

export async function trackBillingAndSendTelemetry({ log, licenseKey, credits, appSumo, telemetry }: TrackBillingAndSendTelemetryParams): Promise<void> {
    const provider = billingProvider.get(log)
    const creditsEvent: TrackFeatureParams | undefined = isNil(credits) ? undefined : { ...credits, featureId: ConsumableFeatureId.AP_CREDITS }
    const appSumoEvent: TrackFeatureParams | undefined = isNil(appSumo) ? undefined : { ...appSumo, featureId: ConsumableFeatureId.APP_SUMO_AI_CREDITS }
    const tracked = [creditsEvent, appSumoEvent].filter((event): event is TrackFeatureParams => !isNil(event))
    await Promise.all(tracked.map((params) => provider.trackFeature(params)))
    if (isNil(licenseKey) || licenseKey.length === 0 || isNil(telemetry)) {
        return
    }
    captureLicenseKeyEvent({ licenseKey, ...telemetry })
}

type TrackBillingAndSendTelemetryParams = {
    log: FastifyBaseLogger
    licenseKey: string | null | undefined
    credits?: TrackCreditsParams
    appSumo?: TrackAppSumoAiUsageParams
    telemetry?: LicenseKeyEventPayload
}