Add a fat type will all the relations

This commit is contained in:
2025-07-04 15:41:33 +02:00
parent fb16824137
commit da2538ded5
2 changed files with 36 additions and 23 deletions

20
src/lib/types.ts Normal file
View File

@@ -0,0 +1,20 @@
import { IndJobStatusOptions, IndTransactionRecord } from "./pbtypes"
import { IsoDateString } from "./pbtypes"
import { IndBillitemRecord } from "./pbtypes"
export type IndJob = {
billOfMaterials?: IndBillitemRecord[]
consumedMaterials?: IndBillitemRecord[]
created?: IsoDateString
expenditures?: IndTransactionRecord[]
id: string
income?: IndTransactionRecord[]
jobEnd?: IsoDateString
jobStart?: IsoDateString
outputItem: string
outputQuantity: number
saleEnd?: IsoDateString
saleStart?: IsoDateString
status: IndJobStatusOptions
updated?: IsoDateString
}

View File

@@ -1,30 +1,23 @@
import * as jobService from './jobService';
import * as facilityService from './facilityService';
import { IndJobResponse, IndTransactionResponse, IndBillitemResponse } from '@/lib/pbtypes';
import { IndTransactionRecord, IndBillitemRecord, IndJobRecord } from '@/lib/pbtypes';
import pb from '@/lib/pocketbase';
import { IndJob } from '@/lib/types';
export interface JobWithRelations extends IndJobResponse {
expenditures: IndTransactionResponse[];
income: IndTransactionResponse[];
billOfMaterials: IndBillitemResponse[];
consumedMaterials: { name: string; required: number }[];
}
export async function getJobsWithRelations(): Promise<JobWithRelations[]> {
const jobs = await jobService.getJobs();
const jobsWithRelations: JobWithRelations[] = [];
export async function getJobsWithRelations(): Promise<IndJob[]> {
const jobs: IndJobRecord[] = await jobService.getJobs();
const jobsWithRelations: IndJob[] = [];
for (const job of jobs) {
const expenditures: IndTransactionResponse[] = [];
const income: IndTransactionResponse[] = [];
const billOfMaterials: IndBillitemResponse[] = [];
const expenditures: IndTransactionRecord[] = [];
const income: IndTransactionRecord[] = [];
const billOfMaterials: IndBillitemRecord[] = [];
// Fetch related transactions
if (job.expenditures) {
for (const txId of job.expenditures) {
try {
const tx = await pb.collection('ind_transaction').getOne(txId);
expenditures.push(tx as IndTransactionResponse);
expenditures.push(tx as IndTransactionRecord);
} catch (e) {
console.warn('Failed to fetch expenditure transaction:', txId);
}
@@ -35,7 +28,7 @@ export async function getJobsWithRelations(): Promise<JobWithRelations[]> {
for (const txId of job.income) {
try {
const tx = await pb.collection('ind_transaction').getOne(txId);
income.push(tx as IndTransactionResponse);
income.push(tx as IndTransactionRecord);
} catch (e) {
console.warn('Failed to fetch income transaction:', txId);
}
@@ -47,7 +40,7 @@ export async function getJobsWithRelations(): Promise<JobWithRelations[]> {
for (const itemId of job.billOfMaterials) {
try {
const item = await pb.collection('ind_billItem').getOne(itemId);
billOfMaterials.push(item as IndBillitemResponse);
billOfMaterials.push(item as IndBillitemRecord);
} catch (e) {
console.warn('Failed to fetch bill item:', itemId);
}
@@ -66,13 +59,13 @@ export async function getJobsWithRelations(): Promise<JobWithRelations[]> {
return jobsWithRelations;
}
export async function getJobWithRelations(id: string): Promise<JobWithRelations | null> {
export async function getJobWithRelations(id: string): Promise<IndJob | null> {
const job = await jobService.getJob(id);
if (!job) return null;
const expenditures: IndTransactionResponse[] = [];
const income: IndTransactionResponse[] = [];
const billOfMaterials: IndBillitemResponse[] = [];
const expenditures: IndTransactionRecord[] = [];
const income: IndTransactionRecord[] = [];
const billOfMaterials: IndBillitemRecord[] = [];
// Fetch related data similar to above
// ... (similar logic as in getJobsWithRelations)