Refactor transactions store

This commit is contained in:
Jan Bader 2022-02-28 13:03:53 +00:00
parent 27372199f7
commit 024c5e0a1c
2 changed files with 23 additions and 16 deletions

View File

@ -137,18 +137,22 @@ export const useAccountStore = defineStore("budget/account", {
useSessionStore().setTitle(account.Name); useSessionStore().setTitle(account.Name);
await this.FetchAccount(account); await this.FetchAccount(account);
}, },
AddTransaction(account: Account, transaction: any) { AddTransactions(transactions: Array<Transaction>) {
transaction.Date = new Date(transaction.Date); const transactionIds = [] as Array<string>;
this.Transactions.set(transaction.ID, transaction); 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) { async FetchAccount(account: Account) {
const result = await GET("/account/" + account.ID + "/transactions"); const result = await GET("/account/" + account.ID + "/transactions");
const response = await result.json(); const response = await result.json();
account.Transactions = []; const transactions = this.AddTransactions(response.Transactions);
for (const transaction of response.Transactions) { account.Transactions = transactions;
this.AddTransaction(account, transaction);
account.Transactions.push(transaction.ID);
}
}, },
async FetchMonthBudget(budgetid: string, year: number, month: number) { async FetchMonthBudget(budgetid: string, year: number, month: number) {
const result = await GET("/budget/" + budgetid + "/" + year + "/" + month); const result = await GET("/budget/" + budgetid + "/" + year + "/" + month);
@ -197,7 +201,7 @@ export const useAccountStore = defineStore("budget/account", {
const response = await result.json(); const response = await result.json();
const recTrans = response.ReconciliationTransaction; const recTrans = response.ReconciliationTransaction;
if (recTrans) { if (recTrans) {
this.AddTransaction(account, recTrans); this.AddTransactions([recTrans]);
account.Transactions.unshift(recTrans.ID); account.Transactions.unshift(recTrans.ID);
} }
console.log("Reconcile: " + response.message); console.log("Reconcile: " + response.message);
@ -207,14 +211,14 @@ export const useAccountStore = defineStore("budget/account", {
}, },
async saveTransaction(payload: string) { async saveTransaction(payload: string) {
const result = await POST("/transaction/new", payload); const result = await POST("/transaction/new", payload);
const response = await result.json(); const response = await result.json() as Transaction;
this.AddTransaction(this.CurrentAccount!, response); this.AddTransactions([response]);
this.CurrentAccount?.Transactions.unshift(response.ID); this.CurrentAccount?.Transactions.unshift(response.ID);
}, },
async editTransaction(transactionid: string, payload: string) { async editTransaction(transactionid: string, payload: string) {
const result = await POST("/transaction/" + transactionid, payload); const result = await POST("/transaction/" + transactionid, payload);
const response = await result.json(); const response = await result.json() as Transaction;
this.AddTransaction(this.CurrentAccount!, response); this.AddTransactions([response]);
} }
} }

View File

@ -53,12 +53,15 @@ export const useBudgetsStore = defineStore('budget', {
const response = await result.json(); const response = await result.json();
this.MergeBudgetingData(response); this.MergeBudgetingData(response);
}, },
MergeBudgetingData(response : any) { MergeBudgetingData(response: any) {
const accounts = useAccountStore();
for (const account of response.Accounts || []) { 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 || []) { for (const category of response.Categories || []) {
useAccountStore().Categories.set(category.ID, category); accounts.Categories.set(category.ID, category);
} }
}, },
} }