Version 1.0 squash
This commit is contained in:
53
web/src/services/companies.ts
Normal file
53
web/src/services/companies.ts
Normal 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("*"));
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user