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

97.24% Statements 106/109
100% Branches 11/11
91.66% Functions 11/12
97.24% Lines 106/109

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 1101x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { http } from '@amalia/core/http/client';
import {
  type CreateDashboardRequest,
  type Dashboard,
  type DashboardShareCandidate,
  type DashboardWithSharedUsers,
  type DuplicateDashboardRequest,
  type PatchDashboardRequest,
  type PostDashboardSharedUsersRequest,
} from '@amalia/reporting/dashboards-v2/types';
import { type UserComputed } from '@amalia/tenants/users/types';
 
const e = encodeURIComponent;
 
export abstract class DashboardsApiClient {
  public static readonly API_ENDPOINT = '/dashboards/v2';
 
  //////////////////////////
  // QUERY METHODS
  //////////////////////////
 
  public static async findDashboard(dashboardId: Dashboard['id']): Promise<DashboardWithSharedUsers> {
    const { data } = await http.get<DashboardWithSharedUsers>(`${DashboardsApiClient.API_ENDPOINT}/${e(dashboardId)}`);
    return data;
  }
 
  public static async findDashboards(): Promise<DashboardWithSharedUsers[]> {
    const { data } = await http.get<DashboardWithSharedUsers[]>(DashboardsApiClient.API_ENDPOINT);
    return data;
  }
 
  public static async findDashboardShareCandidates(dashboardId: Dashboard['id']): Promise<DashboardShareCandidate[]> {
    const { data } = await http.get<DashboardShareCandidate[]>(
      `${DashboardsApiClient.API_ENDPOINT}/${e(dashboardId)}/share-candidates`,
    );
    return data;
  }
 
  public static async getDashboardAsHome(): Promise<{ dashboardId: string | null }> {
    const { data } = await http.get<{ dashboardId: string | null }>(
      `${DashboardsApiClient.API_ENDPOINT}/home-dashboard`,
    );
    return data;
  }
 
  //////////////////////////
  // MUTATION METHODS
  //////////////////////////
 
  public static async createDashboard(createDashboardRequest: CreateDashboardRequest): Promise<Dashboard> {
    const { data } = await http.post<Dashboard>(DashboardsApiClient.API_ENDPOINT, createDashboardRequest);
    return data;
  }
 
  public static async patchDashboard(
    dashboardId: Dashboard['id'],
    patchDashboardRequest: PatchDashboardRequest,
  ): Promise<Dashboard> {
    const { data } = await http.patch<Dashboard>(
      `${DashboardsApiClient.API_ENDPOINT}/${e(dashboardId)}`,
      patchDashboardRequest,
    );
    return data;
  }
 
  public static async deleteDashboard(dashboardId: Dashboard['id']): Promise<void> {
    await http.delete<Dashboard>(`${DashboardsApiClient.API_ENDPOINT}/${e(dashboardId)}`);
  }
 
  public static async duplicateDashboard(
    dashboardId: Dashboard['id'],
    duplicateDashboardRequest: DuplicateDashboardRequest,
  ): Promise<Dashboard> {
    const { data } = await http.post<Dashboard>(
      `${DashboardsApiClient.API_ENDPOINT}/${e(dashboardId)}/duplicate`,
      duplicateDashboardRequest,
    );
    return data;
  }
 
  public static async updateDashboardShareCandidates(
    dashboardId: Dashboard['id'],
    patchDashboardRequest: PostDashboardSharedUsersRequest,
  ): Promise<number> {
    const { data } = await http.post<number>(
      `${DashboardsApiClient.API_ENDPOINT}/${e(dashboardId)}/shared-users`,
      patchDashboardRequest,
    );
    return data;
  }
 
  public static async removeDashboardUserSharedWith(
    dashboardId: Dashboard['id'],
    userToRemoveId: UserComputed['id'],
  ): Promise<UserComputed> {
    const { data } = await http.delete<UserComputed>(
      `${DashboardsApiClient.API_ENDPOINT}/${e(dashboardId)}/shared-users/${userToRemoveId}`,
    );
    return data;
  }
 
  public static async setDashboardAsHome(dashboardId: Dashboard['id']): Promise<void> {
    await http.post(`${DashboardsApiClient.API_ENDPOINT}/${e(dashboardId)}/set-as-home`);
  }
 
  public static async removeDashboardAsHome(): Promise<void> {
    await http.delete(`${DashboardsApiClient.API_ENDPOINT}/home-dashboard`);
  }
}