// --------------------------------------------------------------------------- // Notifications API client (client-side) // --------------------------------------------------------------------------- import { apiFetch } from './client'; // --------------------------------------------------------------------------- // Request / response shapes // TODO: import from @/lib/types once the shared types package is finalised // --------------------------------------------------------------------------- export interface NotificationResponse { id: number; title: string; body: string; notification_type: string; is_read: boolean; data?: Record; created_at: string; } export interface NotificationListResponse { count: number; results: NotificationResponse[]; } export interface UnreadCountResponse { unread_count: number; } export interface NotificationPreferencesResponse { task_reminders: boolean; task_completions: boolean; residence_updates: boolean; share_notifications: boolean; marketing: boolean; } export interface UpdatePreferencesRequest { task_reminders?: boolean; task_completions?: boolean; residence_updates?: boolean; share_notifications?: boolean; marketing?: boolean; } export interface RegisterDeviceRequest { registration_id: string; platform: 'ios' | 'android' | 'web'; device_name?: string; } export interface DeviceResponse { id: number; registration_id: string; platform: string; device_name: string; is_active: boolean; created_at: string; } export interface UnregisterDeviceRequest { registration_id: string; platform?: string; } export interface MessageResponse { message: string; } // --------------------------------------------------------------------------- // API functions // --------------------------------------------------------------------------- /** List notifications with optional pagination. */ export function listNotifications( limit?: number, offset?: number, ): Promise { const params = new URLSearchParams(); if (limit != null) params.set('limit', String(limit)); if (offset != null) params.set('offset', String(offset)); const qs = params.toString(); return apiFetch( `/notifications/${qs ? `?${qs}` : ''}`, ); } /** Get unread notification count. */ export function getUnreadCount(): Promise { return apiFetch('/notifications/unread-count/'); } /** Mark a single notification as read. */ export function markAsRead(id: number): Promise { return apiFetch(`/notifications/${id}/read/`, { method: 'POST', }); } /** Mark all notifications as read. */ export function markAllAsRead(): Promise { return apiFetch('/notifications/mark-all-read/', { method: 'POST', }); } /** Get notification preferences. */ export function getPreferences(): Promise { return apiFetch( '/notifications/preferences/', ); } /** Update notification preferences. */ export function updatePreferences( data: UpdatePreferencesRequest, ): Promise { return apiFetch( '/notifications/preferences/', { method: 'PUT', body: JSON.stringify(data), }, ); } /** Register a push notification device. */ export function registerDevice( data: RegisterDeviceRequest, ): Promise { return apiFetch('/notifications/devices/', { method: 'POST', body: JSON.stringify(data), }); } /** List registered devices. */ export function listDevices(): Promise { return apiFetch('/notifications/devices/'); } /** Unregister a push notification device by registration ID. */ export function unregisterDevice( data: UnregisterDeviceRequest, ): Promise { return apiFetch('/notifications/devices/unregister/', { method: 'POST', body: JSON.stringify(data), }); } /** Delete a device by ID. */ export function deleteDevice( id: number, platform?: string, ): Promise { const qs = platform ? `?platform=${platform}` : ''; return apiFetch(`/notifications/devices/${id}/${qs}`, { method: 'DELETE', }); }