155 lines
4.7 KiB
TypeScript
155 lines
4.7 KiB
TypeScript
/**
|
||
* 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<T> {
|
||
count: number;
|
||
next: string | null;
|
||
previous: string | null;
|
||
results: T[];
|
||
}
|
||
|
||
/**
|
||
* Получить список уведомлений
|
||
*/
|
||
export async function getNotifications(params?: {
|
||
page?: number;
|
||
page_size?: number;
|
||
is_read?: boolean;
|
||
}): Promise<PaginatedResponse<Notification>> {
|
||
const response = await apiClient.get<PaginatedResponse<Notification>>('/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<void> {
|
||
await apiClient.post(`/notifications/${id}/mark_as_read/`);
|
||
}
|
||
|
||
/**
|
||
* Отметить все уведомления как прочитанные
|
||
*/
|
||
export async function markAllAsRead(): Promise<void> {
|
||
await apiClient.post('/notifications/mark_all_as_read/');
|
||
}
|
||
|
||
/**
|
||
* Удалить уведомление
|
||
*/
|
||
export async function deleteNotification(id: number): Promise<void> {
|
||
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<string, Record<string, boolean>>;
|
||
quiet_hours_enabled?: boolean;
|
||
quiet_hours_start?: string;
|
||
quiet_hours_end?: string;
|
||
}
|
||
|
||
/**
|
||
* Получить настройки уведомлений
|
||
*/
|
||
export async function getNotificationPreferences(): Promise<NotificationPreference | null> {
|
||
try {
|
||
const response = await apiClient.get<any>('/notifications/preferences/me/');
|
||
const data = response.data?.data ?? response.data;
|
||
return data;
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Обновить настройки уведомлений.
|
||
* Возвращает обновлённые настройки с сервера; после PATCH очищает кэш GET, чтобы следующий запрос получил актуальные данные.
|
||
*/
|
||
export async function updateNotificationPreferences(
|
||
preferences: Partial<NotificationPreference>
|
||
): Promise<NotificationPreference | null> {
|
||
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<string, boolean>;
|
||
created_at?: string;
|
||
updated_at?: string;
|
||
}
|
||
|
||
/**
|
||
* Получить настройки уведомлений родителя для конкретного ребенка
|
||
*/
|
||
export async function getParentChildNotificationSettingsForChild(
|
||
childId: string
|
||
): Promise<ParentChildNotificationSettings> {
|
||
const response = await apiClient.get<ParentChildNotificationSettings>(
|
||
`/notifications/parent-child-settings/for_child/?child_id=${childId}`
|
||
);
|
||
return response.data;
|
||
}
|
||
|
||
/**
|
||
* Обновить настройки уведомлений родителя для конкретного ребенка
|
||
*/
|
||
export async function updateParentChildNotificationSettings(
|
||
childId: string,
|
||
settings: Partial<Pick<ParentChildNotificationSettings, 'enabled' | 'type_settings'>>
|
||
): Promise<ParentChildNotificationSettings> {
|
||
const response = await apiClient.patch<ParentChildNotificationSettings>(
|
||
`/notifications/parent-child-settings/for_child/?child_id=${childId}`,
|
||
settings
|
||
);
|
||
return response.data;
|
||
}
|