Entirely rework job service
This commit is contained in:
@@ -1,99 +1,109 @@
|
|||||||
|
|
||||||
import type { IndJobRecord, IndJobResponse, IndTransactionRecord, IndTransactionResponse, IndBillitemRecord, IndBillitemResponse } from '../lib/pbtypes';
|
import { IndJob } from '@/lib/types';
|
||||||
|
import type { IndJobRecord, IndTransactionRecord, IndBillitemRecord, IndJobRecordNoId } from '../lib/pbtypes';
|
||||||
import pb from '../lib/pocketbase';
|
import pb from '../lib/pocketbase';
|
||||||
|
|
||||||
export type { IndJobRecord as Job } from '../lib/pbtypes';
|
export type { IndJobRecord as Job } from '../lib/pbtypes';
|
||||||
export type { IndTransactionRecord as Transaction } from '../lib/pbtypes';
|
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: Omit<IndJobRecord, 'id' | 'created' | 'updated'>): Promise<IndJobResponse> {
|
export async function createJob(job: IndJobRecordNoId): Promise<IndJobRecord> {
|
||||||
return await pb.collection('ind_job').create(job) as IndJobResponse;
|
return await pb.collection<IndJobRecord>('ind_job').create(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getJobs(): Promise<IndJobResponse[]> {
|
export async function getJobs(): Promise<IndJobRecord[]> {
|
||||||
const result = await pb.collection('ind_job').getFullList();
|
const result = await pb.collection<IndJobRecord>('ind_job').getFullList();
|
||||||
return result as IndJobResponse[];
|
return result;
|
||||||
|
}
|
||||||
|
export async function getJobsFull(): Promise<IndJob[]> {
|
||||||
|
const jobs = await getJobs();
|
||||||
|
return await Promise.all(jobs.map(toFullJob));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getJob(id: string): Promise<IndJobResponse | null> {
|
export async function getJob(id: string): Promise<IndJobRecord | null> {
|
||||||
try {
|
try {
|
||||||
return await pb.collection('ind_job').getOne(id) as IndJobResponse;
|
return await pb.collection<IndJobRecord>('ind_job').getOne(id);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.status === 404) return null;
|
if (e.status === 404) return null;
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export async function getJobFull(id: string): Promise<IndJob | null> {
|
||||||
|
const job = await getJob(id);
|
||||||
|
if (!job) return null;
|
||||||
|
return await toFullJob(job);
|
||||||
|
}
|
||||||
|
|
||||||
export async function updateJob(id: string, updates: Partial<IndJobRecord>): Promise<IndJobResponse> {
|
export async function updateJob(id: string, updates: Partial<IndJobRecord>): Promise<IndJobRecord> {
|
||||||
return await pb.collection('ind_job').update(id, updates) as IndJobResponse;
|
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> {
|
||||||
await pb.collection('ind_job').delete(id);
|
await pb.collection<IndJobRecord>('ind_job').delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function addTransaction(
|
async function toFullJob(job: IndJobRecord): Promise<IndJob> {
|
||||||
jobId: string,
|
const fullJob = {
|
||||||
transaction: Omit<IndTransactionRecord, 'id' | 'created' | 'updated'>,
|
...job,
|
||||||
type: 'expenditure' | 'income'
|
expenditures: [],
|
||||||
): Promise<void> {
|
income: [],
|
||||||
// Create the transaction
|
billOfMaterials: [],
|
||||||
const createdTransaction = await pb.collection('ind_transaction').create({
|
consumedMaterials: []
|
||||||
...transaction,
|
};
|
||||||
job: jobId
|
|
||||||
}) as IndTransactionResponse;
|
|
||||||
|
|
||||||
// Update the job to include the new transaction
|
if (job.expenditures) {
|
||||||
const job = await getJob(jobId);
|
for (const txId of job.expenditures) {
|
||||||
if (!job) throw new Error('Job not found');
|
try {
|
||||||
|
const tx = await pb.collection('ind_transaction').getOne(txId);
|
||||||
const field = type === 'expenditure' ? 'expenditures' : 'income';
|
fullJob.expenditures.push(tx as IndTransactionRecord);
|
||||||
const currentIds = job[field] || [];
|
} catch (e) {
|
||||||
|
console.warn('Failed to fetch expenditure transaction:', txId);
|
||||||
await updateJob(jobId, {
|
}
|
||||||
[field]: [...currentIds, createdTransaction.id]
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateTransaction(
|
if (job.income) {
|
||||||
jobId: string,
|
for (const txId of job.income) {
|
||||||
transactionId: string,
|
try {
|
||||||
updates: Partial<IndTransactionRecord>
|
const tx = await pb.collection('ind_transaction').getOne(txId);
|
||||||
): Promise<void> {
|
fullJob.income.push(tx as IndTransactionRecord);
|
||||||
await pb.collection('ind_transaction').update(transactionId, updates);
|
} catch (e) {
|
||||||
|
console.warn('Failed to fetch income transaction:', txId);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteTransaction(jobId: string, transactionId: string): Promise<void> {
|
if (job.billOfMaterials) {
|
||||||
// Delete the transaction
|
for (const itemId of job.billOfMaterials) {
|
||||||
await pb.collection('ind_transaction').delete(transactionId);
|
try {
|
||||||
|
const item = await pb.collection('ind_billItem').getOne(itemId);
|
||||||
// Update the job to remove the transaction reference
|
fullJob.billOfMaterials.push(item as IndBillitemRecord);
|
||||||
const job = await getJob(jobId);
|
} catch (e) {
|
||||||
if (!job) return;
|
console.warn('Failed to fetch bill item:', itemId);
|
||||||
|
}
|
||||||
// Remove from both expenditures and income arrays
|
}
|
||||||
const expenditures = (job.expenditures || []).filter(id => id !== transactionId);
|
|
||||||
const income = (job.income || []).filter(id => id !== transactionId);
|
|
||||||
|
|
||||||
await updateJob(jobId, {
|
|
||||||
expenditures,
|
|
||||||
income
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function addBillItem(
|
if (job.consumedMaterials) {
|
||||||
jobId: string,
|
for (const itemId of job.consumedMaterials) {
|
||||||
billItem: Omit<IndBillitemRecord, 'id' | 'created' | 'updated'>
|
try {
|
||||||
): Promise<void> {
|
const item = await pb.collection('ind_billItem').getOne(itemId);
|
||||||
// Create the bill item
|
fullJob.consumedMaterials.push(item as IndBillitemRecord);
|
||||||
const createdItem = await pb.collection('ind_billItem').create(billItem) as IndBillitemResponse;
|
} catch (e) {
|
||||||
|
console.warn('Failed to fetch consumed material:', itemId);
|
||||||
// Update the job to include the new bill item
|
}
|
||||||
const job = await getJob(jobId);
|
}
|
||||||
if (!job) throw new Error('Job not found');
|
}
|
||||||
|
|
||||||
const currentBillItems = job.billOfMaterials || [];
|
return fullJob;
|
||||||
await updateJob(jobId, {
|
}
|
||||||
billOfMaterials: [...currentBillItems, createdItem.id]
|
|
||||||
});
|
async function toHollowJob(job: IndJob): Promise<IndJobRecord> {
|
||||||
|
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)
|
||||||
|
};
|
||||||
}
|
}
|
Reference in New Issue
Block a user