Files
dashboard/frontend/src/services/notifications.ts
2026-03-12 16:42:21 +01:00

40 lines
1.2 KiB
TypeScript

import { api } from './api';
import type { Notification } from '../types/notification.types';
async function unwrap<T>(
promise: ReturnType<typeof api.get<{ success: boolean; data: T }>>
): Promise<T> {
const response = await promise;
if (response.data?.data === undefined || response.data?.data === null) {
throw new Error('Invalid API response');
}
return response.data.data;
}
export const notificationsApi = {
async getNotifications(): Promise<Notification[]> {
return unwrap(api.get<{ success: boolean; data: Notification[] }>('/api/notifications'));
},
async getUnreadCount(): Promise<number> {
const data = await unwrap(api.get<{ success: boolean; data: { count: number } }>('/api/notifications/count'));
return data.count;
},
async markRead(id: string): Promise<void> {
await api.patch(`/api/notifications/${id}/read`);
},
async markAllRead(): Promise<void> {
await api.post('/api/notifications/mark-all-read');
},
async dismissByType(quellTyp: string): Promise<void> {
await api.post('/api/notifications/dismiss-by-type', { quellTyp });
},
async deleteAllRead(): Promise<void> {
await api.delete('/api/notifications/read');
},
};