Add logs to services

This commit is contained in:
2025-07-04 16:19:18 +02:00
parent 2e2682e5c5
commit 70d6979b47
4 changed files with 15 additions and 83 deletions

View File

@@ -1,13 +1,14 @@
import { IndBillitemRecord } from "@/lib/pbtypes"; import { IndBillitemRecord, IndBillitemRecordNoId } from "@/lib/pbtypes";
import { IndJob } from "@/lib/types"; import { IndJob } from "@/lib/types";
import pb from "@/lib/pocketbase"; import pb from "@/lib/pocketbase";
import { updateJob } from "./jobService"; import { updateJob } from "./jobService";
export async function addBillItem( export async function addBillItem(
job: IndJob, job: IndJob,
billItem: Omit<IndBillitemRecord, 'id' | 'created' | 'updated'>, billItem: IndBillitemRecordNoId,
type: 'billOfMaterials' | 'consumedMaterials' type: 'billOfMaterials' | 'consumedMaterials'
): Promise<IndBillitemRecord> { ): Promise<IndBillitemRecord> {
console.log('Adding bill item:', billItem);
// Create the bill item // Create the bill item
const createdItem = await pb.collection<IndBillitemRecord>('ind_billItem').create(billItem); const createdItem = await pb.collection<IndBillitemRecord>('ind_billItem').create(billItem);

View File

@@ -1,80 +0,0 @@
import * as jobService from './jobService';
import { IndTransactionRecord, IndBillitemRecord, IndJobRecord } from '@/lib/pbtypes';
import pb from '@/lib/pocketbase';
import { IndJob } from '@/lib/types';
export async function getJobsWithRelations(): Promise<IndJob[]> {
const jobs: IndJobRecord[] = await jobService.getJobs();
const jobsWithRelations: IndJob[] = [];
for (const job of jobs) {
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 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);
income.push(tx as IndTransactionRecord);
} catch (e) {
console.warn('Failed to fetch income transaction:', txId);
}
}
}
// Fetch bill of materials
if (job.billOfMaterials) {
for (const itemId of job.billOfMaterials) {
try {
const item = await pb.collection('ind_billItem').getOne(itemId);
billOfMaterials.push(item as IndBillitemRecord);
} catch (e) {
console.warn('Failed to fetch bill item:', itemId);
}
}
}
jobsWithRelations.push({
...job,
expenditures,
income,
billOfMaterials,
consumedMaterials: [] // This would need to be stored/retrieved differently
});
}
return jobsWithRelations;
}
export async function getJobWithRelations(id: string): Promise<IndJob | null> {
const job = await jobService.getJob(id);
if (!job) return null;
const expenditures: IndTransactionRecord[] = [];
const income: IndTransactionRecord[] = [];
const billOfMaterials: IndBillitemRecord[] = [];
// Fetch related data similar to above
// ... (similar logic as in getJobsWithRelations)
return {
...job,
expenditures,
income,
billOfMaterials,
consumedMaterials: []
};
}

View File

