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