All files / src/utils notificationRouting.ts

89.47% Statements 17/19
88.88% Branches 8/9
100% Functions 2/2
94.11% Lines 16/17

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              2x     3x 3x   3x     3x 2x     1x     1x 1x           2x     5x     5x   2x 2x 1x     1x    
interface RoutableNotificationPayload {
  type?: string | null;
  event_id?: string | null;
  deep_link?: string | null;
  data?: Record<string, string> | null;
}
 
const UUIDISH_SEGMENT = '[0-9a-fA-F-]{20,}';
 
function normalizeEventRoute(value: string): string | null {
  const trimmed = value.trim();
  Iif (!trimmed) return null;
 
  const eventRouteMatch = trimmed.match(
    new RegExp(`(?:^|://|/)(?:events|event)/(${UUIDISH_SEGMENT})(?:$|[/?#])`),
  );
  if (eventRouteMatch?.[1]) {
    return `/event/${eventRouteMatch[1]}`;
  }
 
  const notificationRouteMatch = trimmed.match(
    /(?:^|:\/\/|\/)notifications(?:$|[/?#])/,
  );
  if (notificationRouteMatch) {
    return '/notifications';
  }
 
  return null;
}
 
export function resolveNotificationRoute(
  notification: RoutableNotificationPayload,
): string | null {
  const explicitRoute = notification.deep_link
    ? normalizeEventRoute(notification.deep_link)
    : null;
  if (explicitRoute) return explicitRoute;
 
  const eventID = notification.event_id ?? notification.data?.event_id;
  if (eventID) {
    return `/event/${eventID}`;
  }
 
  return null;
}