Normalize transaction store

This commit is contained in:
Jan Bader 2022-02-25 20:47:58 +00:00
parent 0f6990407d
commit a452482381

View File

@ -8,7 +8,7 @@ interface State {
CurrentAccountID: string | null, CurrentAccountID: string | null,
Categories: Map<string, Category>, Categories: Map<string, Category>,
Months: Map<number, Map<number, Map<string, Category>>>, Months: Map<number, Map<number, Map<string, Category>>>,
Transactions: any[], Transactions: Map<string, Transaction>,
Assignments: [] Assignments: []
} }
@ -30,6 +30,7 @@ export interface Account {
Name: string Name: string
OnBudget: boolean OnBudget: boolean
Balance: number Balance: number
Transactions: string[]
} }
export interface Category { export interface Category {
@ -48,7 +49,7 @@ export const useAccountStore = defineStore("budget/account", {
CurrentAccountID: null, CurrentAccountID: null,
Months: new Map<number, Map<number, Map<string, Category>>>(), Months: new Map<number, Map<number, Map<string, Category>>>(),
Categories: new Map<string, Category>(), Categories: new Map<string, Category>(),
Transactions: [], Transactions: new Map<string, Transaction>(),
Assignments: [] Assignments: []
}), }),
getters: { getters: {
@ -100,8 +101,10 @@ export const useAccountStore = defineStore("budget/account", {
OffBudgetAccountsBalance(state): number { OffBudgetAccountsBalance(state): number {
return this.OffBudgetAccounts.reduce((prev, curr) => prev + Number(curr.Balance), 0); return this.OffBudgetAccounts.reduce((prev, curr) => prev + Number(curr.Balance), 0);
}, },
TransactionsList(state) { TransactionsList(state) : Transaction[] {
return (state.Transactions || []); return this.CurrentAccount!.Transactions.map(x => {
return this.Transactions.get(x)!
});
} }
}, },
actions: { actions: {
@ -110,16 +113,21 @@ export const useAccountStore = defineStore("budget/account", {
return return
this.CurrentAccountID = accountid; this.CurrentAccountID = accountid;
if (this.CurrentAccount == undefined) const account = this.CurrentAccount;
if (account == undefined)
return return
useSessionStore().setTitle(this.CurrentAccount.Name); useSessionStore().setTitle(account.Name);
await this.FetchAccount(accountid); await this.FetchAccount(account);
}, },
async FetchAccount(accountid: string) { async FetchAccount(account: Account) {
const result = await GET("/account/" + accountid + "/transactions"); const result = await GET("/account/" + account.ID + "/transactions");
const response = await result.json(); const response = await result.json();
this.Transactions = response.Transactions; account.Transactions = [];
for (const transaction of response.Transactions) {
this.Transactions.set(transaction.ID, 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);
@ -151,7 +159,7 @@ 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();
this.Transactions.unshift(response); this.CurrentAccount?.Transactions.unshift(response);
} }
} }