118 lines
3.6 KiB
TypeScript
118 lines
3.6 KiB
TypeScript
|
|
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<IndJobRecord> {
|
|
console.log('Creating job:', job);
|
|
return await pb.collection<IndJobRecord>('ind_job').create(job);
|
|
}
|
|
|
|
export async function getJobs(): Promise<IndJobRecord[]> {
|
|
console.log('Getting jobs');
|
|
const result = await pb.collection<IndJobRecord>('ind_job').getFullList();
|
|
return result;
|
|
}
|
|
export async function getJobsFull(): Promise<IndJob[]> {
|
|
const jobs = await getJobs();
|
|
console.log('Jobs:', jobs);
|
|
return await Promise.all(jobs.map(toFullJob));
|
|
}
|
|
|
|
export async function getJob(id: string): Promise<IndJobRecord | null> {
|
|
console.log('Getting job:', id);
|
|
try {
|
|
return await pb.collection<IndJobRecord>('ind_job').getOne(id);
|
|
} catch (e) {
|
|
if (e.status === 404) return null;
|
|
throw e;
|
|
}
|
|
}
|
|
export async function getJobFull(id: string): Promise<IndJob | null> {
|
|
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<IndJobRecord>): Promise<IndJobRecord> {
|
|
console.log('Updating job:', id, updates);
|
|
return await pb.collection<IndJobRecord>('ind_job').update(id, updates);
|
|
}
|
|
|
|
export async function deleteJob(id: string): Promise<void> {
|
|
console.log('Deleting job:', id);
|
|
await pb.collection<IndJobRecord>('ind_job').delete(id);
|
|
}
|
|
|
|
async function toFullJob(job: IndJobRecord): Promise<IndJob> {
|
|
// 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<IndJobRecord> {
|
|
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)
|
|
};
|
|
} |