All files / web/src/lib/notifications/channels web-push-channel.ts

0% Statements 0/67
0% Branches 0/1
0% Functions 0/1
0% Lines 0/67

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                                                                                                                                       
import type {
  NotificationChannel,
  DeliveryTarget,
  NotificationEvent,
  DeliveryResult,
} from '../types'
import { formatNotificationContent } from '../types'
import { sendWebPush, sendWebPushToUser } from '../web-push'

/**
 * Web Push notification channel implementation.
 *
 * For authenticated users: looks up all registered push endpoints from
 * user_push_subscriptions and sends to each.
 *
 * For legacy/anonymous subscribers: uses the push subscription stored
 * directly on the delivery target (from the practice subscription record).
 */
export const webPushChannel: NotificationChannel = {
  name: 'webPush',

  canDeliver(target: DeliveryTarget): boolean {
    return target.channels.push && (!!target.userId || !!target.subscriptionPushEndpoint)
  },

  async deliver(target: DeliveryTarget, event: NotificationEvent): Promise<DeliveryResult> {
    const content = formatNotificationContent(event)

    const payload = {
      title: content.title,
      body: content.body,
      icon: content.icon,
      data: { url: content.url },
    }

    // Legacy path: use the push subscription from the subscription record
    if (target.subscriptionPushEndpoint) {
      try {
        const result = await sendWebPush(target.subscriptionPushEndpoint, payload)
        if (result.success) return { success: true }
        return {
          success: false,
          error: `Push endpoint returned ${result.statusCode}`,
          shouldDisable: true,
        }
      } catch (err) {
        const message = err instanceof Error ? err.message : String(err)
        return { success: false, error: `webPush: ${message}` }
      }
    }

    // User-level path: send to all registered push endpoints
    try {
      const result = await sendWebPushToUser(target.userId, payload)
      if (result.sent === 0 && result.total === 0) {
        return { success: false, error: 'No push subscriptions registered for user' }
      }
      if (result.sent === 0) {
        return { success: false, error: `All ${result.total} push endpoints failed` }
      }
      return { success: true }
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err)
      return { success: false, error: `webPush: ${message}` }
    }
  },
}