Version 1.0 squash

This commit is contained in:
Kenan Alić
2025-10-21 18:45:02 +02:00
parent 7536290f23
commit d1784542d3
30 changed files with 2534 additions and 15 deletions

View File

@@ -0,0 +1,53 @@
/**
* Companies Service
*
* Handles all data operations for companies including fetching,
* updating presence status, and real-time subscriptions.
*
* Returns data directly from PocketBase with no transformations.
* File URLs are resolved at display time using getFileUrl() helper.
*/
import { pb, Collections } from "@/lib/pocketbase";
import type { Company } from "@/types/company";
import { safely } from "@/lib/result";
/**
* Fetches all companies from PocketBase
* Returns companies sorted alphabetically by short name
*
* @returns Array of all company records sorted by name
*/
export async function getCompanies(): Promise<Company[]> {
return await pb.collection(Collections.Companies).getFullList<Company>({
sort: "short_name",
});
}
/**
* Updates a company's presence status (present/absent)
* Used when tenants mark themselves as present or absent at the kiosk
*/
export async function updateCompanyStatus(companyId: string, status: "present" | "absent"): Promise<Company> {
return await pb.collection(Collections.Companies).update<Company>(companyId, {
status,
});
}
/**
* Subscribes to real-time company updates
* Callback is invoked whenever any company record is created, updated, or deleted
*
* @param callback Function to call when companies change
* @returns Unsubscribe function to clean up the subscription
*/
export function subscribeToCompanies(callback: (companies: Company[]) => void): () => void {
pb.collection(Collections.Companies).subscribe<Company>("*", async () => {
const companies = await getCompanies();
callback(companies);
});
return () => {
safely(() => pb.collection(Collections.Companies).unsubscribe("*"));
};
}