xpeditis2.0/apps/frontend/src/lib/api/notifications.ts
2025-11-04 07:30:15 +01:00

99 lines
2.8 KiB
TypeScript

/**
* Notifications API
*
* Endpoints for managing user notifications
*/
import { get, post, patch, del } from './client';
import type {
NotificationResponse,
NotificationListResponse,
CreateNotificationRequest,
UpdateNotificationPreferencesRequest,
NotificationPreferencesResponse,
SuccessResponse,
} from '@/types/api';
/**
* List user notifications with pagination
* GET /api/v1/notifications?page=1&limit=20&isRead=false&type=BOOKING_CONFIRMED
*/
export async function listNotifications(params?: {
page?: number;
limit?: number;
isRead?: boolean;
type?: string;
}): Promise<NotificationListResponse> {
const queryParams = new URLSearchParams();
if (params?.page) queryParams.append('page', params.page.toString());
if (params?.limit) queryParams.append('limit', params.limit.toString());
if (params?.isRead !== undefined) queryParams.append('isRead', params.isRead.toString());
if (params?.type) queryParams.append('type', params.type);
const queryString = queryParams.toString();
return get<NotificationListResponse>(
`/api/v1/notifications${queryString ? `?${queryString}` : ''}`
);
}
/**
* Get notification by ID
* GET /api/v1/notifications/:id
*/
export async function getNotification(id: string): Promise<NotificationResponse> {
return get<NotificationResponse>(`/api/v1/notifications/${id}`);
}
/**
* Create notification (admin only)
* POST /api/v1/notifications
* Requires: ADMIN role
*/
export async function createNotification(
data: CreateNotificationRequest
): Promise<NotificationResponse> {
return post<NotificationResponse>('/api/v1/notifications', data);
}
/**
* Mark notification as read
* PATCH /api/v1/notifications/:id/read
*/
export async function markNotificationAsRead(id: string): Promise<SuccessResponse> {
return patch<SuccessResponse>(`/api/v1/notifications/${id}/read`);
}
/**
* Mark all notifications as read
* PATCH /api/v1/notifications/read-all
*/
export async function markAllNotificationsAsRead(): Promise<SuccessResponse> {
return patch<SuccessResponse>('/api/v1/notifications/read-all');
}
/**
* Delete notification
* DELETE /api/v1/notifications/:id
*/
export async function deleteNotification(id: string): Promise<SuccessResponse> {
return del<SuccessResponse>(`/api/v1/notifications/${id}`);
}
/**
* Get notification preferences
* GET /api/v1/notifications/preferences
*/
export async function getNotificationPreferences(): Promise<NotificationPreferencesResponse> {
return get<NotificationPreferencesResponse>('/api/v1/notifications/preferences');
}
/**
* Update notification preferences
* PATCH /api/v1/notifications/preferences
*/
export async function updateNotificationPreferences(
data: UpdateNotificationPreferencesRequest
): Promise<NotificationPreferencesResponse> {
return patch<NotificationPreferencesResponse>('/api/v1/notifications/preferences', data);
}