From a2b0c7c15e292470d2d8473f5556d2d59fb4e0b6 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 4 Jul 2025 16:10:39 +0200 Subject: [PATCH] Implement transaction service --- src/services/transactionService.ts | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/services/transactionService.ts diff --git a/src/services/transactionService.ts b/src/services/transactionService.ts new file mode 100644 index 0000000..d1fc045 --- /dev/null +++ b/src/services/transactionService.ts @@ -0,0 +1,44 @@ +import { IndJob } from '@/lib/types'; +import type { IndTransactionRecord, IndTransactionRecordNoId } from '../lib/pbtypes'; +import pb from '../lib/pocketbase'; +import { getJob, updateJob } from './jobService'; + +export async function createTransaction( + job: IndJob, + transaction: IndTransactionRecordNoId, + type: 'expenditure' | 'income' +): Promise { + console.log('Adding transaction:', transaction); + // Create the transaction + transaction.job = job.id; + const createdTransaction = await pb.collection('ind_transaction').create(transaction); + + // Update the job to include the new transaction + const field = type === 'expenditure' ? 'expenditures' : 'income'; + const currentIds = job[field] || []; + + await updateJob(job.id, { + [field]: [...currentIds, createdTransaction.id] + }); +} + +export async function updateTransaction( + transactionId: string, + updates: Partial +): Promise { + await pb.collection('ind_transaction').update(transactionId, updates); +} + +export async function deleteTransaction(job: IndJob, transactionId: string): Promise { + // Delete the transaction + await pb.collection('ind_transaction').delete(transactionId); + + // Remove from both expenditures and income arrays + const expenditures = job.expenditures.filter(ex => ex.id !== transactionId); + const income = job.income.filter(pero => pero.id !== transactionId); + + await updateJob(job.id, { + expenditures: expenditures.map(ex => ex.id), + income: income.map(pero => pero.id) + }); +}