import apiClient from './api/client' export interface Notification { id: string title: string message: string type: 'system' | 'user' | 'alert' | 'invoice' | 'payment' recipient: string status: 'sent' | 'delivered' | 'read' | 'unread' isRead: boolean createdAt: string sentAt?: string readAt?: string } export interface NotificationSettings { emailNotifications: boolean pushNotifications: boolean invoiceReminders: boolean paymentAlerts: boolean systemUpdates: boolean } class NotificationService { /** * Get all notifications for current user */ async getNotifications(params?: { type?: string status?: string search?: string }): Promise { const response = await apiClient.get('/notifications', { params, }) return response.data } /** * Get unread notification count */ async getUnreadCount(): Promise { const response = await apiClient.get<{ count: number }>('/notifications/unread-count') return response.data.count } /** * Mark notification as read */ async markAsRead(id: string): Promise { await apiClient.post(`/notifications/${id}/read`) } /** * Mark all notifications as read */ async markAllAsRead(): Promise { await apiClient.post('/notifications/read-all') } /** * Broadcast push, SMS, and/or email (Super Admin & Admin only) */ async sendBroadcast(data: { title: string message: string channels: ('push' | 'sms' | 'email')[] audience?: 'all_end_users' | 'system_users_only' | 'everyone_with_access' }): Promise<{ id: string }> { const response = await apiClient.post<{ id: string }>( '/admin/notifications/broadcast', data, ) return response.data } /** * Send notification (ADMIN only) */ async sendNotification(data: { title: string message: string type: string recipient?: string recipientType?: 'user' | 'all' }): Promise { const response = await apiClient.post('/notifications/send', data) return response.data } /** * Subscribe to push notifications */ async subscribeToPush(subscription: PushSubscription): Promise { await apiClient.post('/notifications/subscribe', subscription) } /** * Unsubscribe from push notifications */ async unsubscribeFromPush(endpoint: string): Promise { await apiClient.delete(`/notifications/unsubscribe/${encodeURIComponent(endpoint)}`) } /** * Get notification settings */ async getSettings(): Promise { const response = await apiClient.get('/notifications/settings') return response.data } /** * Update notification settings */ async updateSettings(settings: Partial): Promise { const response = await apiClient.put('/notifications/settings', settings) return response.data } /** * Send invoice reminder */ async sendInvoiceReminder(invoiceId: string): Promise { await apiClient.post(`/notifications/invoice/${invoiceId}/reminder`) } /** * Export notifications (creates CSV from current data) */ async exportNotifications(notifications: Notification[]): Promise { const csvContent = [ ['ID', 'Title', 'Message', 'Type', 'Status', 'Created Date', 'Read Date'], ...notifications.map(n => [ n.id, n.title, n.message, n.type, n.status, n.createdAt, n.readAt || '-' ]) ].map(row => row.join(',')).join('\n') return new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }) } } export const notificationService = new NotificationService()