Files
kiosk/web/src/services/notices.ts
2025-10-24 19:25:46 +02:00

45 lines
1.3 KiB
TypeScript

/**
* Notices Service
*
* Handles all data operations for notices including fetching
* and real-time subscriptions for bulletin board display.
*
* Returns data directly from PocketBase with no transformations.
* Uses created field from RecordModel for posted date.
*/
import { pb, Collections } from "@/lib/pocketbase";
import type { Notice } from "@/types/notice";
import { safely } from "@/lib/result";
/**
* Fetches all notices from PocketBase
* Returns notices sorted by created date (newest first)
* Includes both active and expired notices for display
*
* @returns Array of all notice records sorted by creation date
*/
export async function getNotices(): Promise<Notice[]> {
return await pb.collection(Collections.Notices).getFullList<Notice>({
sort: "-created",
});
}
/**
* Subscribes to real-time notice updates
* Callback is invoked whenever any notice is created, updated, or deleted
*
* @param callback Function to call when notices change
* @returns Unsubscribe function to clean up the subscription
*/
export function subscribeToNotices(callback: (notices: Notice[]) => void): () => void {
pb.collection(Collections.Notices).subscribe<Notice>("*", async () => {
const notices = await getNotices();
callback(notices);
});
return () => {
safely(() => pb.collection(Collections.Notices).unsubscribe("*"));
};
}