/** * 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 { return await pb.collection(Collections.Notices).getFullList({ 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("*", async () => { const notices = await getNotices(); callback(notices); }); return () => { safely(() => pb.collection(Collections.Notices).unsubscribe("*")); }; }