/** * API модуль для уведомлений */ import apiClient from '@/lib/api-client'; export interface Notification { id: number; title: string; message: string; type?: 'info' | 'success' | 'warning' | 'error'; notification_type?: string; is_read: boolean; read_at?: string | null; created_at: string; action_url?: string; related_object_type?: string; related_object_id?: number; } export interface PaginatedResponse { count: number; next: string | null; previous: string | null; results: T[]; } /** * Получить список уведомлений */ export async function getNotifications(params?: { page?: number; page_size?: number; is_read?: boolean; }): Promise> { const response = await apiClient.get>('/notifications/', { params, }); return response.data; } /** * Получить непрочитанные уведомления и их количество */ export async function getUnread(): Promise<{ data: Notification[]; count: number }> { const response = await apiClient.get<{ success: boolean; data: Notification[]; count: number }>( '/notifications/unread/' ); const { data = [], count = 0 } = response.data ?? {}; return { data, count }; } /** * Отметить уведомление как прочитанное */ export async function markAsRead(id: number): Promise { await apiClient.post(`/notifications/${id}/mark_as_read/`); } /** * Отметить все уведомления как прочитанные */ export async function markAllAsRead(): Promise { await apiClient.post('/notifications/mark_all_as_read/'); } /** * Удалить уведомление */ export async function deleteNotification(id: number): Promise { await apiClient.delete(`/notifications/${id}/`); } export interface NotificationPreference { id?: number; enabled: boolean; email_enabled: boolean; telegram_enabled: boolean; in_app_enabled: boolean; type_preferences?: Record>; quiet_hours_enabled?: boolean; quiet_hours_start?: string; quiet_hours_end?: string; } /** * Получить настройки уведомлений */ export async function getNotificationPreferences(): Promise { try { const response = await apiClient.get('/notifications/preferences/me/'); const data = response.data?.data ?? response.data; return data; } catch { return null; } } /** * Обновить настройки уведомлений. * Возвращает обновлённые настройки с сервера; после PATCH очищает кэш GET, чтобы следующий запрос получил актуальные данные. */ export async function updateNotificationPreferences( preferences: Partial ): Promise { const response = await apiClient.patch<{ success?: boolean; data?: NotificationPreference }>( '/notifications/preferences/me/', preferences ); const updated = response.data?.data ?? response.data; if (updated && typeof updated === 'object') { apiClient.clearCache('preferences/me'); return updated as NotificationPreference; } return null; } export interface ParentChildNotificationSettings { id?: number; parent?: number; child?: number; child_id: number; child_name: string; enabled: boolean; type_settings: Record; created_at?: string; updated_at?: string; } /** * Получить настройки уведомлений родителя для конкретного ребенка */ export async function getParentChildNotificationSettingsForChild( childId: string ): Promise { const response = await apiClient.get( `/notifications/parent-child-settings/for_child/?child_id=${childId}` ); return response.data; } /** * Обновить настройки уведомлений родителя для конкретного ребенка */ export async function updateParentChildNotificationSettings( childId: string, settings: Partial> ): Promise { const response = await apiClient.patch( `/notifications/parent-child-settings/for_child/?child_id=${childId}`, settings ); return response.data; }