All files / src/api notifications.ts

26.31% Statements 5/19
0% Branches 0/6
0% Functions 0/5
26.31% Lines 5/19

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                                            1x                                 1x               1x             1x             1x      
import apiClient from './client';
 
export interface Notification {
  id: number;
  verb: string;
  read: boolean;
  timestamp: string;
  data: Record<string, any>;
  actor_type: string | null;
  actor_display: string | null;
  target_type: string | null;
  target_display: string | null;
  target_url: string | null;
}
 
export interface UnreadCountResponse {
  count: number;
}
 
/**
 * Get all notifications for the current user
 */
export const getNotifications = async (params?: { read?: boolean; limit?: number }): Promise<Notification[]> => {
  const queryParams = new URLSearchParams();
  if (params?.read !== undefined) {
    queryParams.append('read', String(params.read));
  }
  if (params?.limit !== undefined) {
    queryParams.append('limit', String(params.limit));
  }
  const query = queryParams.toString();
  const url = query ? `/notifications/?${query}` : '/notifications/';
  const response = await apiClient.get(url);
  return response.data;
};
 
/**
 * Get count of unread notifications
 */
export const getUnreadCount = async (): Promise<number> => {
  const response = await apiClient.get<UnreadCountResponse>('/notifications/unread_count/');
  return response.data.count;
};
 
/**
 * Mark a single notification as read
 */
export const markNotificationRead = async (id: number): Promise<void> => {
  await apiClient.post(`/notifications/${id}/mark_read/`);
};
 
/**
 * Mark all notifications as read
 */
export const markAllNotificationsRead = async (): Promise<void> => {
  await apiClient.post('/notifications/mark_all_read/');
};
 
/**
 * Delete all read notifications
 */
export const clearAllNotifications = async (): Promise<void> => {
  await apiClient.delete('/notifications/clear_all/');
};