code refactor
This commit is contained in:
+76
-54
@@ -11,53 +11,11 @@ import iconConstants from '@/consts/icon.js';
|
|||||||
import colorConstants from '@/consts/color.js';
|
import colorConstants from '@/consts/color.js';
|
||||||
import services from '@/lib/services.js';
|
import services from '@/lib/services.js';
|
||||||
import logger from '@/lib/logger.js';
|
import logger from '@/lib/logger.js';
|
||||||
import { isNumber, isObject } from '@/lib/common.js';
|
import {
|
||||||
|
isEquals,
|
||||||
function loadTransactionStatistics(state, { statistics, defaultCurrency }) {
|
isNumber,
|
||||||
if (statistics && statistics.items && statistics.items.length) {
|
isObject
|
||||||
const accountsStore = useAccountsStore();
|
} from '@/lib/common.js';
|
||||||
const transactionCategoriesStore = useTransactionCategoriesStore();
|
|
||||||
const exchangeRatesStore = useExchangeRatesStore();
|
|
||||||
|
|
||||||
for (let i = 0; i < statistics.items.length; i++) {
|
|
||||||
const item = statistics.items[i];
|
|
||||||
|
|
||||||
if (item.accountId) {
|
|
||||||
item.account = accountsStore.allAccountsMap[item.accountId];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.account && item.account.parentId !== '0') {
|
|
||||||
item.primaryAccount = accountsStore.allAccountsMap[item.account.parentId];
|
|
||||||
} else {
|
|
||||||
item.primaryAccount = item.account;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.categoryId) {
|
|
||||||
item.category = transactionCategoriesStore.allTransactionCategoriesMap[item.categoryId];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.category && item.category.parentId !== '0') {
|
|
||||||
item.primaryCategory = transactionCategoriesStore.allTransactionCategoriesMap[item.category.parentId];
|
|
||||||
} else {
|
|
||||||
item.primaryCategory = item.category;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.account && item.account.currency !== defaultCurrency) {
|
|
||||||
const amount = exchangeRatesStore.getExchangedAmount(item.amount, item.account.currency, defaultCurrency);
|
|
||||||
|
|
||||||
if (isNumber(amount)) {
|
|
||||||
item.amountInDefaultCurrency = Math.floor(amount);
|
|
||||||
}
|
|
||||||
} else if (item.account && item.account.currency === defaultCurrency) {
|
|
||||||
item.amountInDefaultCurrency = item.amount;
|
|
||||||
} else {
|
|
||||||
item.amountInDefaultCurrency = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
state.transactionStatistics = statistics;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useStatisticsStore = defineStore('statistics', {
|
export const useStatisticsStore = defineStore('statistics', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
@@ -70,10 +28,72 @@ export const useStatisticsStore = defineStore('statistics', {
|
|||||||
filterAccountIds: {},
|
filterAccountIds: {},
|
||||||
filterCategoryIds: {}
|
filterCategoryIds: {}
|
||||||
},
|
},
|
||||||
transactionStatistics: [],
|
transactionStatisticsData: {},
|
||||||
transactionStatisticsStateInvalid: true
|
transactionStatisticsStateInvalid: true
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
|
transactionStatistics(state) {
|
||||||
|
const statistics = state.transactionStatisticsData;
|
||||||
|
const finalStatistics = {
|
||||||
|
startTime: statistics.startTime,
|
||||||
|
endTime: statistics.endTime,
|
||||||
|
items: []
|
||||||
|
};
|
||||||
|
|
||||||
|
if (statistics && statistics.items && statistics.items.length) {
|
||||||
|
const userStore = useUserStore();
|
||||||
|
const accountsStore = useAccountsStore();
|
||||||
|
const transactionCategoriesStore = useTransactionCategoriesStore();
|
||||||
|
const exchangeRatesStore = useExchangeRatesStore();
|
||||||
|
|
||||||
|
const defaultCurrency = userStore.currentUserDefaultCurrency;
|
||||||
|
|
||||||
|
for (let i = 0; i < statistics.items.length; i++) {
|
||||||
|
const dataItem = statistics.items[i];
|
||||||
|
const item = {
|
||||||
|
categoryId: dataItem.categoryId,
|
||||||
|
accountId: dataItem.accountId,
|
||||||
|
amount: dataItem.amount
|
||||||
|
};
|
||||||
|
|
||||||
|
if (item.accountId) {
|
||||||
|
item.account = accountsStore.allAccountsMap[item.accountId];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.account && item.account.parentId !== '0') {
|
||||||
|
item.primaryAccount = accountsStore.allAccountsMap[item.account.parentId];
|
||||||
|
} else {
|
||||||
|
item.primaryAccount = item.account;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.categoryId) {
|
||||||
|
item.category = transactionCategoriesStore.allTransactionCategoriesMap[item.categoryId];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.category && item.category.parentId !== '0') {
|
||||||
|
item.primaryCategory = transactionCategoriesStore.allTransactionCategoriesMap[item.category.parentId];
|
||||||
|
} else {
|
||||||
|
item.primaryCategory = item.category;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.account && item.account.currency !== defaultCurrency) {
|
||||||
|
const amount = exchangeRatesStore.getExchangedAmount(item.amount, item.account.currency, defaultCurrency);
|
||||||
|
|
||||||
|
if (isNumber(amount)) {
|
||||||
|
item.amountInDefaultCurrency = Math.floor(amount);
|
||||||
|
}
|
||||||
|
} else if (item.account && item.account.currency === defaultCurrency) {
|
||||||
|
item.amountInDefaultCurrency = item.amount;
|
||||||
|
} else {
|
||||||
|
item.amountInDefaultCurrency = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
finalStatistics.items.push(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return finalStatistics;
|
||||||
|
},
|
||||||
statisticsItemsByTransactionStatisticsData(state) {
|
statisticsItemsByTransactionStatisticsData(state) {
|
||||||
if (!state.transactionStatistics || !state.transactionStatistics.items) {
|
if (!state.transactionStatistics || !state.transactionStatistics.items) {
|
||||||
return null;
|
return null;
|
||||||
@@ -381,7 +401,7 @@ export const useStatisticsStore = defineStore('statistics', {
|
|||||||
this.transactionStatisticsFilter.sortingType = filter.sortingType;
|
this.transactionStatisticsFilter.sortingType = filter.sortingType;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
loadTransactionStatistics({ defaultCurrency }) {
|
loadTransactionStatistics({ force }) {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@@ -396,15 +416,17 @@ export const useStatisticsStore = defineStore('statistics', {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
loadTransactionStatistics(self, {
|
|
||||||
statistics: data.result,
|
|
||||||
defaultCurrency: defaultCurrency
|
|
||||||
});
|
|
||||||
|
|
||||||
if (self.transactionStatisticsStateInvalid) {
|
if (self.transactionStatisticsStateInvalid) {
|
||||||
self.updateTransactionStatisticsInvalidState(false);
|
self.updateTransactionStatisticsInvalidState(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (force && data.result && isEquals(self.transactionStatisticsData, data.result)) {
|
||||||
|
reject({ message: 'Data is up to date' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.transactionStatisticsData = data.result;
|
||||||
|
|
||||||
resolve(data.result);
|
resolve(data.result);
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
logger.error('failed to get transaction statistics', error);
|
logger.error('failed to get transaction statistics', error);
|
||||||
|
|||||||
@@ -474,7 +474,7 @@ export default {
|
|||||||
self.transactionCategoriesStore.loadAllCategories({ force: false })
|
self.transactionCategoriesStore.loadAllCategories({ force: false })
|
||||||
]).then(() => {
|
]).then(() => {
|
||||||
return self.statisticsStore.loadTransactionStatistics({
|
return self.statisticsStore.loadTransactionStatistics({
|
||||||
defaultCurrency: self.defaultCurrency
|
force: false
|
||||||
});
|
});
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
self.loading = false;
|
self.loading = false;
|
||||||
@@ -507,7 +507,7 @@ export default {
|
|||||||
self.query.chartDataType === self.allChartDataTypes.IncomeByPrimaryCategory.type ||
|
self.query.chartDataType === self.allChartDataTypes.IncomeByPrimaryCategory.type ||
|
||||||
self.query.chartDataType === self.allChartDataTypes.IncomeBySecondaryCategory.type) {
|
self.query.chartDataType === self.allChartDataTypes.IncomeBySecondaryCategory.type) {
|
||||||
dispatchPromise = self.statisticsStore.loadTransactionStatistics({
|
dispatchPromise = self.statisticsStore.loadTransactionStatistics({
|
||||||
defaultCurrency: self.defaultCurrency
|
force: force
|
||||||
});
|
});
|
||||||
} else if (self.query.chartDataType === self.allChartDataTypes.AccountTotalAssets.type ||
|
} else if (self.query.chartDataType === self.allChartDataTypes.AccountTotalAssets.type ||
|
||||||
self.query.chartDataType === self.allChartDataTypes.AccountTotalLiabilities.type) {
|
self.query.chartDataType === self.allChartDataTypes.AccountTotalLiabilities.type) {
|
||||||
|
|||||||
Reference in New Issue
Block a user