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 { Label } from '@/components/ui/label';
|
||||
import { Import, Download, FileText } from 'lucide-react';
|
||||
import { IndBillitemRecord } from '@/lib/pbtypes';
|
||||
import { IndBillitemRecord, IndBillitemRecordNoId } from '@/lib/pbtypes';
|
||||
import { IndJob } from '@/lib/types';
|
||||
import { addBillItem } from '@/services/billItemService';
|
||||
import { updateJob } from '@/services/jobService';
|
||||
import { dataService } from '@/services/dataService';
|
||||
|
||||
interface MaterialsImportExportProps {
|
||||
job: IndJob;
|
||||
job?: IndJob;
|
||||
billOfMaterials: IndBillitemRecord[];
|
||||
consumedMaterials: IndBillitemRecord[];
|
||||
onBillOfMaterialsUpdate: (billItems: IndBillitemRecord[]) => void;
|
||||
@@ -27,9 +26,9 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
||||
const [bomInput, setBomInput] = 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 materials: IndBillitemRecord[] = [];
|
||||
const materials: IndBillitemRecordNoId[] = [];
|
||||
|
||||
for (const line of lines) {
|
||||
const parts = line.trim().split(/\s+/);
|
||||
@@ -37,20 +36,17 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
||||
const name = parts.slice(0, -1).join(' ');
|
||||
const quantity = parseInt(parts[parts.length - 1]);
|
||||
if (name && !isNaN(quantity)) {
|
||||
const newBillItem = await addBillItem(job.id, { name, quantity });
|
||||
materials.push(newBillItem);
|
||||
materials.push({ name, quantity });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
job.billOfMaterials = materials;
|
||||
await updateJob(job.id, { billOfMaterials: materials.map(item => item.id) });
|
||||
return job;
|
||||
return materials;
|
||||
};
|
||||
|
||||
const parseConsumedMaterials = async (text: string): Promise<IndJob> => {
|
||||
const parseConsumedMaterials = (text: string): IndBillitemRecordNoId[] => {
|
||||
const lines = text.split('\n').filter(line => line.trim());
|
||||
const materials: IndBillitemRecord[] = [];
|
||||
const materials: IndBillitemRecordNoId[] = [];
|
||||
|
||||
for (const line of lines) {
|
||||
const parts = line.trim().split('\t');
|
||||
@@ -58,15 +54,12 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
||||
const name = parts[0];
|
||||
const quantity = parseInt(parts[1]);
|
||||
if (name && !isNaN(quantity)) {
|
||||
const newBillItem = await addBillItem(job.id, { name, quantity });
|
||||
materials.push(newBillItem);
|
||||
materials.push({ name, quantity });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
job.consumedMaterials = materials;
|
||||
await updateJob(job.id, { consumedMaterials: materials.map(item => item.id) });
|
||||
return job;
|
||||
return materials;
|
||||
};
|
||||
|
||||
const exportBillOfMaterials = (): string => {
|
||||
@@ -78,15 +71,33 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
||||
};
|
||||
|
||||
const handleImportBom = async () => {
|
||||
const parsed = await parseBillOfMaterials(bomInput);
|
||||
onBillOfMaterialsUpdate(parsed.billOfMaterials);
|
||||
setBomInput('');
|
||||
if (!job) return;
|
||||
|
||||
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 parsed = await parseConsumedMaterials(consumedInput);
|
||||
onConsumedMaterialsUpdate(parsed.consumedMaterials);
|
||||
setConsumedInput('');
|
||||
if (!job) return;
|
||||
|
||||
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 = () => {
|
||||
@@ -131,6 +142,7 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
||||
<Button
|
||||
onClick={handleImportBom}
|
||||
className="bg-blue-600 hover:bg-blue-700"
|
||||
disabled={!job}
|
||||
>
|
||||
<Import className="w-4 h-4 mr-2" />
|
||||
Import Bill of Materials
|
||||
@@ -160,6 +172,7 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
||||
<Button
|
||||
onClick={handleImportConsumed}
|
||||
className="bg-blue-600 hover:bg-blue-700"
|
||||
disabled={!job}
|
||||
>
|
||||
<Import className="w-4 h-4 mr-2" />
|
||||
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 { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Plus, Factory, TrendingUp, Briefcase, FileText } from 'lucide-react';
|
||||
import { IndTransactionRecordNoId, IndJobRecordNoId, IndTransactionRecord, IndJobStatusOptions } from '@/lib/pbtypes';
|
||||
import * as jobService from '@/services/jobService';
|
||||
import * as transactionService from '@/services/transactionService';
|
||||
import { IndTransactionRecordNoId, IndJobRecordNoId, IndJobStatusOptions } from '@/lib/pbtypes';
|
||||
import { formatISK } from '@/utils/priceUtils';
|
||||
import JobCard from '@/components/JobCard';
|
||||
import JobForm from '@/components/JobForm';
|
||||
import TransactionForm from '@/components/TransactionForm';
|
||||
import TransactionTable from '@/components/TransactionTable';
|
||||
import { IndJob } from '@/lib/types';
|
||||
import { createJob } from '@/services/jobService';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
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 [jobs, setJobs] = useState<IndJob[]>([]);
|
||||
const {
|
||||
jobs,
|
||||
loading,
|
||||
error,
|
||||
createJob,
|
||||
updateJob,
|
||||
deleteJob,
|
||||
createMultipleTransactions,
|
||||
updateTransaction,
|
||||
deleteTransaction
|
||||
} = useJobs();
|
||||
|
||||
const [showJobForm, setShowJobForm] = useState(false);
|
||||
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);
|
||||
|
||||
useEffect(() => {
|
||||
loadJobs();
|
||||
}, []);
|
||||
const selectedJob = useJob(selectedJobId);
|
||||
|
||||
const loadJobs = async () => {
|
||||
try {
|
||||
const fetchedJobs = await jobService.getJobsFull();
|
||||
// Convert to JobWithRelations format
|
||||
const jobsWithRelations: IndJob[] = fetchedJobs;
|
||||
setJobs(jobsWithRelations);
|
||||
} catch (error) {
|
||||
console.error('Error loading jobs:', error);
|
||||
}
|
||||
};
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-950 p-6 flex items-center justify-center">
|
||||
<div className="text-white">Loading jobs...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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 => {
|
||||
switch (status) {
|
||||
case 'Planned': return 6;
|
||||
@@ -48,7 +57,7 @@ const Index = () => {
|
||||
case 'Done': return 3;
|
||||
case 'Selling': return 4;
|
||||
case 'Closed': return 5;
|
||||
case 'Tracked': return 7; // Put tracked jobs at the end
|
||||
case 'Tracked': return 7;
|
||||
default: return 0;
|
||||
}
|
||||
};
|
||||
@@ -70,17 +79,14 @@ const Index = () => {
|
||||
const priorityA = getStatusPriority(a.status);
|
||||
const priorityB = getStatusPriority(b.status);
|
||||
if (priorityA === priorityB) {
|
||||
// If same status, sort by creation date (newest first)
|
||||
return new Date(b.created || '').getTime() - new Date(a.created || '').getTime();
|
||||
}
|
||||
return priorityA - priorityB;
|
||||
});
|
||||
|
||||
// Separate regular and tracked jobs
|
||||
const regularJobs = sortedJobs.filter(job => job.status !== 'Tracked');
|
||||
const trackedJobs = sortedJobs.filter(job => job.status === 'Tracked');
|
||||
|
||||
// Calculate totals excluding tracked jobs
|
||||
const totalJobs = regularJobs.length;
|
||||
const totalProfit = regularJobs.reduce((sum, job) => {
|
||||
const expenditure = job.expenditures.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
||||
@@ -94,15 +100,7 @@ const Index = () => {
|
||||
|
||||
const handleCreateJob = async (jobData: IndJobRecordNoId) => {
|
||||
try {
|
||||
const newJob = await createJob(jobData);
|
||||
const jobWithRelations: IndJob = {
|
||||
...newJob,
|
||||
expenditures: [],
|
||||
income: [],
|
||||
billOfMaterials: [],
|
||||
consumedMaterials: []
|
||||
};
|
||||
setJobs([...jobs, jobWithRelations]);
|
||||
await createJob(jobData);
|
||||
setShowJobForm(false);
|
||||
} catch (error) {
|
||||
console.error('Error creating job:', error);
|
||||
@@ -118,22 +116,9 @@ const Index = () => {
|
||||
if (!editingJob) return;
|
||||
|
||||
try {
|
||||
const updatedJob = await jobService.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));
|
||||
await updateJob(editingJob.id, jobData);
|
||||
setShowJobForm(false);
|
||||
setEditingJob(null);
|
||||
|
||||
if (selectedJob?.id === editingJob.id) {
|
||||
setSelectedJob(updatedJobWithRelations);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating job:', error);
|
||||
}
|
||||
@@ -142,10 +127,9 @@ const Index = () => {
|
||||
const handleDeleteJob = async (jobId: string) => {
|
||||
if (confirm('Are you sure you want to delete this job?')) {
|
||||
try {
|
||||
await jobService.deleteJob(jobId);
|
||||
setJobs(jobs.filter(job => job.id !== jobId));
|
||||
if (selectedJob?.id === jobId) {
|
||||
setSelectedJob(null);
|
||||
await deleteJob(jobId);
|
||||
if (selectedJobId === jobId) {
|
||||
setSelectedJobId(null);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting job:', error);
|
||||
@@ -154,76 +138,43 @@ const Index = () => {
|
||||
};
|
||||
|
||||
const handleTransactionsAdded = async (transactions: IndTransactionRecordNoId[], type: 'expenditure' | 'income') => {
|
||||
if (!selectedJob) return;
|
||||
if (!selectedJobId) return;
|
||||
|
||||
try {
|
||||
let updatedJob = selectedJob;
|
||||
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));
|
||||
await createMultipleTransactions(selectedJobId, transactions, type);
|
||||
} catch (error) {
|
||||
console.error('Error adding transactions:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleUpdateTransaction = async (transactionId: string, updates: Partial<IndTransactionRecord>) => {
|
||||
if (!selectedJob) return;
|
||||
if (!selectedJobId) return;
|
||||
|
||||
try {
|
||||
let updatedJob = selectedJob;
|
||||
updatedJob = await transactionService.updateTransaction(updatedJob, transactionId, updates);
|
||||
// Update local state
|
||||
setSelectedJob(updatedJob);
|
||||
setJobs(jobs.map(job => job.id === selectedJob.id ? updatedJob : job));
|
||||
await updateTransaction(selectedJobId, transactionId, updates);
|
||||
} catch (error) {
|
||||
console.error('Error updating transaction:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteTransaction = async (transactionId: string) => {
|
||||
if (!selectedJob) return;
|
||||
if (!selectedJobId) return;
|
||||
|
||||
try {
|
||||
await transactionService.deleteTransaction(selectedJob, 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));
|
||||
await deleteTransaction(selectedJobId, transactionId);
|
||||
} catch (error) {
|
||||
console.error('Error deleting transaction:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleUpdateProduced = async (jobId: string, produced: number) => {
|
||||
if (!selectedJob) return;
|
||||
|
||||
try {
|
||||
const updatedJob = await jobService.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));
|
||||
await updateJob(jobId, { produced });
|
||||
} catch (error) {
|
||||
console.error('Error updating produced quantity:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// Group jobs by status
|
||||
const jobGroups = regularJobs.reduce((groups, job) => {
|
||||
const status = job.status;
|
||||
if (!groups[status]) {
|
||||
@@ -233,13 +184,11 @@ const Index = () => {
|
||||
return groups;
|
||||
}, {} as Record<string, IndJob[]>);
|
||||
|
||||
// Load collapsed state from localStorage
|
||||
const [collapsedGroups, setCollapsedGroups] = useState<Record<string, boolean>>(() => {
|
||||
const saved = localStorage.getItem('jobGroupsCollapsed');
|
||||
return saved ? JSON.parse(saved) : {};
|
||||
});
|
||||
|
||||
// Toggle group collapse
|
||||
const toggleGroup = (status: string) => {
|
||||
const newState = { ...collapsedGroups, [status]: !collapsedGroups[status] };
|
||||
setCollapsedGroups(newState);
|
||||
@@ -249,15 +198,7 @@ const Index = () => {
|
||||
const handleBatchTransactionsAssigned = async (assignments: { jobId: string, transactions: IndTransactionRecordNoId[] }[]) => {
|
||||
try {
|
||||
for (const { jobId, transactions } of assignments) {
|
||||
const job = jobs.find(j => j.id === jobId);
|
||||
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));
|
||||
}
|
||||
await createMultipleTransactions(jobId, transactions, 'income');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error assigning batch transactions:', error);
|
||||
@@ -292,7 +233,7 @@ const Index = () => {
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setSelectedJob(null)}
|
||||
onClick={() => setSelectedJobId(null)}
|
||||
className="border-gray-600 hover:bg-gray-800"
|
||||
>
|
||||
Back to Jobs
|
||||
@@ -335,7 +276,6 @@ const Index = () => {
|
||||
|
||||
return (
|
||||
<div className="container mx-auto p-4 space-y-4">
|
||||
{/* Stats Cards */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||
<Card className="bg-gray-900 border-gray-700 text-white">
|
||||
<CardHeader>
|
||||
@@ -374,7 +314,6 @@ const Index = () => {
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Regular Jobs */}
|
||||
<div className="space-y-4">
|
||||
<div className="flex justify-between items-center">
|
||||
<h2 className="text-xl font-bold text-white">Jobs</h2>
|
||||
@@ -421,7 +360,7 @@ const Index = () => {
|
||||
{!collapsedGroups[status] && (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{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
|
||||
job={job}
|
||||
onEdit={handleEditJob}
|
||||
@@ -437,7 +376,6 @@ const Index = () => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Tracked Jobs */}
|
||||
{trackedJobs.length > 0 && (
|
||||
<div className="space-y-4 mt-8 pt-8 border-t border-gray-700">
|
||||
<div
|
||||
@@ -457,7 +395,7 @@ const Index = () => {
|
||||
{!collapsedGroups['Tracked'] && (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{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
|
||||
job={job}
|
||||
onEdit={handleEditJob}
|
||||
@@ -472,46 +410,6 @@ const Index = () => {
|
||||
</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 && (
|
||||
<BatchTransactionForm
|
||||
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 type { IndTransactionRecord, IndTransactionRecordNoId } from '../lib/pbtypes';
|
||||
import pb from '../lib/pocketbase';
|
||||
import { getJob, updateJob } from './jobService';
|
||||
import { updateJob } from './jobService';
|
||||
|
||||
export async function createTransaction(
|
||||
job: IndJob,
|
||||
transaction: IndTransactionRecordNoId,
|
||||
type: 'expenditure' | 'income'
|
||||
): Promise<IndJob> {
|
||||
): Promise<IndTransactionRecord> {
|
||||
console.log('Creating transaction:', transaction);
|
||||
// Create the transaction
|
||||
transaction.job = job.id;
|
||||
const createdTransaction = await pb.collection<IndTransactionRecord>('ind_transaction').create(transaction);
|
||||
|
||||
// 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;
|
||||
return createdTransaction;
|
||||
}
|
||||
|
||||
export async function updateTransaction(
|
||||
job: IndJob,
|
||||
transactionId: string,
|
||||
updates: Partial<IndTransactionRecord>
|
||||
): Promise<IndJob> {
|
||||
): Promise<IndTransactionRecord> {
|
||||
console.log('Updating transaction:', transactionId, updates);
|
||||
const updatedTransaction = await pb.collection<IndTransactionRecord>('ind_transaction').update(transactionId, updates);
|
||||
job.expenditures = job.expenditures.map(exp => exp.id === transactionId ? updatedTransaction : exp);
|
||||
job.income = job.income.map(inc => inc.id === transactionId ? updatedTransaction : inc);
|
||||
return job;
|
||||
return updatedTransaction;
|
||||
}
|
||||
|
||||
export async function deleteTransaction(job: IndJob, transactionId: string): Promise<void> {
|
||||
|
Reference in New Issue
Block a user