Add logs to services
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import { IndBillitemRecord } from "@/lib/pbtypes";
|
||||
import { IndBillitemRecord, IndBillitemRecordNoId } from "@/lib/pbtypes";
|
||||
import { IndJob } from "@/lib/types";
|
||||
import pb from "@/lib/pocketbase";
|
||||
import { updateJob } from "./jobService";
|
||||
|
||||
export async function addBillItem(
|
||||
job: IndJob,
|
||||
billItem: Omit<IndBillitemRecord, 'id' | 'created' | 'updated'>,
|
||||
billItem: IndBillitemRecordNoId,
|
||||
type: 'billOfMaterials' | 'consumedMaterials'
|
||||
): Promise<IndBillitemRecord> {
|
||||
console.log('Adding bill item:', billItem);
|
||||
// Create the bill item
|
||||
const createdItem = await pb.collection<IndBillitemRecord>('ind_billItem').create(billItem);
|
||||
|
||||
|
@@ -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: []
|
||||
};
|
||||
}
|
@@ -8,19 +8,23 @@ 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) {
|
||||
@@ -29,20 +33,24 @@ export async function getJob(id: string): Promise<IndJobRecord | null> {
|
||||
}
|
||||
}
|
||||
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: [],
|
||||
@@ -99,6 +107,7 @@ async function toFullJob(job: IndJobRecord): Promise<IndJob> {
|
||||
}
|
||||
|
||||
async function toHollowJob(job: IndJob): Promise<IndJobRecord> {
|
||||
console.log('Converting job to hollow job:', job);
|
||||
return {
|
||||
...job,
|
||||
expenditures: job.expenditures.map(tx => tx.id),
|
||||
|
@@ -8,7 +8,7 @@ export async function createTransaction(
|
||||
transaction: IndTransactionRecordNoId,
|
||||
type: 'expenditure' | 'income'
|
||||
): Promise<void> {
|
||||
console.log('Adding transaction:', transaction);
|
||||
console.log('Creating transaction:', transaction);
|
||||
// Create the transaction
|
||||
transaction.job = job.id;
|
||||
const createdTransaction = await pb.collection<IndTransactionRecord>('ind_transaction').create(transaction);
|
||||
@@ -26,10 +26,12 @@ export async function updateTransaction(
|
||||
transactionId: string,
|
||||
updates: Partial<IndTransactionRecord>
|
||||
): Promise<void> {
|
||||
console.log('Updating transaction:', transactionId, updates);
|
||||
await pb.collection<IndTransactionRecord>('ind_transaction').update(transactionId, updates);
|
||||
}
|
||||
|
||||
export async function deleteTransaction(job: IndJob, transactionId: string): Promise<void> {
|
||||
console.log('Deleting transaction:', transactionId);
|
||||
// Delete the transaction
|
||||
await pb.collection<IndTransactionRecord>('ind_transaction').delete(transactionId);
|
||||
|
||||
|
Reference in New Issue
Block a user