import { api } from '@services/api/axios-instance'; import type { NotificationChannel, NotificationTemplate, NotificationTemplateCreateInput, NotificationPreference, NotificationPreferenceUpdateInput, Notification, NotificationCreateInput, NotificationStatus, InAppNotification, InAppNotificationCreateInput, InAppNotificationsFilters, InAppNotificationsResponse, } from '../types'; const NOTIFICATIONS_BASE = '/api/v1/notifications'; // ============================================================================ // Channels API // ============================================================================ export const channelsApi = { getAll: async (): Promise => { const response = await api.get(`${NOTIFICATIONS_BASE}/channels`); return response.data; }, getByCode: async (code: string): Promise => { const response = await api.get(`${NOTIFICATIONS_BASE}/channels/${code}`); return response.data; }, }; // ============================================================================ // Templates API // ============================================================================ export const templatesApi = { getAll: async (): Promise => { const response = await api.get(`${NOTIFICATIONS_BASE}/templates`); return response.data; }, getByCode: async (code: string): Promise => { const response = await api.get(`${NOTIFICATIONS_BASE}/templates/${code}`); return response.data; }, create: async (data: NotificationTemplateCreateInput): Promise => { const response = await api.post(`${NOTIFICATIONS_BASE}/templates`, data); return response.data; }, update: async (id: string, data: Partial): Promise => { const response = await api.patch(`${NOTIFICATIONS_BASE}/templates/${id}`, data); return response.data; }, delete: async (id: string): Promise => { await api.delete(`${NOTIFICATIONS_BASE}/templates/${id}`); }, }; // ============================================================================ // Preferences API // ============================================================================ export const preferencesApi = { get: async (): Promise => { const response = await api.get(`${NOTIFICATIONS_BASE}/preferences`); return response.data; }, update: async (data: NotificationPreferenceUpdateInput): Promise => { const response = await api.patch(`${NOTIFICATIONS_BASE}/preferences`, data); return response.data; }, }; // ============================================================================ // Notifications API // ============================================================================ export const notificationsApi = { create: async (data: NotificationCreateInput): Promise => { const response = await api.post(`${NOTIFICATIONS_BASE}`, data); return response.data; }, getPending: async (limit = 50): Promise => { const response = await api.get(`${NOTIFICATIONS_BASE}/pending?limit=${limit}`); return response.data; }, updateStatus: async (id: string, status: NotificationStatus, errorMessage?: string): Promise => { const response = await api.patch(`${NOTIFICATIONS_BASE}/${id}/status`, { status, errorMessage, }); return response.data; }, }; // ============================================================================ // In-App Notifications API // ============================================================================ export const inAppApi = { getAll: async (filters: InAppNotificationsFilters = {}): Promise => { const params = new URLSearchParams(); if (filters.includeRead !== undefined) params.append('include_read', String(filters.includeRead)); if (filters.category) params.append('category', filters.category); if (filters.page) params.append('page', String(filters.page)); if (filters.limit) params.append('limit', String(filters.limit)); const response = await api.get(`${NOTIFICATIONS_BASE}/in-app?${params}`); return response.data; }, getUnreadCount: async (): Promise => { const response = await api.get<{ count: number }>(`${NOTIFICATIONS_BASE}/in-app/unread-count`); return response.data.count; }, markAsRead: async (id: string): Promise => { const response = await api.post(`${NOTIFICATIONS_BASE}/in-app/${id}/read`); return response.data; }, markAllAsRead: async (): Promise => { await api.post(`${NOTIFICATIONS_BASE}/in-app/read-all`); }, create: async (data: InAppNotificationCreateInput): Promise => { const response = await api.post(`${NOTIFICATIONS_BASE}/in-app`, data); return response.data; }, archive: async (id: string): Promise => { const response = await api.post(`${NOTIFICATIONS_BASE}/in-app/${id}/archive`); return response.data; }, delete: async (id: string): Promise => { await api.delete(`${NOTIFICATIONS_BASE}/in-app/${id}`); }, };