From 024c5e0a1c7449bc1e7e410be28a138dd09351f4 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Mon, 28 Feb 2022 13:03:53 +0000 Subject: [PATCH] Refactor transactions store --- web/src/stores/budget-account.ts | 30 +++++++++++++++++------------- web/src/stores/budget.ts | 9 ++++++--- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/web/src/stores/budget-account.ts b/web/src/stores/budget-account.ts index 9ae89b1..9a3f8f1 100644 --- a/web/src/stores/budget-account.ts +++ b/web/src/stores/budget-account.ts @@ -137,18 +137,22 @@ export const useAccountStore = defineStore("budget/account", { useSessionStore().setTitle(account.Name); await this.FetchAccount(account); }, - AddTransaction(account: Account, transaction: any) { - transaction.Date = new Date(transaction.Date); - this.Transactions.set(transaction.ID, transaction); + AddTransactions(transactions: Array) { + const transactionIds = [] as Array; + this.$patch(() => { + for (const transaction of transactions) { + transaction.Date = new Date(transaction.Date); + this.Transactions.set(transaction.ID, transaction); + transactionIds.push(transaction.ID); + } + }); + return transactionIds; }, async FetchAccount(account: Account) { const result = await GET("/account/" + account.ID + "/transactions"); const response = await result.json(); - account.Transactions = []; - for (const transaction of response.Transactions) { - this.AddTransaction(account, transaction); - account.Transactions.push(transaction.ID); - } + const transactions = this.AddTransactions(response.Transactions); + account.Transactions = transactions; }, async FetchMonthBudget(budgetid: string, year: number, month: number) { const result = await GET("/budget/" + budgetid + "/" + year + "/" + month); @@ -197,7 +201,7 @@ export const useAccountStore = defineStore("budget/account", { const response = await result.json(); const recTrans = response.ReconciliationTransaction; if (recTrans) { - this.AddTransaction(account, recTrans); + this.AddTransactions([recTrans]); account.Transactions.unshift(recTrans.ID); } console.log("Reconcile: " + response.message); @@ -207,14 +211,14 @@ export const useAccountStore = defineStore("budget/account", { }, async saveTransaction(payload: string) { const result = await POST("/transaction/new", payload); - const response = await result.json(); - this.AddTransaction(this.CurrentAccount!, response); + const response = await result.json() as Transaction; + this.AddTransactions([response]); this.CurrentAccount?.Transactions.unshift(response.ID); }, async editTransaction(transactionid: string, payload: string) { const result = await POST("/transaction/" + transactionid, payload); - const response = await result.json(); - this.AddTransaction(this.CurrentAccount!, response); + const response = await result.json() as Transaction; + this.AddTransactions([response]); } } diff --git a/web/src/stores/budget.ts b/web/src/stores/budget.ts index 919996f..c9b1295 100644 --- a/web/src/stores/budget.ts +++ b/web/src/stores/budget.ts @@ -53,12 +53,15 @@ export const useBudgetsStore = defineStore('budget', { const response = await result.json(); this.MergeBudgetingData(response); }, - MergeBudgetingData(response : any) { + MergeBudgetingData(response: any) { + const accounts = useAccountStore(); for (const account of response.Accounts || []) { - useAccountStore().Accounts.set(account.ID, account); + const existingAccount = accounts.Accounts.get(account.ID); + account.Transactions = existingAccount?.Transactions ?? []; + accounts.Accounts.set(account.ID, account); } for (const category of response.Categories || []) { - useAccountStore().Categories.set(category.ID, category); + accounts.Categories.set(category.ID, category); } }, }