Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

3.6 KB
Raw Blame
import fetchMock from "fetch-mock";
import { match } from "ts-pattern";

import {
  createMockCloudAddOns,
  mockAdvancedTransformsCloudAddOn,
} from "metabase-types/api/mocks/add-ons";
import type { AddOnProductType } from "metabase-types/api/store";

export function setupBillingEndpoints({
  billingPeriodMonths = 12,
  hasBasicTransformsAddOn = true,
  hasAdvancedTransformsAddOn = true,
  skipCloudAddOns = false,
  previousAddOns = [],
}: {
  billingPeriodMonths?: number;
  hasBasicTransformsAddOn?: boolean;
  hasAdvancedTransformsAddOn?: boolean;
  skipCloudAddOns?: boolean;
  previousAddOns?: Array<{
    product_type: AddOnProductType;
    self_service: boolean;
  }>;
} = {}) {
  const cloudAddOns = [
    ...(hasBasicTransformsAddOn
      ? [
          {
            id: 1,
            name: "Basic Transforms Add-on metered",
            short_name: "Basic Transforms - metered",
            description: null,
            active: true,
            self_service: true,
            deployment: "hosting",
            billing_period_months: billingPeriodMonths,
            default_base_fee: 0,
            default_included_units: 0,
            default_prepaid_units: 1,
            default_price_per_unit: 0.01,
            default_total_units: 1,
            free_units: 1000,
            is_metered: true,
            product_type: "transforms-basic-metered",
            token_features: ["transforms-basic"],
            trial_days: 0,
            product_tiers: [],
          },
        ]
      : []),
    ...(hasAdvancedTransformsAddOn
      ? [
          {
            ...mockAdvancedTransformsCloudAddOn,
            billing_period_months: billingPeriodMonths,
          },
        ]
      : []),
  ];

  if (!skipCloudAddOns) {
    fetchMock.get("path:/api/ee/cloud-add-ons/addons", cloudAddOns);
  }

  fetchMock.get("path:/api/ee/billing", {
    version: "0",
    data: {
      billing_period_months: billingPeriodMonths,
      previous_add_ons: previousAddOns,
    },
  });
}

export function setupStoreEEBillingEndpoint(
  billing_period_months: number,
  had_metabot: false | "trial" | "tiered" = false,
  simulate_http_get_error: boolean = false,
) {
  fetchMock.get("path:/api/ee/billing", {
    body: {
      version: "v1",
      data: {
        billing_period_months,
        previous_add_ons:
          had_metabot === "trial"
            ? [{ product_type: "metabase-ai", self_service: true }]
            : had_metabot === "tiered"
              ? [{ product_type: "metabase-ai-tiered", self_service: true }]
              : null,
      },
    },
    status: simulate_http_get_error ? 500 : 200,
  });
}

export function setupStoreEECloudAddOnsEndpoint(
  billing_period_months: number,
  simulate_http_get_error: boolean = false,
) {
  fetchMock.get("path:/api/ee/cloud-add-ons/addons", {
    body: createMockCloudAddOns({ billing_period_months }),
    status: simulate_http_get_error ? 500 : 200,
  });
}

export function setupStoreEETieredMetabotAI(
  simulate_http_post_error:
    | false
    | "error-no-quantity"
    | "error-no-connection" = false,
) {
  fetchMock.post(
    "path:/api/ee/cloud-add-ons/metabase-ai-tiered",
    match(simulate_http_post_error)
      .with("error-no-quantity", () => ({
        body: { errors: { quantity: "Purchase of add-on requires quantity." } },
        status: 403,
      }))
      .with("error-no-connection", () => ({
        // eslint-disable-next-line metabase/no-literal-metabase-strings -- Used for fetch mock only
        body: "Could not establish a connection to Metabase Cloud.",
        status: 404,
      }))
      .otherwise(() => ({
        status: 200,
      })),
  );
}