mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-17 16:24:25 +08:00
code refactor
This commit is contained in:
+177
-3
@@ -18,6 +18,8 @@ import {
|
||||
UPDATE_ACCOUNT_LIST_INVALID_STATE,
|
||||
|
||||
LOAD_TRANSACTION_LIST,
|
||||
SAVE_TRANSACTION_IN_TRANSACTION_LIST,
|
||||
REMOVE_TRANSACTION_FROM_TRANSACTION_LIST,
|
||||
UPDATE_TRANSACTION_LIST_INVALID_STATE,
|
||||
|
||||
LOAD_TRANSACTION_CATEGORY_LIST,
|
||||
@@ -34,7 +36,7 @@ import {
|
||||
CHANGE_TAG_DISPLAY_ORDER_IN_TRANSACTION_TAG_LIST,
|
||||
UPDATE_TAG_VISIBILITY_IN_TRANSACTION_TAG_LIST,
|
||||
REMOVE_TAG_FROM_TRANSACTION_TAG_LIST,
|
||||
UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE
|
||||
UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE,
|
||||
} from './mutations.js';
|
||||
|
||||
import user from './user.js';
|
||||
@@ -56,6 +58,7 @@ const stores = {
|
||||
allCategorizedAccounts: {},
|
||||
accountListStateInvalid: true,
|
||||
transactions: [],
|
||||
transactionsNextTimeId: 0,
|
||||
transactionListStateInvalid: true,
|
||||
allTransactionCategories: {},
|
||||
allTransactionCategoriesMap: {},
|
||||
@@ -73,6 +76,8 @@ const stores = {
|
||||
allVisiblePlainAccounts: account.allVisiblePlainAccounts,
|
||||
allAvailableAccountsCount: account.allAvailableAccountsCount,
|
||||
allVisibleAccountsCount: account.allVisibleAccountsCount,
|
||||
noTransaction: transaction.noTransaction,
|
||||
hasMoreTransaction: transaction.hasMoreTransaction,
|
||||
},
|
||||
mutations: {
|
||||
[RESET_STATE] (state) {
|
||||
@@ -84,6 +89,7 @@ const stores = {
|
||||
state.accountListStateInvalid = true;
|
||||
|
||||
state.transactions = [];
|
||||
state.transactionsNextTimeId = 0;
|
||||
state.transactionListStateInvalid = true;
|
||||
|
||||
state.allTransactionCategories = {};
|
||||
@@ -115,6 +121,13 @@ const stores = {
|
||||
for (let i = 0; i < accounts.length; i++) {
|
||||
const account = accounts[i];
|
||||
state.allAccountsMap[account.id] = account;
|
||||
|
||||
if (account.subAccounts) {
|
||||
for (let j = 0; j < account.subAccounts.length; j++) {
|
||||
const subAccount = account.subAccounts[j];
|
||||
state.allAccountsMap[subAccount.id] = subAccount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state.allCategorizedAccounts = utils.getCategorizedAccounts(accounts);
|
||||
@@ -133,6 +146,13 @@ const stores = {
|
||||
|
||||
state.allAccountsMap[account.id] = account;
|
||||
|
||||
if (account.subAccounts) {
|
||||
for (let i = 0; i < account.subAccounts.length; i++) {
|
||||
const subAccount = account.subAccounts[i];
|
||||
state.allAccountsMap[subAccount.id] = subAccount;
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allCategorizedAccounts[account.category]) {
|
||||
const accountList = state.allCategorizedAccounts[account.category].accounts;
|
||||
accountList.push(account);
|
||||
@@ -150,6 +170,13 @@ const stores = {
|
||||
|
||||
state.allAccountsMap[account.id] = account;
|
||||
|
||||
if (account.subAccounts) {
|
||||
for (let i = 0; i < account.subAccounts.length; i++) {
|
||||
const subAccount = account.subAccounts[i];
|
||||
state.allAccountsMap[subAccount.id] = subAccount;
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allCategorizedAccounts[account.category]) {
|
||||
const accountList = state.allCategorizedAccounts[account.category].accounts;
|
||||
|
||||
@@ -203,6 +230,17 @@ const stores = {
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allAccountsMap[account.id] && state.allAccountsMap[account.id].subAccounts) {
|
||||
const subAccounts = state.allAccountsMap[account.id].subAccounts;
|
||||
|
||||
for (let i = 0; i < subAccounts.length; i++) {
|
||||
const subAccount = subAccounts[i];
|
||||
if (state.allAccountsMap[subAccount.id]) {
|
||||
delete state.allAccountsMap[subAccount.id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allAccountsMap[account.id]) {
|
||||
delete state.allAccountsMap[account.id];
|
||||
}
|
||||
@@ -221,8 +259,131 @@ const stores = {
|
||||
[UPDATE_ACCOUNT_LIST_INVALID_STATE] (state, invalidState) {
|
||||
state.accountListStateInvalid = invalidState;
|
||||
},
|
||||
[LOAD_TRANSACTION_LIST] (state, transactions) {
|
||||
state.transactions = transactions;
|
||||
[LOAD_TRANSACTION_LIST] (state, { transactions, reload, autoExpand, defaultCurrency, accountId }) {
|
||||
if (reload) {
|
||||
state.transactions = [];
|
||||
}
|
||||
|
||||
if (transactions.items && transactions.items.length) {
|
||||
let currentMonthListIndex = -1;
|
||||
let currentMonthList = null;
|
||||
|
||||
for (let i = 0; i < transactions.items.length; i++) {
|
||||
const item = transactions.items[i];
|
||||
const transactionTime = utils.parseDateFromUnixTime(item.time);
|
||||
|
||||
item.day = utils.getDay(transactionTime);
|
||||
item.dayOfWeek = utils.getDayOfWeek(transactionTime);
|
||||
item.sourceAccount = state.allAccountsMap[item.sourceAccountId];
|
||||
item.destinationAccount = state.allAccountsMap[item.destinationAccountId];
|
||||
item.category = state.allTransactionCategoriesMap[item.categoryId];
|
||||
item.tags = [];
|
||||
|
||||
if (item.tagIds && item.tagIds.length) {
|
||||
for (let j = 0; j < item.tagIds.length; j++) {
|
||||
const tag = state.allTransactionTagsMap[item.tagIds[j]];
|
||||
|
||||
if (tag) {
|
||||
item.tags.push(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const transactionYear = utils.getYear(transactionTime);
|
||||
const transactionMonth = utils.getMonth(transactionTime);
|
||||
const transactionYearMonth = utils.getYearAndMonth(transactionTime);
|
||||
|
||||
if (currentMonthList && currentMonthList.year === transactionYear && currentMonthList.month === transactionMonth) {
|
||||
currentMonthList.items.push(item);
|
||||
transaction.calculateMonthTotalAmount(state, currentMonthList, defaultCurrency, accountId, true);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let j = currentMonthListIndex + 1; j < state.transactions.length; j++) {
|
||||
if (state.transactions[j].year === transactionYear && state.transactions[j].month === transactionMonth) {
|
||||
currentMonthListIndex = j;
|
||||
currentMonthList = state.transactions[j];
|
||||
|
||||
if (j > 0) {
|
||||
transaction.calculateMonthTotalAmount(state, state.transactions[j - 1], defaultCurrency, accountId, false);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!currentMonthList && state.transactions.length > 0) {
|
||||
transaction.calculateMonthTotalAmount(state, state.transactions[state.transactions.length - 1], defaultCurrency, accountId, false);
|
||||
}
|
||||
|
||||
if (!currentMonthList || currentMonthList.year !== transactionYear || currentMonthList.month !== transactionMonth) {
|
||||
transaction.calculateMonthTotalAmount(state, currentMonthList, defaultCurrency, accountId, false);
|
||||
|
||||
state.transactions.push({
|
||||
year: transactionYear,
|
||||
month: transactionMonth,
|
||||
yearMonth: transactionYearMonth,
|
||||
opened: autoExpand,
|
||||
items: []
|
||||
});
|
||||
|
||||
currentMonthListIndex = state.transactions.length - 1;
|
||||
currentMonthList = state.transactions[state.transactions.length - 1];
|
||||
}
|
||||
|
||||
currentMonthList.items.push(item);
|
||||
transaction.calculateMonthTotalAmount(state, currentMonthList, defaultCurrency, accountId, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (transactions.nextTimeSequenceId) {
|
||||
state.transactionsNextTimeId = transactions.nextTimeSequenceId;
|
||||
} else {
|
||||
transaction.calculateMonthTotalAmount(state, state.transactions[state.transactions.length - 1], defaultCurrency, accountId, false);
|
||||
state.transactionsNextTimeId = -1;
|
||||
}
|
||||
},
|
||||
[SAVE_TRANSACTION_IN_TRANSACTION_LIST] (state, { transaction, defaultCurrency, accountId }) {
|
||||
for (let i = 0; i < state.transactions.length; i++) {
|
||||
const transactionMonthList = state.transactions[i];
|
||||
|
||||
if (!transactionMonthList.items ||
|
||||
transactionMonthList.items[0].time < transaction.time ||
|
||||
transactionMonthList.items[transactionMonthList.items.length - 1].time > transaction.time) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let j = 0; j < transactionMonthList.items.length; j++) {
|
||||
if (transactionMonthList.items[j].id === transaction.id) {
|
||||
transactionMonthList.items.splice(j, 1, transaction);
|
||||
transaction.calculateMonthTotalAmount(state, transactionMonthList, defaultCurrency, accountId, i >= state.transactions.length - 1 && state.transactionsNextTimeId > 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[REMOVE_TRANSACTION_FROM_TRANSACTION_LIST] (state, { transaction, defaultCurrency, accountId }) {
|
||||
for (let i = 0; i < state.transactions.length; i++) {
|
||||
const transactionMonthList = state.transactions[i];
|
||||
|
||||
if (!transactionMonthList.items ||
|
||||
transactionMonthList.items[0].time < transaction.time ||
|
||||
transactionMonthList.items[transactionMonthList.items.length - 1].time > transaction.time) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let j = 0; j < transactionMonthList.items.length; j++) {
|
||||
if (transactionMonthList.items[j].id === transaction.id) {
|
||||
transactionMonthList.items.splice(j, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (transactionMonthList.items.length < 1) {
|
||||
state.transactions.splice(i, 1);
|
||||
} else {
|
||||
transaction.calculateMonthTotalAmount(state, transactionMonthList, defaultCurrency, accountId, i >= state.transactions.length - 1 && state.transactionsNextTimeId > 0);
|
||||
}
|
||||
}
|
||||
},
|
||||
[UPDATE_TRANSACTION_LIST_INVALID_STATE] (state, invalidState) {
|
||||
state.transactionListStateInvalid = invalidState;
|
||||
@@ -320,6 +481,17 @@ const stores = {
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allTransactionCategoriesMap[category.id] && state.allTransactionCategoriesMap[category.id].subCategories) {
|
||||
const subCategories = state.allTransactionCategoriesMap[category.id].subCategories;
|
||||
|
||||
for (let i = 0; i < subCategories.length; i++) {
|
||||
const subCategory = subCategories[i];
|
||||
if (state.allTransactionCategoriesMap[subCategory.id]) {
|
||||
delete state.allTransactionCategoriesMap[subCategory.id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allTransactionCategoriesMap[category.id]) {
|
||||
delete state.allTransactionCategoriesMap[category.id];
|
||||
}
|
||||
@@ -405,8 +577,10 @@ const stores = {
|
||||
hideAccount: account.hideAccount,
|
||||
deleteAccount: account.deleteAccount,
|
||||
|
||||
getTransactions: transaction.getTransactions,
|
||||
getTransaction: transaction.getTransaction,
|
||||
saveTransaction: transaction.saveTransaction,
|
||||
deleteTransaction: transaction.deleteTransaction,
|
||||
|
||||
loadAllCategories: transactionCategory.loadAllCategories,
|
||||
getCategory: transactionCategory.getCategory,
|
||||
|
||||
@@ -14,6 +14,8 @@ export const REMOVE_ACCOUNT_FROM_ACCOUNT_LIST = 'REMOVE_ACCOUNT_FROM_ACCOUNT_LIS
|
||||
export const UPDATE_ACCOUNT_LIST_INVALID_STATE = 'UPDATE_ACCOUNT_LIST_INVALID_STATE';
|
||||
|
||||
export const LOAD_TRANSACTION_LIST = 'LOAD_TRANSACTION_LIST';
|
||||
export const SAVE_TRANSACTION_IN_TRANSACTION_LIST = 'SAVE_TRANSACTION_IN_TRANSACTION_LIST';
|
||||
export const REMOVE_TRANSACTION_FROM_TRANSACTION_LIST = 'REMOVE_TRANSACTION_FROM_TRANSACTION_LIST';
|
||||
export const UPDATE_TRANSACTION_LIST_INVALID_STATE = 'UPDATE_TRANSACTION_LIST_INVALID_STATE';
|
||||
|
||||
export const LOAD_TRANSACTION_CATEGORY_LIST = 'LOAD_TRANSACTION_CATEGORY_LIST';
|
||||
|
||||
+192
-9
@@ -1,11 +1,68 @@
|
||||
import transactionConstants from '../consts/transaction.js';
|
||||
import exchangeRates from "./exchangeRates.js";
|
||||
import services from '../lib/services.js';
|
||||
import logger from '../lib/logger.js';
|
||||
import utils from '../lib/utils.js';
|
||||
|
||||
import {
|
||||
LOAD_TRANSACTION_LIST, UPDATE_ACCOUNT_LIST_INVALID_STATE,
|
||||
UPDATE_TRANSACTION_LIST_INVALID_STATE
|
||||
LOAD_TRANSACTION_LIST,
|
||||
REMOVE_TRANSACTION_FROM_TRANSACTION_LIST,
|
||||
UPDATE_TRANSACTION_LIST_INVALID_STATE,
|
||||
UPDATE_ACCOUNT_LIST_INVALID_STATE,
|
||||
} from './mutations.js';
|
||||
|
||||
function getTransactions(context, { reload, autoExpand, defaultCurrency, maxTime, minTime, type, categoryId, accountId, keyword }) {
|
||||
let actualMaxTime = context.state.transactionsNextTimeId;
|
||||
|
||||
if (reload && maxTime > 0) {
|
||||
actualMaxTime = maxTime * 1000 + 999;
|
||||
} else if (reload && maxTime <= 0) {
|
||||
actualMaxTime = 0;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
services.getTransactions({
|
||||
maxTime: actualMaxTime,
|
||||
minTime: minTime,
|
||||
type: type,
|
||||
categoryId: categoryId,
|
||||
accountId: accountId,
|
||||
keyword: keyword
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to get transaction list' });
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(LOAD_TRANSACTION_LIST, {
|
||||
transactions: data.result,
|
||||
reload: reload,
|
||||
autoExpand: autoExpand,
|
||||
defaultCurrency: defaultCurrency,
|
||||
accountId: accountId
|
||||
});
|
||||
|
||||
if (reload) {
|
||||
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, false);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to load transaction list', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
reject({ message: 'Unable to get transaction list' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getTransaction(context, { transactionId }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.getTransaction({
|
||||
@@ -18,9 +75,6 @@ function getTransaction(context, { transactionId }) {
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(LOAD_TRANSACTION_LIST, data.result);
|
||||
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, false);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to load transaction info', error);
|
||||
@@ -58,7 +112,12 @@ function saveTransaction(context, { transaction }) {
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, true);
|
||||
if (!transaction.id) {
|
||||
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, true);
|
||||
} else {
|
||||
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, true);
|
||||
}
|
||||
|
||||
context.commit(UPDATE_ACCOUNT_LIST_INVALID_STATE, true);
|
||||
|
||||
resolve(data.result);
|
||||
@@ -80,7 +139,131 @@ function saveTransaction(context, { transaction }) {
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
getTransaction,
|
||||
saveTransaction
|
||||
function deleteTransaction(context, { transaction, defaultCurrency, accountId, beforeResolve }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.deleteTransaction({
|
||||
id: transaction.id
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to delete this transaction' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (beforeResolve) {
|
||||
beforeResolve(() => {
|
||||
context.commit(REMOVE_TRANSACTION_FROM_TRANSACTION_LIST, {
|
||||
transaction: transaction,
|
||||
defaultCurrency: defaultCurrency,
|
||||
accountId: accountId
|
||||
});
|
||||
});
|
||||
} else {
|
||||
context.commit(REMOVE_TRANSACTION_FROM_TRANSACTION_LIST, {
|
||||
transaction: transaction,
|
||||
defaultCurrency: defaultCurrency,
|
||||
accountId: accountId
|
||||
});
|
||||
}
|
||||
|
||||
context.commit(UPDATE_ACCOUNT_LIST_INVALID_STATE, true);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to delete transaction', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
reject({ message: 'Unable to delete this transaction' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function noTransaction(state) {
|
||||
for (let i = 0; i < state.transactions.length; i++) {
|
||||
const transactionMonthList = state.transactions[i];
|
||||
|
||||
for (let j = 0; j < transactionMonthList.items.length; j++) {
|
||||
if (transactionMonthList.items[j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function hasMoreTransaction(state) {
|
||||
return state.transactionsNextTimeId > 0;
|
||||
}
|
||||
|
||||
function calculateMonthTotalAmount(state, transactionMonthList, defaultCurrency, accountId, incomplete) {
|
||||
if (!transactionMonthList) {
|
||||
return;
|
||||
}
|
||||
|
||||
let totalExpense = 0;
|
||||
let totalIncome = 0;
|
||||
let hasUnCalculatedTotalExpense = false;
|
||||
let hasUnCalculatedTotalIncome = false;
|
||||
|
||||
for (let i = 0; i < transactionMonthList.items.length; i++) {
|
||||
const transaction = transactionMonthList.items[i];
|
||||
|
||||
if (!transaction.sourceAccount) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let amount = transaction.sourceAmount;
|
||||
|
||||
if (transaction.sourceAccount.currency !== defaultCurrency) {
|
||||
const balance = exchangeRates.getExchangedAmount(state)(amount, transaction.sourceAccount.currency, defaultCurrency);
|
||||
|
||||
if (!utils.isNumber(balance)) {
|
||||
if (transaction.type === transactionConstants.allTransactionTypes.Expense) {
|
||||
hasUnCalculatedTotalExpense = true;
|
||||
} else if (transaction.type === transactionConstants.allTransactionTypes.Income) {
|
||||
hasUnCalculatedTotalIncome = true;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
amount = Math.floor(balance);
|
||||
}
|
||||
|
||||
if (transaction.type === transactionConstants.allTransactionTypes.Expense) {
|
||||
totalExpense += amount;
|
||||
} else if (transaction.type === transactionConstants.allTransactionTypes.Income) {
|
||||
totalIncome += amount;
|
||||
} else if (transaction.type === transactionConstants.allTransactionTypes.Transfer && accountId) {
|
||||
if (accountId === transaction.sourceAccountId) {
|
||||
totalExpense += amount;
|
||||
} else if (accountId === transaction.destinationAccountId) {
|
||||
totalIncome += amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transactionMonthList.totalAmount = {
|
||||
expense: totalExpense,
|
||||
incompleteExpense: incomplete || hasUnCalculatedTotalExpense,
|
||||
income: totalIncome,
|
||||
incompleteIncome: incomplete || hasUnCalculatedTotalIncome
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
getTransactions,
|
||||
getTransaction,
|
||||
saveTransaction,
|
||||
deleteTransaction,
|
||||
noTransaction,
|
||||
hasMoreTransaction,
|
||||
calculateMonthTotalAmount
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user