Refactor transactions store
This commit is contained in:
parent
27372199f7
commit
024c5e0a1c
@ -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>) {
|
||||||
|
const transactionIds = [] as Array<string>;
|
||||||
|
this.$patch(() => {
|
||||||
|
for (const transaction of transactions) {
|
||||||
transaction.Date = new Date(transaction.Date);
|
transaction.Date = new Date(transaction.Date);
|
||||||
this.Transactions.set(transaction.ID, transaction);
|
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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,11 +54,14 @@ export const useBudgetsStore = defineStore('budget', {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user