Skip to content

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

1.6 KB
Raw Blame
import type {
  CreateTenantInput,
  Tenant,
  UpdateTenantInput,
} from "metabase-types/api";

import { EnterpriseApi } from "./api";
import { idTag, invalidateTags, listTag } from "./tags";

export const tenantsApi = EnterpriseApi.injectEndpoints({
  endpoints: (builder) => ({
    createTenant: builder.mutation<void, CreateTenantInput>({
      query: (body) => ({
        method: "POST",
        url: "/api/ee/tenant",
        body,
      }),
      invalidatesTags: (_, error) =>
        invalidateTags(error, [
          listTag("tenant"),
          listTag("setup-guide-checklist"),
        ]),
    }),
    getTenant: builder.query<Tenant, Tenant["id"]>({
      query: (id) => ({
        method: "GET",
        url: `/api/ee/tenant/${id}`,
      }),
      providesTags: (_, __, id) => [idTag("tenant", id)],
    }),
    listTenants: builder.query<
      { data: Tenant[] },
      { status: "active" | "deactivated" | "all" }
    >({
      query: (params) => ({
        method: "GET",
        url: "/api/ee/tenant",
        params,
      }),
      providesTags: [listTag("tenant")],
    }),
    updateTenant: builder.mutation<void, UpdateTenantInput>({
      query: ({ id, ...body }) => ({
        method: "PUT",
        url: `/api/ee/tenant/${id}`,
        body,
      }),
      invalidatesTags: (_, error, { id }) =>
        invalidateTags(error, [
          listTag("tenant"),
          idTag("tenant", id),
          // since users inherit tenant attributes, we can get stale caches
          "user",
        ]),
    }),
  }),
});

export const {
  useCreateTenantMutation,
  useGetTenantQuery,
  useListTenantsQuery,
  useUpdateTenantMutation,
} = tenantsApi;