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