import { IndJob } from '@/lib/types'; import type { IndJobRecord, IndTransactionRecord, IndBillitemRecord, IndJobRecordNoId } from '../lib/pbtypes'; import pb from '../lib/pocketbase'; export type { IndJobRecord as Job } from '../lib/pbtypes'; export type { IndTransactionRecord as Transaction } from '../lib/pbtypes'; export type { IndBillitemRecord as BillItem } from '../lib/pbtypes'; export async function createJob(job: IndJobRecordNoId): Promise { console.log('Creating job:', job); return await pb.collection('ind_job').create(job); } export async function getJobs(): Promise { console.log('Getting jobs'); const result = await pb.collection('ind_job').getFullList(); return result; } export async function getJobsFull(): Promise { const jobs = await getJobs(); console.log('Jobs:', jobs); return await Promise.all(jobs.map(toFullJob)); } export async function getJob(id: string): Promise { console.log('Getting job:', id); try { return await pb.collection('ind_job').getOne(id); } catch (e) { if (e.status === 404) return null; throw e; } } export async function getJobFull(id: string): Promise { console.log('Getting job full:', id); const job = await getJob(id); if (!job) return null; return await toFullJob(job); } export async function updateJob(id: string, updates: Partial): Promise { console.log('Updating job:', id, updates); return await pb.collection('ind_job').update(id, updates); } export async function deleteJob(id: string): Promise { console.log('Deleting job:', id); await pb.collection('ind_job').delete(id); } async function toFullJob(job: IndJobRecord): Promise { // console.log('Converting job to full job:', job); const fullJob = { ...job, expenditures: [], income: [], billOfMaterials: [], consumedMaterials: [] }; if (job.expenditures) { for (const txId of job.expenditures) { try { const tx = await pb.collection('ind_transaction').getOne(txId); fullJob.expenditures.push(tx as IndTransactionRecord); } catch (e) { console.warn('Failed to fetch expenditure transaction:', txId); } } } if (job.income) { for (const txId of job.income) { try { const tx = await pb.collection('ind_transaction').getOne(txId); fullJob.income.push(tx as IndTransactionRecord); } catch (e) { console.warn('Failed to fetch income transaction:', txId); } } } if (job.billOfMaterials) { for (const itemId of job.billOfMaterials) { try { const item = await pb.collection('ind_billItem').getOne(itemId); fullJob.billOfMaterials.push(item as IndBillitemRecord); } catch (e) { console.warn('Failed to fetch bill item:', itemId); } } } if (job.consumedMaterials) { for (const itemId of job.consumedMaterials) { try { const item = await pb.collection('ind_billItem').getOne(itemId); fullJob.consumedMaterials.push(item as IndBillitemRecord); } catch (e) { console.warn('Failed to fetch consumed material:', itemId); } } } return fullJob; } async function toHollowJob(job: IndJob): Promise { console.log('Converting job to hollow job:', job); return { ...job, expenditures: job.expenditures.map(tx => tx.id), income: job.income.map(tx => tx.id), billOfMaterials: job.billOfMaterials.map(item => item.id), consumedMaterials: job.consumedMaterials.map(item => item.id) }; }