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

34.37% Statements 11/32
100% Branches 0/0
0% Functions 0/3
34.37% Lines 11/32

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 331x 1x 1x 1x 1x 1x                                 1x 1x       1x 1x     1x  
import { http } from '@amalia/core/http/client';
import { type Notification } from '@amalia/core/types';
import { type PaginatedResponse } from '@amalia/core-http-types';
 
export class NotificationsApiClient {
  public static async fetchNotifications(
    page: number,
    pageSize: number = 24,
  ): Promise<{
    paginatedNotifications: PaginatedResponse<Notification>;
    unreadCount: number;
  }> {
    const {
      data: { unreadCount, ...rest },
    } = await http.get<PaginatedResponse<Notification> & { unreadCount: number }>('/notifications', {
      params: { page, limit: pageSize },
    });
    return {
      paginatedNotifications: rest,
      unreadCount,
    };
  }
 
  public static async fetchUnreadCount(): Promise<number> {
    // Cannot use limit: 0 because the plugin returns all notifications instead.
    return (await NotificationsApiClient.fetchNotifications(1, 1)).unreadCount;
  }
 
  public static async markAsRead(lastCheck: Date): Promise<void> {
    await http.post('/notifications/read-notifications', { lastCheck });
  }
}