Extract transactions store
This commit is contained in:
parent
024c5e0a1c
commit
a3e12df2e2
@ -2,31 +2,14 @@ import { defineStore } from "pinia"
|
|||||||
import { GET, POST } from "../api";
|
import { GET, POST } from "../api";
|
||||||
import { useBudgetsStore } from "./budget";
|
import { useBudgetsStore } from "./budget";
|
||||||
import { useSessionStore } from "./session";
|
import { useSessionStore } from "./session";
|
||||||
|
import { useTransactionsStore } from "./transactions";
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
Accounts: Map<string, Account>
|
Accounts: Map<string, Account>
|
||||||
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: Map<string, Transaction>
|
|
||||||
Assignments: []
|
Assignments: []
|
||||||
Reconciling: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Transaction {
|
|
||||||
ID: string
|
|
||||||
Date: Date
|
|
||||||
TransferAccount: string
|
|
||||||
CategoryGroup: string
|
|
||||||
Category: string
|
|
||||||
CategoryID: string | undefined
|
|
||||||
Memo: string
|
|
||||||
Status: string
|
|
||||||
GroupID: string
|
|
||||||
Payee: string
|
|
||||||
PayeeID: string | undefined
|
|
||||||
Amount: number
|
|
||||||
Reconciled: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Account {
|
export interface Account {
|
||||||
@ -55,9 +38,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: new Map<string, Transaction>(),
|
|
||||||
Assignments: [],
|
Assignments: [],
|
||||||
Reconciling: false,
|
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
AccountsList(state) {
|
AccountsList(state) {
|
||||||
@ -121,11 +102,6 @@ export const useAccountStore = defineStore("budget/account", {
|
|||||||
OffBudgetAccountsBalance(state): number {
|
OffBudgetAccountsBalance(state): number {
|
||||||
return this.OffBudgetAccounts.reduce((prev, curr) => prev + Number(curr.ClearedBalance), 0);
|
return this.OffBudgetAccounts.reduce((prev, curr) => prev + Number(curr.ClearedBalance), 0);
|
||||||
},
|
},
|
||||||
TransactionsList(state): Transaction[] {
|
|
||||||
return this.CurrentAccount!.Transactions.map(x => {
|
|
||||||
return this.Transactions.get(x)!
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
async SetCurrentAccount(budgetid: string, accountid: string) {
|
async SetCurrentAccount(budgetid: string, accountid: string) {
|
||||||
@ -137,21 +113,11 @@ export const useAccountStore = defineStore("budget/account", {
|
|||||||
useSessionStore().setTitle(account.Name);
|
useSessionStore().setTitle(account.Name);
|
||||||
await this.FetchAccount(account);
|
await this.FetchAccount(account);
|
||||||
},
|
},
|
||||||
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) {
|
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();
|
||||||
const transactions = this.AddTransactions(response.Transactions);
|
const transactionsStore = useTransactionsStore()
|
||||||
|
const transactions = transactionsStore.AddTransactions(response.Transactions);
|
||||||
account.Transactions = transactions;
|
account.Transactions = transactions;
|
||||||
},
|
},
|
||||||
async FetchMonthBudget(budgetid: string, year: number, month: number) {
|
async FetchMonthBudget(budgetid: string, year: number, month: number) {
|
||||||
@ -178,48 +144,9 @@ export const useAccountStore = defineStore("budget/account", {
|
|||||||
state.Months.set(year, yearMap);
|
state.Months.set(year, yearMap);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
SetReconciledForAllTransactions(value: boolean) {
|
|
||||||
for (const transaction of this.TransactionsList) {
|
|
||||||
if (transaction.Status == "Reconciled")
|
|
||||||
continue;
|
|
||||||
|
|
||||||
transaction.Reconciled = value;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async SubmitReconcilation(reconciliationTransactionAmount: number) {
|
|
||||||
const account = this.CurrentAccount!;
|
|
||||||
const reconciledTransactions = this.TransactionsList.filter(x => x.Reconciled);
|
|
||||||
for (const transaction of reconciledTransactions) {
|
|
||||||
account.ReconciledBalance += transaction.Amount;
|
|
||||||
transaction.Status = "Reconciled";
|
|
||||||
transaction.Reconciled = false;
|
|
||||||
}
|
|
||||||
const result = await POST("/account/" + this.CurrentAccountID + "/reconcile", JSON.stringify({
|
|
||||||
transactionIDs: reconciledTransactions.map(x => x.ID),
|
|
||||||
reconciliationTransactionAmount: reconciliationTransactionAmount.toString(),
|
|
||||||
}));
|
|
||||||
const response = await result.json();
|
|
||||||
const recTrans = response.ReconciliationTransaction;
|
|
||||||
if (recTrans) {
|
|
||||||
this.AddTransactions([recTrans]);
|
|
||||||
account.Transactions.unshift(recTrans.ID);
|
|
||||||
}
|
|
||||||
console.log("Reconcile: " + response.message);
|
|
||||||
},
|
|
||||||
logout() {
|
logout() {
|
||||||
this.$reset()
|
this.$reset()
|
||||||
},
|
},
|
||||||
async saveTransaction(payload: string) {
|
|
||||||
const result = await POST("/transaction/new", payload);
|
|
||||||
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() as Transaction;
|
|
||||||
this.AddTransactions([response]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
109
web/src/stores/transactions.ts
Normal file
109
web/src/stores/transactions.ts
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
|
||||||
|
import { defineStore } from "pinia"
|
||||||
|
import { GET, POST } from "../api";
|
||||||
|
import { useBudgetsStore } from "./budget";
|
||||||
|
import { useAccountStore } from "./budget-account";
|
||||||
|
import { useSessionStore } from "./session";
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
Transactions: Map<string, Transaction>
|
||||||
|
Reconciling: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Transaction {
|
||||||
|
ID: string
|
||||||
|
Date: Date
|
||||||
|
TransferAccount: string
|
||||||
|
CategoryGroup: string
|
||||||
|
Category: string
|
||||||
|
CategoryID: string | undefined
|
||||||
|
Memo: string
|
||||||
|
Status: string
|
||||||
|
GroupID: string
|
||||||
|
Payee: string
|
||||||
|
PayeeID: string | undefined
|
||||||
|
Amount: number
|
||||||
|
Reconciled: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useTransactionsStore = defineStore("budget/transactions", {
|
||||||
|
state: (): State => ({
|
||||||
|
Transactions: new Map<string, Transaction>(),
|
||||||
|
Reconciling: false,
|
||||||
|
}),
|
||||||
|
getters: {
|
||||||
|
ReconcilingBalance(state): number {
|
||||||
|
const accountsStore = useAccountStore()
|
||||||
|
let reconciledBalance = accountsStore.CurrentAccount!.ReconciledBalance;
|
||||||
|
for (const transaction of this.TransactionsList) {
|
||||||
|
if (transaction.Reconciled)
|
||||||
|
reconciledBalance += transaction.Amount;
|
||||||
|
}
|
||||||
|
return reconciledBalance;
|
||||||
|
},
|
||||||
|
TransactionsList(state): Transaction[] {
|
||||||
|
const accountsStore = useAccountStore()
|
||||||
|
return accountsStore.CurrentAccount!.Transactions.map(x => {
|
||||||
|
return this.Transactions.get(x)!
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
actions: {
|
||||||
|
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;
|
||||||
|
},
|
||||||
|
SetReconciledForAllTransactions(value: boolean) {
|
||||||
|
for (const transaction of this.TransactionsList) {
|
||||||
|
if (transaction.Status == "Reconciled")
|
||||||
|
continue;
|
||||||
|
|
||||||
|
transaction.Reconciled = value;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async SubmitReconcilation(reconciliationTransactionAmount: number) {
|
||||||
|
const accountsStore = useAccountStore()
|
||||||
|
const account = accountsStore.CurrentAccount!;
|
||||||
|
const reconciledTransactions = this.TransactionsList.filter(x => x.Reconciled);
|
||||||
|
for (const transaction of reconciledTransactions) {
|
||||||
|
account.ReconciledBalance += transaction.Amount;
|
||||||
|
transaction.Status = "Reconciled";
|
||||||
|
transaction.Reconciled = false;
|
||||||
|
}
|
||||||
|
const result = await POST("/account/" + accountsStore.CurrentAccountID + "/reconcile", JSON.stringify({
|
||||||
|
transactionIDs: reconciledTransactions.map(x => x.ID),
|
||||||
|
reconciliationTransactionAmount: reconciliationTransactionAmount.toString(),
|
||||||
|
}));
|
||||||
|
const response = await result.json();
|
||||||
|
const recTrans = response.ReconciliationTransaction;
|
||||||
|
if (recTrans) {
|
||||||
|
this.AddTransactions([recTrans]);
|
||||||
|
account.Transactions.unshift(recTrans.ID);
|
||||||
|
}
|
||||||
|
console.log("Reconcile: " + response.message);
|
||||||
|
},
|
||||||
|
logout() {
|
||||||
|
this.$reset()
|
||||||
|
},
|
||||||
|
async saveTransaction(payload: string) {
|
||||||
|
const accountsStore = useAccountStore()
|
||||||
|
const result = await POST("/transaction/new", payload);
|
||||||
|
const response = await result.json() as Transaction;
|
||||||
|
this.AddTransactions([response]);
|
||||||
|
accountsStore.CurrentAccount?.Transactions.unshift(response.ID);
|
||||||
|
},
|
||||||
|
async editTransaction(transactionid: string, payload: string) {
|
||||||
|
const result = await POST("/transaction/" + transactionid, payload);
|
||||||
|
const response = await result.json() as Transaction;
|
||||||
|
this.AddTransactions([response]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user