43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import {
|
|
errorResult,
|
|
handleServiceError,
|
|
type ServiceCallOptions,
|
|
type ServiceResult,
|
|
shouldBubbleErrorToParent,
|
|
successResult,
|
|
} from '@/services/errorHandler'
|
|
import { getNotifications } from '@/api/generated/notifications/notifications'
|
|
import { clearStoredPushSubscriptionId, getStoredPushSubscriptionId, unsubscribeBrowserPushManager } from '@/lib/pwa'
|
|
|
|
const notificationsApi = getNotifications()
|
|
|
|
const UNSUBSCRIBE_PUSH = async (subscriptionId: string, options?: ServiceCallOptions): Promise<ServiceResult<{ deleted: boolean }>> => {
|
|
try {
|
|
await notificationsApi.notificationsControllerUnsubscribePush(subscriptionId)
|
|
|
|
return successResult({ deleted: true })
|
|
} catch (error) {
|
|
const normalizedError = handleServiceError(error, options)
|
|
|
|
if (shouldBubbleErrorToParent(options)) {
|
|
throw normalizedError
|
|
}
|
|
|
|
return errorResult(normalizedError)
|
|
}
|
|
}
|
|
|
|
/** Best-effort cleanup on admin logout — no subscribe flow in this app. */
|
|
export const unsubscribeCurrentDevicePush = async (): Promise<void> => {
|
|
const subscriptionId = getStoredPushSubscriptionId()
|
|
|
|
try {
|
|
if (subscriptionId) {
|
|
await UNSUBSCRIBE_PUSH(subscriptionId, { errorMode: 'silent' })
|
|
}
|
|
} finally {
|
|
clearStoredPushSubscriptionId()
|
|
await unsubscribeBrowserPushManager().catch(() => undefined)
|
|
}
|
|
}
|