-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactions.js
More file actions
157 lines (143 loc) · 4.38 KB
/
transactions.js
File metadata and controls
157 lines (143 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import fetchWithAuth from "../utils/fetch";
export const useTransactionsStore = defineStore("transactions", () => {
// State
const transactions = ref([]);
// Getters
const transactionsByDate = computed(() => {
return [...transactions.value].sort((a, b) => {
return new Date(a.date) - new Date(b.date);
});
});
// Reset state method to clear transactions when switching books
function resetState() {
transactions.value = [];
}
// Actions
async function fetchTransactionById(id) {
try {
const response = await fetchWithAuth(`/api/transactions/${id}`);
if (!response.ok) {
const json = await response.json();
throw new Error(json.error);
}
const transaction = await response.json();
// Update transaction in local state if exists
const index = transactions.value.findIndex((t) => t.id === id);
if (index !== -1) {
transactions.value[index] = transaction;
} else {
transactions.value.push(transaction);
}
return transaction;
} catch (error) {
console.error("Error fetching transaction:", error);
throw error;
}
}
async function fetchTransactionsByBook(
bookId,
{
page = 1,
limit = 1000,
sortKey = "date",
sortDirection = "asc",
accountId = null,
search = "",
startDate = null,
endDate = null,
} = {}
) {
try {
const params = new URLSearchParams({
page,
limit,
sortKey,
sortDirection,
});
if (accountId) params.append("account_id", accountId); // Here it's converted to account_id
if (search) params.append("search", search);
if (startDate) params.append("start_date", startDate); // Here it's converted to start_date
if (endDate) params.append("end_date", endDate); // Here it's converted to end_date
if (!bookId) throw new Error("bookId is required");
// Updated endpoint URL to use the unified /books/:id/transactions endpoint
const url = `/api/books/${bookId}/transactions?${params.toString()}`;
const response = await fetchWithAuth(url);
if (!response.ok) {
const json = await response.json();
throw new Error(json.error);
}
const data = await response.json();
// Update local state with fetched transactions
transactions.value = data.transactions;
return { transactions: data.transactions, total: data.total };
} catch (error) {
console.error("Error fetching transactions by book:", error);
throw error;
}
}
async function addTransaction(transaction) {
try {
const response = await fetchWithAuth("/api/transactions", {
method: "POST",
body: JSON.stringify(transaction),
});
const newTransaction = await response.json();
transactions.value.push(newTransaction);
return newTransaction;
} catch (error) {
console.error("Error adding transaction:", error);
throw error;
}
}
async function updateTransaction(id, updates) {
try {
const response = await fetchWithAuth(`/api/transactions/${id}`, {
method: "PUT",
body: JSON.stringify(updates),
});
const updatedTransaction = await response.json();
const index = transactions.value.findIndex((t) => t.id === id);
if (index !== -1) {
transactions.value[index] = updatedTransaction;
}
return updatedTransaction;
} catch (error) {
console.error("Error updating transaction:", error);
throw error;
}
}
async function deleteTransaction(id) {
try {
const response = await fetchWithAuth(`/api/transactions/${id}`, {
method: "DELETE",
});
if (!response.ok) {
const json = await response.json();
throw new Error(json.error);
}
const index = transactions.value.findIndex((t) => t.id === id);
if (index !== -1) {
transactions.value.splice(index, 1);
}
} catch (error) {
console.error("Error deleting transaction:", error);
throw error;
}
}
return {
// State
transactions,
// Getters
transactionsByDate,
// Reset method
resetState,
// Actions
fetchTransactionById,
fetchTransactionsByBook,
addTransaction,
updateTransaction,
deleteTransaction,
};
});