All files / libs/tenants/assignments/teams/state/src/lib/api-client team-assignments.api-client.ts

88.31% Statements 68/77
83.33% Branches 5/6
83.33% Functions 5/6
88.31% Lines 68/77

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 781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { type SetRequired } from 'type-fest';
 
import { http } from '@amalia/core/http/client';
import {
  type BulkImportTeamAssignmentsRequest,
  type CreateTeamAssignmentRequest,
  type CreateTeamAssignmentsRequest,
  type DeleteTeamAssignmentsRequest,
  type TeamAssignment,
  type TeamAssignmentsValidateResponse,
  type TeamRole,
  type UpdateTeamAssignmentRequest,
  type UpdateTeamAssignmentsRequest,
} from '@amalia/tenants/assignments/teams/types';
 
export class TeamAssignmentsApiClient {
  public static async getTeamAssignments<TRelations extends ('team' | 'user')[] = []>({
    teamsIds,
    teamRole,
    relations = [] as unknown as TRelations,
  }: {
    teamsIds: string[];
    teamRole?: TeamRole;
    relations?: TRelations;
  }): Promise<SetRequired<TeamAssignment, TRelations[number]>[]> {
    const { data } = await http.get<SetRequired<TeamAssignment, TRelations[number]>[]>('/team_assignments', {
      params: {
        teamId: teamsIds,
        teamRole: teamRole || undefined,
        relations: relations.join(',') || undefined,
      },
    });
 
    return data;
  }
 
  public static async createTeamAssignments(
    createTeamAssignmentRequests: CreateTeamAssignmentRequest[],
  ): Promise<TeamAssignment[]> {
    const { data } = await http.post<TeamAssignment[]>('/team_assignments', {
      teamAssignments: createTeamAssignmentRequests,
    } satisfies CreateTeamAssignmentsRequest);
 
    return data;
  }
 
  public static async createTeamAssignment(
    createTeamAssignmentRequest: CreateTeamAssignmentRequest,
  ): Promise<TeamAssignment> {
    return (await this.createTeamAssignments([createTeamAssignmentRequest]))[0];
  }
 
  public static async importBulkTeamAssignments({
    rowsToImport,
    dryRun = true,
  }: BulkImportTeamAssignmentsRequest): Promise<TeamAssignmentsValidateResponse> {
    const { data } = await http.post<TeamAssignmentsValidateResponse>('/team_assignments/bulk-import', {
      rowsToImport,
      dryRun,
    });
    return data;
  }
 
  public static async updateTeamAssignments(
    updateTeamAssignmentRequests: UpdateTeamAssignmentRequest[],
  ): Promise<TeamAssignment[]> {
    const { data } = await http.patch<TeamAssignment[]>('/team_assignments', {
      teamAssignments: updateTeamAssignmentRequests,
    } satisfies UpdateTeamAssignmentsRequest);
 
    return data;
  }
 
  public static async removeTeamAssignments(teamAssignmentIds: TeamAssignment['id'][]) {
    await http.delete('/team_assignments', { data: { teamAssignmentIds } satisfies DeleteTeamAssignmentsRequest });
  }
}