All files / libs/payout-definition/state/src/lib/variables variables.api-client.ts

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

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                                                                                                                                                                                                     
import {
  type CreateVariableRequest,
  type PatchVariableRequest,
  type Variable,
  type VariableType,
  type VariableWithPlan,
} from '@amalia/amalia-lang/tokens/types';
import { http } from '@amalia/core/http/client';

export class VariablesApiClient {
  public static async list(
    types?: readonly VariableType[],
    userIds?: string[],
    planIds?: string[],
  ): Promise<Variable[]> {
    const qs = [
      ...(types ? types.map((t) => `types=${t}`) : []),
      ...(userIds ? userIds.map((u) => `userIds=${u}`) : []),
      ...(planIds ? planIds.map((u) => `planIds=${u}`) : []),
    ]
      .filter(Boolean)
      .join('&');

    const { data } = await http.get<Variable[]>(`/variables/?${qs}`);
    return data;
  }

  public static async get(variableId: string): Promise<Variable> {
    const { data } = await http.get<Variable>(`/variables/${variableId}`);
    return data;
  }

  public static async create({
    variable,
    createdFrom,
  }: {
    variable: CreateVariableRequest;
    createdFrom?: { ruleId?: string; planId?: string; activeFilterId?: string };
  }): Promise<Variable> {
    const { data } = await http.post<Variable>('/variables', variable, {
      params: {
        planId: createdFrom?.planId,
        ruleId: createdFrom?.ruleId,
        activeFilterId: createdFrom?.activeFilterId,
      },
    });
    return data;
  }

  public static async patch({
    id,
    variable,
    updatedFrom,
  }: {
    id: string;
    variable: PatchVariableRequest;
    updatedFrom?: { planId?: string };
  }): Promise<Variable> {
    const { data } = await http.patch<Variable>(`/variables/${id}`, variable, {
      params: {
        planId: updatedFrom?.planId,
      },
    });
    return data;
  }

  public static async delete(variableId: string, planId?: string): Promise<Variable> {
    const { data } = await http.delete<Variable>(`/variables/${variableId}`, {
      params: {
        planId,
      },
    });
    return data;
  }

  public static async duplicateInNewContext(
    variablesId: string[],
    from: { planId: string; ruleId: string; activeFilterId?: string },
  ): Promise<{ isErrorWhileDuplicating: boolean }> {
    const { data } = await http.post<{ isErrorWhileDuplicating: boolean }>(
      '/variables/duplicateInContext',
      variablesId,
      {
        params: {
          planId: from.planId,
          ruleId: from.ruleId,
          activeFilterId: from.activeFilterId,
        },
      },
    );
    return data;
  }

  public static async getByObjectId(customDefinitionObjectId: string) {
    const { data } = await http.get<VariableWithPlan[]>(`/variables/customObject/${customDefinitionObjectId}`);
    return data;
  }
}