All files notification.ts

87.5% Statements 7/8
66.66% Branches 2/3
80% Functions 4/5
87.5% Lines 7/8

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                                                                                      3x   1x 1x 1x   1x                   1x           1x                    
import type {
  ApiClient,
  BusinessId,
  CursorPageResponse,
} from "./api";
 
export interface InAppNotification {
  id: BusinessId;
  templateCode: string;
  referenceType: string;
  referenceNo: string;
  title: string;
  content: string;
  status: string;
  readAt: string | null;
  createdAt: string;
}
 
export interface NotificationUnreadCount {
  count: number;
}
 
export interface EmailPreference {
  userId: BusinessId;
  email: string | null;
  enabled: boolean;
  updatedAt: string;
}
 
export interface NotificationApi {
  notifications(
    cursor?: string,
    size?: number,
  ): Promise<CursorPageResponse<InAppNotification>>;
  unreadCount(): Promise<NotificationUnreadCount>;
  markRead(notificationId: BusinessId): Promise<void>;
  saveEmailPreference(
    email: string | null,
    enabled: boolean,
  ): Promise<EmailPreference>;
}
 
export function createNotificationApi(client: ApiClient): NotificationApi {
  return {
    notifications(cursor, size = 20) {
      const query = new URLSearchParams({ size: String(size) });
      Eif (cursor) {
        query.set("cursor", cursor);
      }
      return client.request<CursorPageResponse<InAppNotification>>(
        `/api/v1/notifications?${query}`,
      );
    },
    unreadCount() {
      return client.request<NotificationUnreadCount>(
        "/api/v1/notifications/unread-count",
      );
    },
    markRead(notificationId) {
      return client.request<void>(
        `/api/v1/notifications/${encodeURIComponent(notificationId)}/read`,
        { method: "POST" },
      );
    },
    saveEmailPreference(email, enabled) {
      return client.request<EmailPreference>(
        "/api/v1/notifications/email-preference",
        {
          method: "PUT",
          body: JSON.stringify({ email, enabled }),
        },
      );
    },
  };
}