import PocketBase from 'pocketbase' import { POCKETBASE_URL, PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD } from './pocketbase-config.js' const COLLECTION = 'travellers' const VACCINES_COLLECTION = 'vaccines' let pb = null let authPromise = null function getClient() { if (!pb) { pb = new PocketBase(POCKETBASE_URL) // Main process may receive overlapping IPC calls (e.g. React Strict Mode). // Default auto-cancel would abort duplicate in-flight PocketBase requests. pb.autoCancellation(false) } return pb } function parseEntry(r) { return { id: r.id, emrID: r.emr_id ?? '', name: r.name ?? '', location: r.location ?? '', countryISO: r.country_iso ?? '', countriesISO: r.countries_iso ?? [], status: r.status ?? '', departure: r.departure ?? '', arrival: r.arrival ?? '', created: r.created, updated: r.updated, } } export async function ensureAuth() { const client = getClient() if (client.authStore.isValid) return if (authPromise) { await authPromise return } // PocketBase v0.23+ removed /api/admins/*; superusers authenticate as a collection. authPromise = client .collection('_superusers') .authWithPassword(PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD) try { await authPromise } catch (err) { authPromise = null throw err } } export async function fetchEntries() { await ensureAuth() const list = await getClient().collection(COLLECTION).getFullList({ sort: '-created' }) return list.map(parseEntry) } export async function createEntry(trip) { await ensureAuth() const payload = { name: trip.name, location: trip.location, countries_iso: trip.countriesISO, departure: trip.departure, arrival: trip.arrival, } if (trip.emrID !== undefined && trip.emrID !== null && trip.emrID !== '') { const emrNum = Number(trip.emrID) payload.emr_id = !isNaN(emrNum) ? emrNum : trip.emrID } const record = await getClient().collection(COLLECTION).create(payload) return parseEntry(record) } export async function updateEntry(id, trip) { await ensureAuth() const payload = { name: trip.name, location: trip.location, countries_iso: trip.countriesISO, departure: trip.departure, arrival: trip.arrival, } if (trip.emrID !== undefined && trip.emrID !== null && trip.emrID !== '') { const emrNum = Number(trip.emrID) payload.emr_id = !isNaN(emrNum) ? emrNum : trip.emrID } else if (trip.emrID === '') { payload.emr_id = null } const record = await getClient().collection(COLLECTION).update(id, payload) return parseEntry(record) } export async function deleteEntry(id) { await ensureAuth() await getClient().collection(COLLECTION).delete(id) } /** * Fetches all records from the vaccines collection. * Returns raw records with all fields preserved. */ export async function fetchVaccines() { await ensureAuth() const list = await getClient().collection(VACCINES_COLLECTION).getFullList({ sort: 'name' }) return list.map((r) => ({ id: r.id, name: r.name ?? '', vaccine_type: r.vaccine_type ?? '', total_doses: r.total_doses ?? 1, dose_intervals: r.dose_intervals ?? [], min_days_before_travel: r.min_days_before_travel ?? 0, allows_grace_period: r.allows_grace_period ?? false, has_accelerated_schedule: r.has_accelerated_schedule ?? false, requires_icvp_certificate: r.requires_icvp_certificate ?? false, created: r.created, updated: r.updated, })) }