All files / libs/reporting/dashboards-v2/state/src/lib/api-client dashboard-charts.api-client.ts

0% Statements 0/148
0% Branches 0/1
0% Functions 0/1
0% Lines 0/148

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                                                                                                                                                                                                                                                                                         
import { http } from '@amalia/core/http/client';
import { bufferCalls } from '@amalia/ext/typescript';
import { type CustomReport, type CustomReportConfigurationField } from '@amalia/reporting/custom-reports/shared';
import {
  type ChartFacet,
  type ChartType,
  type CreateDashboardChartRequest,
  type Dashboard,
  type DashboardChart,
  type DashboardChartConfiguration,
  type DashboardChartFacet,
  type DashboardChartFacets,
  type DashboardChartFiltersOverride,
  type DashboardChartResult,
  type DashboardChartSort,
  type PatchDashboardChartRequest,
} from '@amalia/reporting/dashboards-v2/types';

const e = encodeURIComponent;

export class DashboardChartsApiClient {
  private static readonly DASHBOARDS_API_URL = '/dashboards-v2';

  private static readonly CHARTS_API_URL = `/dashboards-v2-charts`;

  //////////////////////////
  // QUERY METHODS
  //////////////////////////

  public static async getChartData<TChartType extends ChartType = ChartType>(
    dashboardId: Dashboard['id'],
    chartId: DashboardChart<TChartType>['id'],
    filters: DashboardChartFiltersOverride,
    sort: DashboardChartSort | null,
  ): Promise<DashboardChartResult> {
    const { data } = await http.post<DashboardChartResult>(
      `${DashboardChartsApiClient.DASHBOARDS_API_URL}/${e(dashboardId)}/charts/${e(chartId)}/data`,
      {
        filters,
        sort,
      },
    );

    return data;
  }

  public static async getChartFacet<TChartType extends ChartType, TChartFacet extends ChartFacet>(
    dashboardId: Dashboard['id'],
    chartId: DashboardChart<TChartType>['id'],
    field: CustomReportConfigurationField,
  ): Promise<DashboardChartFacet<TChartFacet>[]> {
    const { data } = await http.post<DashboardChartFacet<TChartFacet>[]>(
      `${DashboardChartsApiClient.DASHBOARDS_API_URL}/${e(dashboardId)}/charts/${e(chartId)}/facet-options`,
      { field },
    );

    return data;
  }

  public static async getChartFacets<TChartType extends ChartType = ChartType>(
    dashboardId: Dashboard['id'],
    chartId: DashboardChart<TChartType>['id'],
  ): Promise<DashboardChartFacets> {
    const { data } = await http.get<DashboardChartFacets>(
      `${DashboardChartsApiClient.DASHBOARDS_API_URL}/${e(dashboardId)}/charts/${e(chartId)}/facets`,
    );

    return data;
  }

  public static async getChartPreviewData<TChartType extends ChartType = ChartType>(
    chartConfiguration: DashboardChartConfiguration<TChartType>,
  ): Promise<DashboardChartResult> {
    const { data } = await http.post<DashboardChartResult>(
      `${DashboardChartsApiClient.CHARTS_API_URL}/preview`,
      chartConfiguration,
    );
    return data;
  }

  public static async getChartFacetsForPreview<TChartType extends ChartType = ChartType>(
    chartConfiguration: DashboardChartConfiguration<TChartType>,
  ): Promise<DashboardChartFacets> {
    const { data } = await http.post<DashboardChartFacets>(
      `${DashboardChartsApiClient.CHARTS_API_URL}/facets-preview`,
      chartConfiguration,
    );
    return data;
  }

  public static readonly findCharts = bufferCalls<
    [customReportId: CustomReport['id']],
    DashboardChart[],
    Record<CustomReport['id'], DashboardChart[]>
  >(
    async (bufferedArgs): Promise<Record<CustomReport['id'], DashboardChart[]>> => {
      const { data } = await http.get<Record<CustomReport['id'], DashboardChart[]>>(
        DashboardChartsApiClient.CHARTS_API_URL,
        {
          params: {
            customReportIds: bufferedArgs.map(([customReportId]) => customReportId),
          },
        },
      );

      return data;
    },
    ([customReportId], result) => result[customReportId] ?? [],
  );

  public static async findChartsOnDashboard(dashboardId: Dashboard['id']): Promise<DashboardChart[]> {
    const { data } = await http.get<DashboardChart[]>(
      `${DashboardChartsApiClient.DASHBOARDS_API_URL}/${e(dashboardId)}/charts`,
    );
    return data;
  }

  //////////////////////////
  // MUTATION METHODS
  //////////////////////////

  public static async createChart<TChartType extends ChartType = ChartType>(
    dashboardId: Dashboard['id'],
    createDashboardChartRequest: CreateDashboardChartRequest<TChartType>,
  ): Promise<DashboardChart<TChartType>> {
    const { data } = await http.post<DashboardChart<TChartType>>(
      `${DashboardChartsApiClient.DASHBOARDS_API_URL}/${e(dashboardId)}/charts`,
      createDashboardChartRequest,
    );
    return data;
  }

  public static async patchChart<TChartType extends ChartType = ChartType>(
    dashboardId: Dashboard['id'],
    chartId: DashboardChart<TChartType>['id'],
    patchDashboardChartRequest: PatchDashboardChartRequest<TChartType>,
  ): Promise<DashboardChart<TChartType>> {
    const { data } = await http.patch<DashboardChart<TChartType>>(
      `${DashboardChartsApiClient.DASHBOARDS_API_URL}/${e(dashboardId)}/charts/${e(chartId)}`,
      patchDashboardChartRequest,
    );
    return data;
  }

  public static async deleteChart(dashboardId: Dashboard['id'], chartId: DashboardChart['id']): Promise<void> {
    await http.delete(`${DashboardChartsApiClient.DASHBOARDS_API_URL}/${e(dashboardId)}/charts/${e(chartId)}`);
  }
}