@@ -8,19 +8,23 @@ export type { IndTransactionRecord as Transaction } from '../lib/pbtypes';
export type { IndBillitemRecord as BillItem } from '../lib/pbtypes'; export type { IndBillitemRecord as BillItem } from '../lib/pbtypes';
export async function createJob(job: IndJobRecordNoId): Promise<IndJobRecord> { export async function createJob(job: IndJobRecordNoId): Promise<IndJobRecord> {
console.log('Creating job:', job);
return await pb.collection<IndJobRecord>('ind_job').create(job); return await pb.collection<IndJobRecord>('ind_job').create(job);
} }
export async function getJobs(): Promise<IndJobRecord[]> { export async function getJobs(): Promise<IndJobRecord[]> {
console.log('Getting jobs');
const result = await pb.collection<IndJobRecord>('ind_job').getFullList(); const result = await pb.collection<IndJobRecord>('ind_job').getFullList();
return result; return result;
} }
export async function getJobsFull(): Promise<IndJob[]> { export async function getJobsFull(): Promise<IndJob[]> {
const jobs = await getJobs(); const jobs = await getJobs();
console.log('Jobs:', jobs);
return await Promise.all(jobs.map(toFullJob)); return await Promise.all(jobs.map(toFullJob));
} }
export async function getJob(id: string): Promise<IndJobRecord | null> { export async function getJob(id: string): Promise<IndJobRecord | null> {
console.log('Getting job:', id);
try { try {
return await pb.collection<IndJobRecord>('ind_job').getOne(id); return await pb.collection<IndJobRecord>('ind_job').getOne(id);
} catch (e) { } catch (e) {
@@ -29,20 +33,24 @@ export async function getJob(id: string): Promise<IndJobRecord | null> {
} }
} }
export async function getJobFull(id: string): Promise<IndJob | null> { export async function getJobFull(id: string): Promise<IndJob | null> {
console.log('Getting job full:', id);
const job = await getJob(id); const job = await getJob(id);
if (!job) return null; if (!job) return null;
return await toFullJob(job); return await toFullJob(job);
} }
export async function updateJob(id: string, updates: Partial<IndJobRecord>): Promise<IndJobRecord> { 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); return await pb.collection<IndJobRecord>('ind_job').update(id, updates);
} }
export async function deleteJob(id: string): Promise<void> { export async function deleteJob(id: string): Promise<void> {
console.log('Deleting job:', id);
await pb.collection<IndJobRecord>('ind_job').delete(id); await pb.collection<IndJobRecord>('ind_job').delete(id);
} }
async function toFullJob(job: IndJobRecord): Promise<IndJob> { async function toFullJob(job: IndJobRecord): Promise<IndJob> {
console.log('Converting job to full job:', job);
const fullJob = { const fullJob = {
...job, ...job,
expenditures: [], expenditures: [],
@@ -99,6 +107,7 @@ async function toFullJob(job: IndJobRecord): Promise<IndJob> {
} }
async function toHollowJob(job: IndJob): Promise<IndJobRecord> { async function toHollowJob(job: IndJob): Promise<IndJobRecord> {
console.log('Converting job to hollow job:', job);
return { return {
...job, ...job,
expenditures: job.expenditures.map(tx => tx.id), expenditures: job.expenditures.map(tx => tx.id),

View File

@@ -8,7 +8,7 @@ export async function createTransaction(
transaction: IndTransactionRecordNoId, transaction: IndTransactionRecordNoId,
type: 'expenditure' | 'income' type: 'expenditure' | 'income'
): Promise<void> { ): Promise<void> {
console.log('Adding transaction:', transaction); console.log('Creating transaction:', transaction);
// Create the transaction // Create the transaction
transaction.job = job.id; transaction.job = job.id;
const createdTransaction = await pb.collection<IndTransactionRecord>('ind_transaction').create(transaction); const createdTransaction = await pb.collection<IndTransactionRecord>('ind_transaction').create(transaction);
@@ -26,10 +26,12 @@ export async function updateTransaction(
transactionId: string, transactionId: string,
updates: Partial<IndTransactionRecord> updates: Partial<IndTransactionRecord>
): Promise<void> { ): Promise<void> {
console.log('Updating transaction:', transactionId, updates);
await pb.collection<IndTransactionRecord>('ind_transaction').update(transactionId, updates); await pb.collection<IndTransactionRecord>('ind_transaction').update(transactionId, updates);
} }
export async function deleteTransaction(job: IndJob, transactionId: string): Promise<void> { export async function deleteTransaction(job: IndJob, transactionId: string): Promise<void> {
console.log('Deleting transaction:', transactionId);
// Delete the transaction // Delete the transaction
await pb.collection<IndTransactionRecord>('ind_transaction').delete(transactionId); await pb.collection<IndTransactionRecord>('ind_transaction').delete(transactionId);