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);
await this.FetchAccount(account);
},
AddTransaction(account: Account, transaction: any) {
transaction.Date = new Date(transaction.Date);
this.Transactions.set(transaction.ID, transaction);
AddTransactions(transactions: Array<Transaction>) {
const transactionIds = [] as Array<string>;
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]);
}
}

View File

@ -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);
}
},
}