All files / libs/tenants/users/profile/state/src/lib/api-client user-profile.api-client.ts

81.57% Statements 62/76
100% Branches 7/7
50% Functions 5/10
81.57% Lines 62/76

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 771x 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 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x     1x 1x     1x 1x 1x   1x 1x  
import { AxiosError } from 'axios';
import { type SetRequired } from 'type-fest';
 
import { http } from '@amalia/core/http/client';
import { assert } from '@amalia/ext/typescript';
import { type EmploymentHistoryEvent } from '@amalia/lti/types';
import {
  isInviteUsersAggregatedError,
  type InviteUsersRequest,
  type UpdateUserProfileRequest,
  type UserProfile,
} from '@amalia/tenants/users/profile/types';
 
export abstract class UserProfileApiClient {
  public static async getAuthorizedProfiles(): Promise<UserProfile[]> {
    const { data } = await http.get<UserProfile[]>('/users/profile');

    return data;
  }
 
  public static async getUserProfile(userId: UserProfile['id']): Promise<UserProfile> {
    const encodedUserId = encodeURI(userId);
    const { data: profile } = await http.get<UserProfile>(`/users/profile/${encodedUserId}`);
 
    return profile;
  }
 
  public static async getEditionHistory(userId: UserProfile['id']): Promise<EmploymentHistoryEvent[]> {
    const encodedUserId = encodeURI(userId);
    const { data } = await http.get<EmploymentHistoryEvent[]>(`/users/profile/${encodedUserId}/edition-history`);

    return data;
  }
 
  public static async updateUserInfo(user: UpdateUserProfileRequest): Promise<UserProfile> {
    const { data } = await http.put<UserProfile>('/users/profile', user);
 
    return data;
  }
 
  public static async toggleUserDeactivation(userId: UserProfile['id']): Promise<UserProfile> {
    const encodedUserId = encodeURI(userId);
    const { data } = await http.post<UserProfile>(`/users/profile/${encodedUserId}/toggle-deactivation`);
 
    return data;
  }
 
  public static async randomizeUserAvatar() {
    const { data: pictureURL } = await http.patch<unknown>('/users/avatar/randomize');
 
    assert(pictureURL && typeof pictureURL === 'string', 'No avatar URL is returned from the server');
 
    return pictureURL;
  }
 
  public static async uploadAvatar(avatarBase64: string) {
    const { data: pictureURL } = await http.patch<unknown>('/users/avatar', { avatarBase64 });
 
    assert(pictureURL && typeof pictureURL === 'string', 'No avatar URL is returned from the server');
 
    return pictureURL;
  }
 
  public static async inviteUser(userId: string) {
    return UserProfileApiClient.inviteUsers([userId]);
  }
 
  public static async inviteUsers(userIds: string[]) {
    await http.post('/users/profile/invitations', userIds satisfies InviteUsersRequest);
  }
 
  public static isBulkInvitationError = (
    error: unknown,
  ): error is SetRequired<AxiosError<{ errors: { id: string; error: Error }[]; total: number }>, 'response'> =>
    error instanceof AxiosError && isInviteUsersAggregatedError(error.response?.data);
}