All files / apps/host/src/services/notification sync.ts

100% Statements 20/20
83.33% Branches 10/12
100% Functions 2/2
100% Lines 18/18

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              3x 3x 2x 2x   2x 3x   3x 2x 2x   2x 2x       2x   1x           1x 1x 1x 2x 2x            
import notifee from '@notifee/react-native';
 
// Identify new tray notifications
export async function syncTrayNotifications(
  isProcessed: (id: string) => boolean,
  markAsProcessed: (id: string) => void,
): Promise<{ hasNew: boolean; ticketIds: string[] }> {
  try {
    const displayed = await notifee.getDisplayedNotifications();
    let hasNew = false;
    const ticketIds: string[] = [];
 
    for (const item of displayed) {
      const id = item.id || (item.notification?.data?._messageId as string);
 
      if (id && !isProcessed(id)) {
        hasNew = true;
        markAsProcessed(id);
 
        const ticketId = item.notification?.data?.ticketId as string;
        if (ticketId) ticketIds.push(ticketId);
      }
    }
 
    return { hasNew, ticketIds };
  } catch {
    return { hasNew: false, ticketIds: [] };
  }
}
 
// Mark tray notifications as processed
export async function markTrayAsProcessed(markAsProcessed: (id: string) => void): Promise<void> {
  try {
    const displayed = await notifee.getDisplayedNotifications();
    for (const item of displayed) {
      const id = item.id || (item.notification?.data?._messageId as string);
      Eif (id) markAsProcessed(id);
    }
  } catch {
    // Ignore errors
  }
}