Refactor: Centralize database operations
Consolidated database interaction logic into service files. Streamlined job and transaction creation/update processes to minimize database requests, improving efficiency.
This commit is contained in:
@@ -4,13 +4,12 @@ import { Button } from '@/components/ui/button';
|
|||||||
import { Textarea } from '@/components/ui/textarea';
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
import { Import, Download, FileText } from 'lucide-react';
|
import { Import, Download, FileText } from 'lucide-react';
|
||||||
import { IndBillitemRecord } from '@/lib/pbtypes';
|
import { IndBillitemRecord, IndBillitemRecordNoId } from '@/lib/pbtypes';
|
||||||
import { IndJob } from '@/lib/types';
|
import { IndJob } from '@/lib/types';
|
||||||
import { addBillItem } from '@/services/billItemService';
|
import { dataService } from '@/services/dataService';
|
||||||
import { updateJob } from '@/services/jobService';
|
|
||||||
|
|
||||||
interface MaterialsImportExportProps {
|
interface MaterialsImportExportProps {
|
||||||
job: IndJob;
|
job?: IndJob;
|
||||||
billOfMaterials: IndBillitemRecord[];
|
billOfMaterials: IndBillitemRecord[];
|
||||||
consumedMaterials: IndBillitemRecord[];
|
consumedMaterials: IndBillitemRecord[];
|
||||||
onBillOfMaterialsUpdate: (billItems: IndBillitemRecord[]) => void;
|
onBillOfMaterialsUpdate: (billItems: IndBillitemRecord[]) => void;
|
||||||
@@ -27,9 +26,9 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
const [bomInput, setBomInput] = useState('');
|
const [bomInput, setBomInput] = useState('');
|
||||||
const [consumedInput, setConsumedInput] = useState('');
|
const [consumedInput, setConsumedInput] = useState('');
|
||||||
|
|
||||||
const parseBillOfMaterials = async (text: string): Promise<IndJob> => {
|
const parseBillOfMaterials = (text: string): IndBillitemRecordNoId[] => {
|
||||||
const lines = text.split('\n').filter(line => line.trim());
|
const lines = text.split('\n').filter(line => line.trim());
|
||||||
const materials: IndBillitemRecord[] = [];
|
const materials: IndBillitemRecordNoId[] = [];
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const parts = line.trim().split(/\s+/);
|
const parts = line.trim().split(/\s+/);
|
||||||
@@ -37,20 +36,17 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
const name = parts.slice(0, -1).join(' ');
|
const name = parts.slice(0, -1).join(' ');
|
||||||
const quantity = parseInt(parts[parts.length - 1]);
|
const quantity = parseInt(parts[parts.length - 1]);
|
||||||
if (name && !isNaN(quantity)) {
|
if (name && !isNaN(quantity)) {
|
||||||
const newBillItem = await addBillItem(job.id, { name, quantity });
|
materials.push({ name, quantity });
|
||||||
materials.push(newBillItem);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
job.billOfMaterials = materials;
|
return materials;
|
||||||
await updateJob(job.id, { billOfMaterials: materials.map(item => item.id) });
|
|
||||||
return job;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseConsumedMaterials = async (text: string): Promise<IndJob> => {
|
const parseConsumedMaterials = (text: string): IndBillitemRecordNoId[] => {
|
||||||
const lines = text.split('\n').filter(line => line.trim());
|
const lines = text.split('\n').filter(line => line.trim());
|
||||||
const materials: IndBillitemRecord[] = [];
|
const materials: IndBillitemRecordNoId[] = [];
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const parts = line.trim().split('\t');
|
const parts = line.trim().split('\t');
|
||||||
@@ -58,15 +54,12 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
const name = parts[0];
|
const name = parts[0];
|
||||||
const quantity = parseInt(parts[1]);
|
const quantity = parseInt(parts[1]);
|
||||||
if (name && !isNaN(quantity)) {
|
if (name && !isNaN(quantity)) {
|
||||||
const newBillItem = await addBillItem(job.id, { name, quantity });
|
materials.push({ name, quantity });
|
||||||
materials.push(newBillItem);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
job.consumedMaterials = materials;
|
return materials;
|
||||||
await updateJob(job.id, { consumedMaterials: materials.map(item => item.id) });
|
|
||||||
return job;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const exportBillOfMaterials = (): string => {
|
const exportBillOfMaterials = (): string => {
|
||||||
@@ -78,15 +71,33 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleImportBom = async () => {
|
const handleImportBom = async () => {
|
||||||
const parsed = await parseBillOfMaterials(bomInput);
|
if (!job) return;
|
||||||
onBillOfMaterialsUpdate(parsed.billOfMaterials);
|
|
||||||
setBomInput('');
|
const materials = parseBillOfMaterials(bomInput);
|
||||||
|
if (materials.length > 0) {
|
||||||
|
try {
|
||||||
|
const updatedJob = await dataService.createMultipleBillItems(job.id, materials, 'billOfMaterials');
|
||||||
|
onBillOfMaterialsUpdate(updatedJob.billOfMaterials);
|
||||||
|
setBomInput('');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error importing bill of materials:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleImportConsumed = async () => {
|
const handleImportConsumed = async () => {
|
||||||
const parsed = await parseConsumedMaterials(consumedInput);
|
if (!job) return;
|
||||||
onConsumedMaterialsUpdate(parsed.consumedMaterials);
|
|
||||||
setConsumedInput('');
|
const materials = parseConsumedMaterials(consumedInput);
|
||||||
|
if (materials.length > 0) {
|
||||||
|
try {
|
||||||
|
const updatedJob = await dataService.createMultipleBillItems(job.id, materials, 'consumedMaterials');
|
||||||
|
onConsumedMaterialsUpdate(updatedJob.consumedMaterials);
|
||||||
|
setConsumedInput('');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error importing consumed materials:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleExportBom = () => {
|
const handleExportBom = () => {
|
||||||
@@ -131,6 +142,7 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
<Button
|
<Button
|
||||||
onClick={handleImportBom}
|
onClick={handleImportBom}
|
||||||
className="bg-blue-600 hover:bg-blue-700"
|
className="bg-blue-600 hover:bg-blue-700"
|
||||||
|
disabled={!job}
|
||||||
>
|
>
|
||||||
<Import className="w-4 h-4 mr-2" />
|
<Import className="w-4 h-4 mr-2" />
|
||||||
Import Bill of Materials
|
Import Bill of Materials
|
||||||
@@ -160,6 +172,7 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
<Button
|
<Button
|
||||||
onClick={handleImportConsumed}
|
onClick={handleImportConsumed}
|
||||||
className="bg-blue-600 hover:bg-blue-700"
|
className="bg-blue-600 hover:bg-blue-700"
|
||||||
|
disabled={!job}
|
||||||
>
|
>
|
||||||
<Import className="w-4 h-4 mr-2" />
|
<Import className="w-4 h-4 mr-2" />
|
||||||
Import Consumed Materials
|
Import Consumed Materials
|
||||||
|
69
src/hooks/useDataService.ts
Normal file
69
src/hooks/useDataService.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import { dataService } from '@/services/dataService';
|
||||||
|
import { IndJob } from '@/lib/types';
|
||||||
|
|
||||||
|
export function useJobs() {
|
||||||
|
const [jobs, setJobs] = useState<IndJob[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const loadJobs = async () => {
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
await dataService.loadJobs();
|
||||||
|
setError(null);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'Failed to load jobs');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
loadJobs();
|
||||||
|
|
||||||
|
const unsubscribe = dataService.subscribe(() => {
|
||||||
|
setJobs(dataService.getJobs());
|
||||||
|
});
|
||||||
|
|
||||||
|
return unsubscribe;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return {
|
||||||
|
jobs,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
createJob: dataService.createJob.bind(dataService),
|
||||||
|
updateJob: dataService.updateJob.bind(dataService),
|
||||||
|
deleteJob: dataService.deleteJob.bind(dataService),
|
||||||
|
createTransaction: dataService.createTransaction.bind(dataService),
|
||||||
|
createMultipleTransactions: dataService.createMultipleTransactions.bind(dataService),
|
||||||
|
updateTransaction: dataService.updateTransaction.bind(dataService),
|
||||||
|
deleteTransaction: dataService.deleteTransaction.bind(dataService),
|
||||||
|
createBillItem: dataService.createBillItem.bind(dataService),
|
||||||
|
createMultipleBillItems: dataService.createMultipleBillItems.bind(dataService)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useJob(jobId: string | null) {
|
||||||
|
const [job, setJob] = useState<IndJob | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!jobId) {
|
||||||
|
setJob(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateJob = () => {
|
||||||
|
setJob(dataService.getJob(jobId));
|
||||||
|
};
|
||||||
|
|
||||||
|
updateJob();
|
||||||
|
|
||||||
|
const unsubscribe = dataService.subscribe(updateJob);
|
||||||
|
return unsubscribe;
|
||||||
|
}, [jobId]);
|
||||||
|
|
||||||
|
return job;
|
||||||
|
}
|
@@ -1,45 +1,54 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState } from 'react';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
import { Plus, Factory, TrendingUp, Briefcase, FileText } from 'lucide-react';
|
import { Plus, Factory, TrendingUp, Briefcase, FileText } from 'lucide-react';
|
||||||
import { IndTransactionRecordNoId, IndJobRecordNoId, IndTransactionRecord, IndJobStatusOptions } from '@/lib/pbtypes';
|
import { IndTransactionRecordNoId, IndJobRecordNoId, IndJobStatusOptions } from '@/lib/pbtypes';
|
||||||
import * as jobService from '@/services/jobService';
|
|
||||||
import * as transactionService from '@/services/transactionService';
|
|
||||||
import { formatISK } from '@/utils/priceUtils';
|
import { formatISK } from '@/utils/priceUtils';
|
||||||
import JobCard from '@/components/JobCard';
|
import JobCard from '@/components/JobCard';
|
||||||
import JobForm from '@/components/JobForm';
|
import JobForm from '@/components/JobForm';
|
||||||
import TransactionForm from '@/components/TransactionForm';
|
import TransactionForm from '@/components/TransactionForm';
|
||||||
import TransactionTable from '@/components/TransactionTable';
|
import TransactionTable from '@/components/TransactionTable';
|
||||||
import { IndJob } from '@/lib/types';
|
import { IndJob } from '@/lib/types';
|
||||||
import { createJob } from '@/services/jobService';
|
|
||||||
import { Badge } from '@/components/ui/badge';
|
import { Badge } from '@/components/ui/badge';
|
||||||
import BatchTransactionForm from '@/components/BatchTransactionForm';
|
import BatchTransactionForm from '@/components/BatchTransactionForm';
|
||||||
|
import { useJobs, useJob } from '@/hooks/useDataService';
|
||||||
|
|
||||||
// TODO: Bill of materials just does not work currently Fix this shit
|
|
||||||
// Extended job type for UI components
|
|
||||||
const Index = () => {
|
const Index = () => {
|
||||||
const [jobs, setJobs] = useState<IndJob[]>([]);
|
const {
|
||||||
|
jobs,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
createJob,
|
||||||
|
updateJob,
|
||||||
|
deleteJob,
|
||||||
|
createMultipleTransactions,
|
||||||
|
updateTransaction,
|
||||||
|
deleteTransaction
|
||||||
|
} = useJobs();
|
||||||
|
|
||||||
const [showJobForm, setShowJobForm] = useState(false);
|
const [showJobForm, setShowJobForm] = useState(false);
|
||||||
const [editingJob, setEditingJob] = useState<IndJob | null>(null);
|
const [editingJob, setEditingJob] = useState<IndJob | null>(null);
|
||||||
const [selectedJob, setSelectedJob] = useState<IndJob | null>(null);
|
const [selectedJobId, setSelectedJobId] = useState<string | null>(null);
|
||||||
const [showBatchForm, setShowBatchForm] = useState(false);
|
const [showBatchForm, setShowBatchForm] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
const selectedJob = useJob(selectedJobId);
|
||||||
loadJobs();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const loadJobs = async () => {
|
if (loading) {
|
||||||
try {
|
return (
|
||||||
const fetchedJobs = await jobService.getJobsFull();
|
<div className="min-h-screen bg-gray-950 p-6 flex items-center justify-center">
|
||||||
// Convert to JobWithRelations format
|
<div className="text-white">Loading jobs...</div>
|
||||||
const jobsWithRelations: IndJob[] = fetchedJobs;
|
</div>
|
||||||
setJobs(jobsWithRelations);
|
);
|
||||||
} catch (error) {
|
}
|
||||||
console.error('Error loading jobs:', error);
|
|
||||||
}
|
if (error) {
|
||||||
};
|
return (
|
||||||
|
<div className="min-h-screen bg-gray-950 p-6 flex items-center justify-center">
|
||||||
|
<div className="text-red-400">Error: {error}</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Sort jobs by status priority
|
|
||||||
const getStatusPriority = (status: IndJobStatusOptions): number => {
|
const getStatusPriority = (status: IndJobStatusOptions): number => {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case 'Planned': return 6;
|
case 'Planned': return 6;
|
||||||
@@ -48,7 +57,7 @@ const Index = () => {
|
|||||||
case 'Done': return 3;
|
case 'Done': return 3;
|
||||||
case 'Selling': return 4;
|
case 'Selling': return 4;
|
||||||
case 'Closed': return 5;
|
case 'Closed': return 5;
|
||||||
case 'Tracked': return 7; // Put tracked jobs at the end
|
case 'Tracked': return 7;
|
||||||
default: return 0;
|
default: return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -70,17 +79,14 @@ const Index = () => {
|
|||||||
const priorityA = getStatusPriority(a.status);
|
const priorityA = getStatusPriority(a.status);
|
||||||
const priorityB = getStatusPriority(b.status);
|
const priorityB = getStatusPriority(b.status);
|
||||||
if (priorityA === priorityB) {
|
if (priorityA === priorityB) {
|
||||||
// If same status, sort by creation date (newest first)
|
|
||||||
return new Date(b.created || '').getTime() - new Date(a.created || '').getTime();
|
return new Date(b.created || '').getTime() - new Date(a.created || '').getTime();
|
||||||
}
|
}
|
||||||
return priorityA - priorityB;
|
return priorityA - priorityB;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Separate regular and tracked jobs
|
|
||||||
const regularJobs = sortedJobs.filter(job => job.status !== 'Tracked');
|
const regularJobs = sortedJobs.filter(job => job.status !== 'Tracked');
|
||||||
const trackedJobs = sortedJobs.filter(job => job.status === 'Tracked');
|
const trackedJobs = sortedJobs.filter(job => job.status === 'Tracked');
|
||||||
|
|
||||||
// Calculate totals excluding tracked jobs
|
|
||||||
const totalJobs = regularJobs.length;
|
const totalJobs = regularJobs.length;
|
||||||
const totalProfit = regularJobs.reduce((sum, job) => {
|
const totalProfit = regularJobs.reduce((sum, job) => {
|
||||||
const expenditure = job.expenditures.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
const expenditure = job.expenditures.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
||||||
@@ -94,15 +100,7 @@ const Index = () => {
|
|||||||
|
|
||||||
const handleCreateJob = async (jobData: IndJobRecordNoId) => {
|
const handleCreateJob = async (jobData: IndJobRecordNoId) => {
|
||||||
try {
|
try {
|
||||||
const newJob = await createJob(jobData);
|
await createJob(jobData);
|
||||||
const jobWithRelations: IndJob = {
|
|
||||||
...newJob,
|
|
||||||
expenditures: [],
|
|
||||||
income: [],
|
|
||||||
billOfMaterials: [],
|
|
||||||
consumedMaterials: []
|
|
||||||
};
|
|
||||||
setJobs([...jobs, jobWithRelations]);
|
|
||||||
setShowJobForm(false);
|
setShowJobForm(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating job:', error);
|
console.error('Error creating job:', error);
|
||||||
@@ -118,22 +116,9 @@ const Index = () => {
|
|||||||
if (!editingJob) return;
|
if (!editingJob) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const updatedJob = await jobService.updateJob(editingJob.id, jobData);
|
await updateJob(editingJob.id, jobData);
|
||||||
const updatedJobWithRelations: IndJob = {
|
|
||||||
...updatedJob,
|
|
||||||
expenditures: editingJob.expenditures,
|
|
||||||
income: editingJob.income,
|
|
||||||
billOfMaterials: editingJob.billOfMaterials,
|
|
||||||
consumedMaterials: editingJob.consumedMaterials
|
|
||||||
};
|
|
||||||
|
|
||||||
setJobs(jobs.map(job => job.id === editingJob.id ? updatedJobWithRelations : job));
|
|
||||||
setShowJobForm(false);
|
setShowJobForm(false);
|
||||||
setEditingJob(null);
|
setEditingJob(null);
|
||||||
|
|
||||||
if (selectedJob?.id === editingJob.id) {
|
|
||||||
setSelectedJob(updatedJobWithRelations);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error updating job:', error);
|
console.error('Error updating job:', error);
|
||||||
}
|
}
|
||||||
@@ -142,10 +127,9 @@ const Index = () => {
|
|||||||
const handleDeleteJob = async (jobId: string) => {
|
const handleDeleteJob = async (jobId: string) => {
|
||||||
if (confirm('Are you sure you want to delete this job?')) {
|
if (confirm('Are you sure you want to delete this job?')) {
|
||||||
try {
|
try {
|
||||||
await jobService.deleteJob(jobId);
|
await deleteJob(jobId);
|
||||||
setJobs(jobs.filter(job => job.id !== jobId));
|
if (selectedJobId === jobId) {
|
||||||
if (selectedJob?.id === jobId) {
|
setSelectedJobId(null);
|
||||||
setSelectedJob(null);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting job:', error);
|
console.error('Error deleting job:', error);
|
||||||
@@ -154,76 +138,43 @@ const Index = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleTransactionsAdded = async (transactions: IndTransactionRecordNoId[], type: 'expenditure' | 'income') => {
|
const handleTransactionsAdded = async (transactions: IndTransactionRecordNoId[], type: 'expenditure' | 'income') => {
|
||||||
if (!selectedJob) return;
|
if (!selectedJobId) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let updatedJob = selectedJob;
|
await createMultipleTransactions(selectedJobId, transactions, type);
|
||||||
for (const transaction of transactions) {
|
|
||||||
updatedJob = await transactionService.createTransaction(updatedJob, transaction, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update local state
|
|
||||||
setSelectedJob(updatedJob);
|
|
||||||
setJobs(jobs.map(job => job.id === selectedJob.id ? updatedJob : job));
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error adding transactions:', error);
|
console.error('Error adding transactions:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUpdateTransaction = async (transactionId: string, updates: Partial<IndTransactionRecord>) => {
|
const handleUpdateTransaction = async (transactionId: string, updates: Partial<IndTransactionRecord>) => {
|
||||||
if (!selectedJob) return;
|
if (!selectedJobId) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let updatedJob = selectedJob;
|
await updateTransaction(selectedJobId, transactionId, updates);
|
||||||
updatedJob = await transactionService.updateTransaction(updatedJob, transactionId, updates);
|
|
||||||
// Update local state
|
|
||||||
setSelectedJob(updatedJob);
|
|
||||||
setJobs(jobs.map(job => job.id === selectedJob.id ? updatedJob : job));
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error updating transaction:', error);
|
console.error('Error updating transaction:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDeleteTransaction = async (transactionId: string) => {
|
const handleDeleteTransaction = async (transactionId: string) => {
|
||||||
if (!selectedJob) return;
|
if (!selectedJobId) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await transactionService.deleteTransaction(selectedJob, transactionId);
|
await deleteTransaction(selectedJobId, transactionId);
|
||||||
|
|
||||||
// Update local state
|
|
||||||
const updatedJob = { ...selectedJob };
|
|
||||||
updatedJob.expenditures = updatedJob.expenditures.filter(tx => tx.id !== transactionId);
|
|
||||||
updatedJob.income = updatedJob.income.filter(tx => tx.id !== transactionId);
|
|
||||||
|
|
||||||
setSelectedJob(updatedJob);
|
|
||||||
setJobs(jobs.map(job => job.id === selectedJob.id ? updatedJob : job));
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting transaction:', error);
|
console.error('Error deleting transaction:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUpdateProduced = async (jobId: string, produced: number) => {
|
const handleUpdateProduced = async (jobId: string, produced: number) => {
|
||||||
if (!selectedJob) return;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const updatedJob = await jobService.updateJob(jobId, { produced });
|
await updateJob(jobId, { produced });
|
||||||
// Update local state
|
|
||||||
const jobWithRelations: IndJob = {
|
|
||||||
...updatedJob,
|
|
||||||
expenditures: selectedJob.expenditures,
|
|
||||||
income: selectedJob.income,
|
|
||||||
billOfMaterials: selectedJob.billOfMaterials,
|
|
||||||
consumedMaterials: selectedJob.consumedMaterials
|
|
||||||
};
|
|
||||||
|
|
||||||
setSelectedJob(jobWithRelations);
|
|
||||||
setJobs(jobs.map(job => job.id === jobId ? jobWithRelations : job));
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error updating produced quantity:', error);
|
console.error('Error updating produced quantity:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Group jobs by status
|
|
||||||
const jobGroups = regularJobs.reduce((groups, job) => {
|
const jobGroups = regularJobs.reduce((groups, job) => {
|
||||||
const status = job.status;
|
const status = job.status;
|
||||||
if (!groups[status]) {
|
if (!groups[status]) {
|
||||||
@@ -233,13 +184,11 @@ const Index = () => {
|
|||||||
return groups;
|
return groups;
|
||||||
}, {} as Record<string, IndJob[]>);
|
}, {} as Record<string, IndJob[]>);
|
||||||
|
|
||||||
// Load collapsed state from localStorage
|
|
||||||
const [collapsedGroups, setCollapsedGroups] = useState<Record<string, boolean>>(() => {
|
const [collapsedGroups, setCollapsedGroups] = useState<Record<string, boolean>>(() => {
|
||||||
const saved = localStorage.getItem('jobGroupsCollapsed');
|
const saved = localStorage.getItem('jobGroupsCollapsed');
|
||||||
return saved ? JSON.parse(saved) : {};
|
return saved ? JSON.parse(saved) : {};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Toggle group collapse
|
|
||||||
const toggleGroup = (status: string) => {
|
const toggleGroup = (status: string) => {
|
||||||
const newState = { ...collapsedGroups, [status]: !collapsedGroups[status] };
|
const newState = { ...collapsedGroups, [status]: !collapsedGroups[status] };
|
||||||
setCollapsedGroups(newState);
|
setCollapsedGroups(newState);
|
||||||
@@ -249,15 +198,7 @@ const Index = () => {
|
|||||||
const handleBatchTransactionsAssigned = async (assignments: { jobId: string, transactions: IndTransactionRecordNoId[] }[]) => {
|
const handleBatchTransactionsAssigned = async (assignments: { jobId: string, transactions: IndTransactionRecordNoId[] }[]) => {
|
||||||
try {
|
try {
|
||||||
for (const { jobId, transactions } of assignments) {
|
for (const { jobId, transactions } of assignments) {
|
||||||
const job = jobs.find(j => j.id === jobId);
|
await createMultipleTransactions(jobId, transactions, 'income');
|
||||||
if (job) {
|
|
||||||
let updatedJob = job;
|
|
||||||
for (const transaction of transactions) {
|
|
||||||
updatedJob = await transactionService.createTransaction(updatedJob, transaction, 'income');
|
|
||||||
}
|
|
||||||
// Update local state
|
|
||||||
setJobs(jobs.map(j => j.id === jobId ? updatedJob : j));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error assigning batch transactions:', error);
|
console.error('Error assigning batch transactions:', error);
|
||||||
@@ -292,7 +233,7 @@ const Index = () => {
|
|||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
onClick={() => setSelectedJob(null)}
|
onClick={() => setSelectedJobId(null)}
|
||||||
className="border-gray-600 hover:bg-gray-800"
|
className="border-gray-600 hover:bg-gray-800"
|
||||||
>
|
>
|
||||||
Back to Jobs
|
Back to Jobs
|
||||||
@@ -335,7 +276,6 @@ const Index = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container mx-auto p-4 space-y-4">
|
<div className="container mx-auto p-4 space-y-4">
|
||||||
{/* Stats Cards */}
|
|
||||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||||
<Card className="bg-gray-900 border-gray-700 text-white">
|
<Card className="bg-gray-900 border-gray-700 text-white">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
@@ -374,7 +314,6 @@ const Index = () => {
|
|||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Regular Jobs */}
|
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<div className="flex justify-between items-center">
|
<div className="flex justify-between items-center">
|
||||||
<h2 className="text-xl font-bold text-white">Jobs</h2>
|
<h2 className="text-xl font-bold text-white">Jobs</h2>
|
||||||
@@ -421,7 +360,7 @@ const Index = () => {
|
|||||||
{!collapsedGroups[status] && (
|
{!collapsedGroups[status] && (
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||||
{statusJobs.map(job => (
|
{statusJobs.map(job => (
|
||||||
<div key={job.id} onClick={() => setSelectedJob(job)} className="cursor-pointer">
|
<div key={job.id} onClick={() => setSelectedJobId(job.id)} className="cursor-pointer">
|
||||||
<JobCard
|
<JobCard
|
||||||
job={job}
|
job={job}
|
||||||
onEdit={handleEditJob}
|
onEdit={handleEditJob}
|
||||||
@@ -437,7 +376,6 @@ const Index = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Tracked Jobs */}
|
|
||||||
{trackedJobs.length > 0 && (
|
{trackedJobs.length > 0 && (
|
||||||
<div className="space-y-4 mt-8 pt-8 border-t border-gray-700">
|
<div className="space-y-4 mt-8 pt-8 border-t border-gray-700">
|
||||||
<div
|
<div
|
||||||
@@ -457,7 +395,7 @@ const Index = () => {
|
|||||||
{!collapsedGroups['Tracked'] && (
|
{!collapsedGroups['Tracked'] && (
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||||
{trackedJobs.map(job => (
|
{trackedJobs.map(job => (
|
||||||
<div key={job.id} onClick={() => setSelectedJob(job)} className="cursor-pointer">
|
<div key={job.id} onClick={() => setSelectedJobId(job.id)} className="cursor-pointer">
|
||||||
<JobCard
|
<JobCard
|
||||||
job={job}
|
job={job}
|
||||||
onEdit={handleEditJob}
|
onEdit={handleEditJob}
|
||||||
@@ -472,46 +410,6 @@ const Index = () => {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Job Form Modal */}
|
|
||||||
{showJobForm && (
|
|
||||||
<JobForm
|
|
||||||
job={editingJob}
|
|
||||||
onSubmit={editingJob ? handleUpdateJob : handleCreateJob}
|
|
||||||
onCancel={() => {
|
|
||||||
setShowJobForm(false);
|
|
||||||
setEditingJob(null);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Transaction Details */}
|
|
||||||
{selectedJob && (
|
|
||||||
<div className="mt-8 space-y-4">
|
|
||||||
<Card className="bg-gray-900 border-gray-700 text-white">
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle>Transactions for {selectedJob.outputItem}</CardTitle>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
<TransactionForm
|
|
||||||
jobId={selectedJob.id}
|
|
||||||
onTransactionsAdded={handleTransactionsAdded}
|
|
||||||
/>
|
|
||||||
<TransactionTable
|
|
||||||
title="Transactions"
|
|
||||||
transactions={[
|
|
||||||
...selectedJob.expenditures.map(tx => ({ ...tx, type: 'expenditure' as const })),
|
|
||||||
...selectedJob.income.map(tx => ({ ...tx, type: 'income' as const }))
|
|
||||||
]}
|
|
||||||
type="expenditure"
|
|
||||||
onUpdateTransaction={handleUpdateTransaction}
|
|
||||||
onDeleteTransaction={handleDeleteTransaction}
|
|
||||||
/>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Batch Transaction Form */}
|
|
||||||
{showBatchForm && (
|
{showBatchForm && (
|
||||||
<BatchTransactionForm
|
<BatchTransactionForm
|
||||||
jobs={jobs}
|
jobs={jobs}
|
||||||
|
261
src/services/dataService.ts
Normal file
261
src/services/dataService.ts
Normal file
@@ -0,0 +1,261 @@
|
|||||||
|
|
||||||
|
import { IndJob } from '@/lib/types';
|
||||||
|
import { IndJobRecord, IndJobRecordNoId, IndTransactionRecord, IndTransactionRecordNoId, IndBillitemRecord, IndBillitemRecordNoId } from '@/lib/pbtypes';
|
||||||
|
import * as jobService from './jobService';
|
||||||
|
import * as transactionService from './transactionService';
|
||||||
|
import * as billItemService from './billItemService';
|
||||||
|
|
||||||
|
export class DataService {
|
||||||
|
private static instance: DataService;
|
||||||
|
private jobs: IndJob[] = [];
|
||||||
|
private listeners: Set<() => void> = new Set();
|
||||||
|
|
||||||
|
private constructor() {}
|
||||||
|
|
||||||
|
static getInstance(): DataService {
|
||||||
|
if (!DataService.instance) {
|
||||||
|
DataService.instance = new DataService();
|
||||||
|
}
|
||||||
|
return DataService.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribe(listener: () => void) {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
return () => this.listeners.delete(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
private notifyListeners() {
|
||||||
|
this.listeners.forEach(listener => listener());
|
||||||
|
}
|
||||||
|
|
||||||
|
getJobs(): IndJob[] {
|
||||||
|
return [...this.jobs];
|
||||||
|
}
|
||||||
|
|
||||||
|
getJob(id: string): IndJob | null {
|
||||||
|
return this.jobs.find(job => job.id === id) || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadJobs(): Promise<IndJob[]> {
|
||||||
|
console.log('Loading jobs from database');
|
||||||
|
this.jobs = await jobService.getJobsFull();
|
||||||
|
this.notifyListeners();
|
||||||
|
return this.getJobs();
|
||||||
|
}
|
||||||
|
|
||||||
|
async createJob(jobData: IndJobRecordNoId): Promise<IndJob> {
|
||||||
|
console.log('Creating job:', jobData);
|
||||||
|
const newJobRecord = await jobService.createJob(jobData);
|
||||||
|
const newJob: IndJob = {
|
||||||
|
...newJobRecord,
|
||||||
|
expenditures: [],
|
||||||
|
income: [],
|
||||||
|
billOfMaterials: [],
|
||||||
|
consumedMaterials: []
|
||||||
|
};
|
||||||
|
|
||||||
|
this.jobs.push(newJob);
|
||||||
|
this.notifyListeners();
|
||||||
|
return newJob;
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateJob(id: string, updates: Partial<IndJobRecord>): Promise<IndJob> {
|
||||||
|
console.log('Updating job:', id, updates);
|
||||||
|
const updatedRecord = await jobService.updateJob(id, updates);
|
||||||
|
|
||||||
|
const jobIndex = this.jobs.findIndex(job => job.id === id);
|
||||||
|
if (jobIndex !== -1) {
|
||||||
|
// Preserve existing relations while updating the record fields
|
||||||
|
this.jobs[jobIndex] = {
|
||||||
|
...this.jobs[jobIndex],
|
||||||
|
...updatedRecord
|
||||||
|
};
|
||||||
|
this.notifyListeners();
|
||||||
|
return this.jobs[jobIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Job with id ${id} not found in local state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteJob(id: string): Promise<void> {
|
||||||
|
console.log('Deleting job:', id);
|
||||||
|
await jobService.deleteJob(id);
|
||||||
|
|
||||||
|
this.jobs = this.jobs.filter(job => job.id !== id);
|
||||||
|
this.notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
async createTransaction(jobId: string, transaction: IndTransactionRecordNoId, type: 'expenditure' | 'income'): Promise<IndJob> {
|
||||||
|
console.log('Creating transaction for job:', jobId, transaction, type);
|
||||||
|
|
||||||
|
const job = this.getJob(jobId);
|
||||||
|
if (!job) throw new Error(`Job with id ${jobId} not found`);
|
||||||
|
|
||||||
|
// Create the transaction in the database
|
||||||
|
transaction.job = jobId;
|
||||||
|
const createdTransaction = await transactionService.createTransaction(job, transaction, type);
|
||||||
|
|
||||||
|
// Update the job's transaction references in the database
|
||||||
|
const field = type === 'expenditure' ? 'expenditures' : 'income';
|
||||||
|
const currentIds = (job[field] || []).map(tr => tr.id);
|
||||||
|
await jobService.updateJob(jobId, {
|
||||||
|
[field]: [...currentIds, createdTransaction.id]
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update local state without re-fetching
|
||||||
|
const jobIndex = this.jobs.findIndex(j => j.id === jobId);
|
||||||
|
if (jobIndex !== -1) {
|
||||||
|
if (type === 'expenditure') {
|
||||||
|
this.jobs[jobIndex].expenditures.push(createdTransaction);
|
||||||
|
} else {
|
||||||
|
this.jobs[jobIndex].income.push(createdTransaction);
|
||||||
|
}
|
||||||
|
this.notifyListeners();
|
||||||
|
return this.jobs[jobIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Job with id ${jobId} not found in local state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async createMultipleTransactions(jobId: string, transactions: IndTransactionRecordNoId[], type: 'expenditure' | 'income'): Promise<IndJob> {
|
||||||
|
console.log('Creating multiple transactions for job:', jobId, transactions.length, type);
|
||||||
|
|
||||||
|
const job = this.getJob(jobId);
|
||||||
|
if (!job) throw new Error(`Job with id ${jobId} not found`);
|
||||||
|
|
||||||
|
const createdTransactions: IndTransactionRecord[] = [];
|
||||||
|
|
||||||
|
// Create all transactions
|
||||||
|
for (const transaction of transactions) {
|
||||||
|
transaction.job = jobId;
|
||||||
|
const createdTransaction = await transactionService.createTransaction(job, transaction, type);
|
||||||
|
createdTransactions.push(createdTransaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the job's transaction references in one database call
|
||||||
|
const field = type === 'expenditure' ? 'expenditures' : 'income';
|
||||||
|
const currentIds = (job[field] || []).map(tr => tr.id);
|
||||||
|
const newIds = createdTransactions.map(tr => tr.id);
|
||||||
|
await jobService.updateJob(jobId, {
|
||||||
|
[field]: [...currentIds, ...newIds]
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update local state
|
||||||
|
const jobIndex = this.jobs.findIndex(j => j.id === jobId);
|
||||||
|
if (jobIndex !== -1) {
|
||||||
|
if (type === 'expenditure') {
|
||||||
|
this.jobs[jobIndex].expenditures.push(...createdTransactions);
|
||||||
|
} else {
|
||||||
|
this.jobs[jobIndex].income.push(...createdTransactions);
|
||||||
|
}
|
||||||
|
this.notifyListeners();
|
||||||
|
return this.jobs[jobIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Job with id ${jobId} not found in local state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateTransaction(jobId: string, transactionId: string, updates: Partial<IndTransactionRecord>): Promise<IndJob> {
|
||||||
|
console.log('Updating transaction:', transactionId, updates);
|
||||||
|
|
||||||
|
const job = this.getJob(jobId);
|
||||||
|
if (!job) throw new Error(`Job with id ${jobId} not found`);
|
||||||
|
|
||||||
|
const updatedTransaction = await transactionService.updateTransaction(job, transactionId, updates);
|
||||||
|
|
||||||
|
// Update local state
|
||||||
|
const jobIndex = this.jobs.findIndex(j => j.id === jobId);
|
||||||
|
if (jobIndex !== -1) {
|
||||||
|
this.jobs[jobIndex].expenditures = this.jobs[jobIndex].expenditures.map(tx =>
|
||||||
|
tx.id === transactionId ? updatedTransaction : tx
|
||||||
|
);
|
||||||
|
this.jobs[jobIndex].income = this.jobs[jobIndex].income.map(tx =>
|
||||||
|
tx.id === transactionId ? updatedTransaction : tx
|
||||||
|
);
|
||||||
|
this.notifyListeners();
|
||||||
|
return this.jobs[jobIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Job with id ${jobId} not found in local state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteTransaction(jobId: string, transactionId: string): Promise<IndJob> {
|
||||||
|
console.log('Deleting transaction:', transactionId);
|
||||||
|
|
||||||
|
const job = this.getJob(jobId);
|
||||||
|
if (!job) throw new Error(`Job with id ${jobId} not found`);
|
||||||
|
|
||||||
|
await transactionService.deleteTransaction(job, transactionId);
|
||||||
|
|
||||||
|
// Update local state
|
||||||
|
const jobIndex = this.jobs.findIndex(j => j.id === jobId);
|
||||||
|
if (jobIndex !== -1) {
|
||||||
|
this.jobs[jobIndex].expenditures = this.jobs[jobIndex].expenditures.filter(tx => tx.id !== transactionId);
|
||||||
|
this.jobs[jobIndex].income = this.jobs[jobIndex].income.filter(tx => tx.id !== transactionId);
|
||||||
|
this.notifyListeners();
|
||||||
|
return this.jobs[jobIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Job with id ${jobId} not found in local state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async createBillItem(jobId: string, billItem: IndBillitemRecordNoId, type: 'billOfMaterials' | 'consumedMaterials'): Promise<IndJob> {
|
||||||
|
console.log('Creating bill item for job:', jobId, billItem, type);
|
||||||
|
|
||||||
|
const job = this.getJob(jobId);
|
||||||
|
if (!job) throw new Error(`Job with id ${jobId} not found`);
|
||||||
|
|
||||||
|
const createdBillItem = await billItemService.addBillItem(jobId, billItem);
|
||||||
|
|
||||||
|
// Update the job's bill item references
|
||||||
|
const currentIds = (job[type] || []).map(item => item.id);
|
||||||
|
await jobService.updateJob(jobId, {
|
||||||
|
[type]: [...currentIds, createdBillItem.id]
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update local state
|
||||||
|
const jobIndex = this.jobs.findIndex(j => j.id === jobId);
|
||||||
|
if (jobIndex !== -1) {
|
||||||
|
this.jobs[jobIndex][type].push(createdBillItem);
|
||||||
|
this.notifyListeners();
|
||||||
|
return this.jobs[jobIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Job with id ${jobId} not found in local state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async createMultipleBillItems(jobId: string, billItems: IndBillitemRecordNoId[], type: 'billOfMaterials' | 'consumedMaterials'): Promise<IndJob> {
|
||||||
|
console.log('Creating multiple bill items for job:', jobId, billItems.length, type);
|
||||||
|
|
||||||
|
const job = this.getJob(jobId);
|
||||||
|
if (!job) throw new Error(`Job with id ${jobId} not found`);
|
||||||
|
|
||||||
|
const createdBillItems: IndBillitemRecord[] = [];
|
||||||
|
|
||||||
|
// Create all bill items
|
||||||
|
for (const billItem of billItems) {
|
||||||
|
const createdBillItem = await billItemService.addBillItem(jobId, billItem);
|
||||||
|
createdBillItems.push(createdBillItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the job's bill item references in one database call
|
||||||
|
const currentIds = (job[type] || []).map(item => item.id);
|
||||||
|
const newIds = createdBillItems.map(item => item.id);
|
||||||
|
await jobService.updateJob(jobId, {
|
||||||
|
[type]: [...currentIds, ...newIds]
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update local state
|
||||||
|
const jobIndex = this.jobs.findIndex(j => j.id === jobId);
|
||||||
|
if (jobIndex !== -1) {
|
||||||
|
this.jobs[jobIndex][type].push(...createdBillItems);
|
||||||
|
this.notifyListeners();
|
||||||
|
return this.jobs[jobIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Job with id ${jobId} not found in local state`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export singleton instance
|
||||||
|
export const dataService = DataService.getInstance();
|
@@ -1,44 +1,29 @@
|
|||||||
|
|
||||||
import { IndJob } from '@/lib/types';
|
import { IndJob } from '@/lib/types';
|
||||||
import type { IndTransactionRecord, IndTransactionRecordNoId } from '../lib/pbtypes';
|
import type { IndTransactionRecord, IndTransactionRecordNoId } from '../lib/pbtypes';
|
||||||
import pb from '../lib/pocketbase';
|
import pb from '../lib/pocketbase';
|
||||||
import { getJob, updateJob } from './jobService';
|
import { updateJob } from './jobService';
|
||||||
|
|
||||||
export async function createTransaction(
|
export async function createTransaction(
|
||||||
job: IndJob,
|
job: IndJob,
|
||||||
transaction: IndTransactionRecordNoId,
|
transaction: IndTransactionRecordNoId,
|
||||||
type: 'expenditure' | 'income'
|
type: 'expenditure' | 'income'
|
||||||
): Promise<IndJob> {
|
): Promise<IndTransactionRecord> {
|
||||||
console.log('Creating 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);
|
||||||
|
return createdTransaction;
|
||||||
// Update the job to include the new transaction
|
|
||||||
const field = type === 'expenditure' ? 'expenditures' : 'income';
|
|
||||||
const currentIds = (job[field] || []).map(tr => tr.id);
|
|
||||||
|
|
||||||
await updateJob(job.id, {
|
|
||||||
[field]: [...currentIds, createdTransaction.id]
|
|
||||||
});
|
|
||||||
|
|
||||||
if (type === 'expenditure') {
|
|
||||||
job.expenditures.push(createdTransaction);
|
|
||||||
} else {
|
|
||||||
job.income.push(createdTransaction);
|
|
||||||
}
|
|
||||||
return job;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateTransaction(
|
export async function updateTransaction(
|
||||||
job: IndJob,
|
job: IndJob,
|
||||||
transactionId: string,
|
transactionId: string,
|
||||||
updates: Partial<IndTransactionRecord>
|
updates: Partial<IndTransactionRecord>
|
||||||
): Promise<IndJob> {
|
): Promise<IndTransactionRecord> {
|
||||||
console.log('Updating transaction:', transactionId, updates);
|
console.log('Updating transaction:', transactionId, updates);
|
||||||
const updatedTransaction = await pb.collection<IndTransactionRecord>('ind_transaction').update(transactionId, updates);
|
const updatedTransaction = await pb.collection<IndTransactionRecord>('ind_transaction').update(transactionId, updates);
|
||||||
job.expenditures = job.expenditures.map(exp => exp.id === transactionId ? updatedTransaction : exp);
|
return updatedTransaction;
|
||||||
job.income = job.income.map(inc => inc.id === transactionId ? updatedTransaction : inc);
|
|
||||||
return job;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteTransaction(job: IndJob, transactionId: string): Promise<void> {
|
export async function deleteTransaction(job: IndJob, transactionId: string): Promise<void> {
|
||||||
|
Reference in New Issue
Block a user