-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooks.js
More file actions
498 lines (418 loc) · 13.7 KB
/
books.js
File metadata and controls
498 lines (418 loc) · 13.7 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import fetchWithAuth from '../utils/fetch';
import { useAccountsStore } from './accounts';
import { useCategoriesStore } from './categories';
import { useTransactionsStore } from './transactions';
import { useUsersStore } from './users';
import { useTeamsStore } from './teams';
export const useBooksStore = defineStore('books', () => {
// State
const currentBook = ref(null);
const isLoading = ref(false);
const error = ref(null);
const currentBookAccounts = ref([]);
// Getters
const hasAdminPermission = computed(() => {
if (!currentBook.value) return false;
const usersStore = useUsersStore();
const teamsStore = useTeamsStore();
const currentUser = usersStore.currentUser;
// Check if user has admin role in the current team
const userEntry = teamsStore.currentTeamUsers.find(
entry => entry.id === currentUser?.id
);
return userEntry?.role === 'admin';
});
// General write permission - for users who can edit (admin or collaborator)
// This determines if user can add/edit categories and transactions
const hasWritePermission = computed(() => {
if (!currentBook.value) return false;
const usersStore = useUsersStore();
const teamsStore = useTeamsStore();
const currentUser = usersStore.currentUser;
// Admin in this team has write permissions
if (hasAdminPermission.value) return true;
// Check if user exists in the current team
const userEntry = teamsStore.currentTeamUsers.find(
entry => entry.id === currentUser?.id
);
return userEntry?.role === 'admin' || userEntry?.role === 'collaborator';
});
// Accounts computed properties
const accountsByName = computed(() => {
return currentBookAccounts.value.sort((a, b) =>
a.name.localeCompare(b.name)
);
});
const getAccountById = computed(() => {
return (id) => currentBookAccounts.value.find(a => a.id === id);
});
const totalBalance = computed(() => {
return currentBookAccounts.value.reduce((sum, account) => {
return sum + (account.type === 'debit' ? account.starting_amount : -account.starting_amount);
}, 0);
});
// Actions
async function fetchBookById(id) {
isLoading.value = true;
error.value = null;
try {
const response = await fetchWithAuth(`/api/books/${id}`);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to fetch book');
}
const book = await response.json();
// Update in teams store books list if exists
const teamsStore = useTeamsStore();
const index = teamsStore.currentTeamBooks.findIndex(w => w.id === parseInt(id));
if (index !== -1) {
teamsStore.currentTeamBooks[index] = book;
} else {
teamsStore.currentTeamBooks.push(book);
}
return book;
} catch (err) {
console.error('Error fetching book:', err);
error.value = err.message;
throw err;
} finally {
isLoading.value = false;
}
}
async function createBook(data) {
isLoading.value = true;
error.value = null;
try {
const usersStore = useUsersStore();
const currentTeam = usersStore.currentTeam;
if (!currentTeam) {
throw new Error('No team selected');
}
// Add teamId to the data
const bookData = {
...data,
team_id: currentTeam.id
};
const response = await fetchWithAuth('/api/books', {
method: 'POST',
body: JSON.stringify(bookData),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to create book');
}
const newBook = await response.json();
// Add to teams store books list
const teamsStore = useTeamsStore();
teamsStore.currentTeamBooks.push(newBook);
return newBook;
} catch (err) {
console.error('Error creating book:', err);
error.value = err.message;
throw err;
} finally {
isLoading.value = false;
}
}
async function updateBook(id, updates) {
isLoading.value = true;
error.value = null;
try {
const response = await fetchWithAuth(`/api/books/${id}`, {
method: 'PUT',
body: JSON.stringify(updates),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to update book');
}
const updatedBook = await response.json();
// Update in teams store books list
const teamsStore = useTeamsStore();
const index = teamsStore.currentTeamBooks.findIndex(w => w.id === parseInt(id));
if (index !== -1) {
teamsStore.currentTeamBooks[index] = updatedBook;
}
// Update current book if it's the one being edited
if (currentBook.value && currentBook.value.id === parseInt(id)) {
currentBook.value = updatedBook;
}
return updatedBook;
} catch (err) {
console.error('Error updating book:', err);
error.value = err.message;
throw err;
} finally {
isLoading.value = false;
}
}
async function deleteBook(id) {
isLoading.value = true;
error.value = null;
try {
const response = await fetchWithAuth(`/api/books/${id}`, {
method: 'DELETE',
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to delete book');
}
// Remove from teams store books list
const teamsStore = useTeamsStore();
const index = teamsStore.currentTeamBooks.findIndex(w => w.id === parseInt(id));
if (index !== -1) {
teamsStore.currentTeamBooks.splice(index, 1);
}
// Reset current book if it's the one being deleted
if (currentBook.value && currentBook.value.id === parseInt(id)) {
currentBook.value = teamsStore.currentTeamBooks.length > 0 ? teamsStore.currentTeamBooks[0] : null;
}
} catch (err) {
console.error('Error deleting book:', err);
error.value = err.message;
throw err;
} finally {
isLoading.value = false;
}
}
// Accounts management functions
/**
* Fetch accounts for the current book
* @returns {Promise<Array>} List of accounts
* @throws {Error} If no current book or fetch fails
*/
async function fetchBookAccounts() {
try {
if (!currentBook.value) {
throw new Error('No book selected');
}
const response = await fetchWithAuth(`/api/books/${currentBook.value.id}/accounts`);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to fetch accounts');
}
const data = await response.json();
currentBookAccounts.value = data;
return data;
} catch (error) {
console.error('Error fetching accounts:', error);
throw error;
}
}
/**
* Add a new account to the current book
* @param {Object} account - The account data to create
* @param {string} account.name - The account name
* @param {string} account.type - The account type (debit/credit)
* @param {number} account.starting_amount - The starting balance
* @returns {Promise<Object>} The created account
* @throws {Error} If no current book or account data is invalid or creation fails
*/
async function addAccountToBook(account) {
try {
if (!currentBook.value) {
throw new Error('No book selected');
}
// Add book_id to the account data
const accountData = {
...account,
book_id: currentBook.value.id
};
// Use accounts store for actual creation
const accountsStore = useAccountsStore();
const newAccount = await accountsStore.createAccount(accountData);
// Add to current book accounts
currentBookAccounts.value.push(newAccount);
return newAccount;
} catch (error) {
console.error('Error adding account to book:', error);
throw error;
}
}
/**
* Update an account in the current book
* @param {number} accountId - The account ID to update
* @param {Object} updates - The fields to update
* @returns {Promise<Object>} The updated account
* @throws {Error} If update fails
*/
async function updateAccountInBook(accountId, updates) {
try {
// Use accounts store for actual update
const accountsStore = useAccountsStore();
const updatedAccount = await accountsStore.updateAccount(accountId, updates);
// Update the account in current book accounts
const index = currentBookAccounts.value.findIndex(a => a.id === accountId);
if (index !== -1) {
currentBookAccounts.value[index] = updatedAccount;
}
return updatedAccount;
} catch (error) {
console.error('Error updating account in book:', error);
throw error;
}
}
// Other book management functions
async function restoreBook(id) {
isLoading.value = true;
error.value = null;
try {
const response = await fetchWithAuth(`/api/books/${id}/restore`, {
method: 'POST',
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to restore book');
}
const restoredBook = await response.json();
// Add to teams store books list
const teamsStore = useTeamsStore();
teamsStore.currentTeamBooks.push(restoredBook);
return restoredBook;
} catch (err) {
console.error('Error restoring book:', err);
error.value = err.message;
throw err;
} finally {
isLoading.value = false;
}
}
// Internal method for setting current book and loading all dependent data
async function setCurrentBook(book) {
// Reset all stores first to clear previous book data
resetBookData();
// Set current book
currentBook.value = book;
// Return early if no book was provided
if (!book) return null;
// Load all dependent data for this book
isLoading.value = true;
error.value = null;
try {
// Get stores
const categoriesStore = useCategoriesStore();
const teamsStore = useTeamsStore();
// Load team users for permission checks
await teamsStore.fetchTeamUsers(book.team_id);
// Load data in parallel
await Promise.all([
fetchBookAccounts(), // Fetch accounts from this store
categoriesStore.fetchCategories(book.id)
]);
return book;
} catch (err) {
console.error('Error loading book data:', err);
error.value = 'Failed to load book data: ' + err.message;
throw err;
} finally {
isLoading.value = false;
}
}
// Function to reset all book-related data
function resetBookData() {
const categoriesStore = useCategoriesStore();
const transactionsStore = useTransactionsStore();
// Reset accounts in this store
currentBookAccounts.value = [];
// Reset other stores
categoriesStore.resetState();
transactionsStore.resetState();
}
// Loads a book by ID with validation and all dependent data
async function loadBookById(bookId) {
isLoading.value = true;
error.value = null;
try {
const usersStore = useUsersStore();
const teamsStore = useTeamsStore();
const currentTeam = usersStore.currentTeam;
if (!currentTeam) {
throw new Error('No team selected');
}
// Ensure books are loaded in teams store
if (teamsStore.currentTeamBooks.length === 0) {
await teamsStore.fetchTeamBooks(currentTeam.id);
}
// Check if book exists in the team's books
const bookExists = teamsStore.getBookById(bookId);
if (!bookExists) {
error.value = 'Book not found';
return { success: false, error: 'invalid-book' };
}
// Fetch the latest book data from the API to ensure freshness
const book = await fetchBookById(bookId);
// Set as current book and load all its dependent data
await setCurrentBook(book);
return { success: true, book };
} catch (err) {
console.error('Error loading book:', err);
error.value = err.message;
return { success: false, error: 'book-error' };
} finally {
isLoading.value = false;
}
}
/**
* Saves book data (create or update based on presence of ID)
* @param {Object} bookData - Book data to save
* @returns {Promise<Object>} The saved book
*/
async function saveBook(bookData) {
try {
isLoading.value = true;
error.value = null;
let savedBook;
if (bookData.id) {
// Update existing book
savedBook = await updateBook(bookData.id, {
name: bookData.name,
note: bookData.note,
currency_symbol: bookData.currency_symbol,
week_start: bookData.week_start
});
} else {
// Create new book
savedBook = await createBook({
name: bookData.name,
note: bookData.note,
currency_symbol: bookData.currency_symbol,
week_start: bookData.week_start
});
}
return savedBook;
} catch (error) {
error.value = error.message;
throw error;
} finally {
isLoading.value = false;
}
}
// Actions
return {
// State
currentBook,
isLoading,
error,
currentBookAccounts,
// Computed properties
hasAdminPermission,
hasWritePermission,
accountsByName,
getAccountById,
totalBalance,
// Actions
fetchBookById,
createBook,
updateBook,
deleteBook,
restoreBook,
resetBookData,
loadBookById,
saveBook,
fetchBookAccounts,
addAccountToBook,
updateAccountInBook
};
});