code refactor

This commit is contained in:
MaysWind
2023-08-14 00:55:41 +08:00
parent e29afa3155
commit d77b9ef7c9
3 changed files with 177 additions and 145 deletions
+28
View File
@@ -116,6 +116,34 @@ export function allVisibleTransactionCategories(allTransactionCategories) {
return ret;
}
export function isCategoryIdAvailable(categories, categoryId) {
if (!categories || !categories.length) {
return false;
}
for (let i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].subCategories.length; j++) {
if (categories[i].subCategories[j].id === categoryId) {
return true;
}
}
}
return false;
}
export function getFirstAvailableCategoryId(categories) {
if (!categories || !categories.length) {
return '';
}
for (let i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].subCategories.length; j++) {
return categories[i].subCategories[j].id;
}
}
}
export function hasAnyAvailableCategory(allVisibleTransactionCategories) {
for (let type in allVisibleTransactionCategories) {
if (!Object.prototype.hasOwnProperty.call(allVisibleTransactionCategories, type)) {
+128
View File
@@ -0,0 +1,128 @@
import categoryConstants from '@/consts/category.js';
import transactionConstants from '@/consts/transaction.js';
import {
isNumber
} from './common.js';
import {
getBrowserTimezoneOffsetMinutes,
getDummyUnixTimeForLocalUsage
} from './datetime.js';
import {
categoryTypeToTransactionType,
isCategoryIdAvailable,
getFirstAvailableCategoryId
} from './category.js';
export function setTransactionModelByTransaction(transaction, transaction2, allCategories, allCategoriesMap, allVisibleAccounts, allAccountsMap, defaultAccountId, options, isNew) {
if ((!options.type || options.type === '0') && options.categoryId && options.categoryId !== '0' && allCategoriesMap[options.categoryId]) {
const category = allCategoriesMap[options.categoryId];
const type = categoryTypeToTransactionType(category.type);
if (isNumber(type)) {
transaction.type = type;
}
}
if (allCategories[categoryConstants.allCategoryTypes.Expense] &&
allCategories[categoryConstants.allCategoryTypes.Expense].length) {
if (options.categoryId && options.categoryId !== '0' && isCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Expense], options.categoryId)) {
transaction.expenseCategory = options.categoryId;
}
if (!transaction.expenseCategory) {
transaction.expenseCategory = getFirstAvailableCategoryId(allCategories[categoryConstants.allCategoryTypes.Expense]);
}
}
if (allCategories[categoryConstants.allCategoryTypes.Income] &&
allCategories[categoryConstants.allCategoryTypes.Income].length) {
if (options.categoryId && options.categoryId !== '0' && isCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Income], options.categoryId)) {
transaction.incomeCategory = options.categoryId;
}
if (!transaction.incomeCategory) {
transaction.incomeCategory = getFirstAvailableCategoryId(allCategories[categoryConstants.allCategoryTypes.Income]);
}
}
if (allCategories[categoryConstants.allCategoryTypes.Transfer] &&
allCategories[categoryConstants.allCategoryTypes.Transfer].length) {
if (options.categoryId && options.categoryId !== '0' && isCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Transfer], options.categoryId)) {
transaction.transferCategory = options.categoryId;
}
if (!transaction.transferCategory) {
transaction.transferCategory = getFirstAvailableCategoryId(allCategories[categoryConstants.allCategoryTypes.Transfer]);
}
}
if (allVisibleAccounts.length) {
if (options.accountId && options.accountId !== '0') {
for (let i = 0; i < allVisibleAccounts.length; i++) {
if (allVisibleAccounts[i].id === options.accountId) {
transaction.sourceAccountId = options.accountId;
transaction.destinationAccountId = options.accountId;
break;
}
}
}
if (!transaction.sourceAccountId) {
if (defaultAccountId && allAccountsMap[defaultAccountId]) {
transaction.sourceAccountId = defaultAccountId;
} else {
transaction.sourceAccountId = allVisibleAccounts[0].id;
}
}
if (!transaction.destinationAccountId) {
if (defaultAccountId && allAccountsMap[defaultAccountId]) {
transaction.destinationAccountId = defaultAccountId;
} else {
transaction.destinationAccountId = allVisibleAccounts[0].id;
}
}
}
if (transaction2) {
if (!isNew) {
transaction.id = transaction2.id;
}
transaction.type = transaction2.type;
if (transaction.type === transactionConstants.allTransactionTypes.Expense) {
transaction.expenseCategory = transaction2.categoryId;
} else if (transaction.type === transactionConstants.allTransactionTypes.Income) {
transaction.incomeCategory = transaction2.categoryId;
} else if (transaction.type === transactionConstants.allTransactionTypes.Transfer) {
transaction.transferCategory = transaction2.categoryId;
}
if (!isNew) {
transaction.utcOffset = transaction2.utcOffset;
transaction.timeZone = transaction2.timeZone;
transaction.time = getDummyUnixTimeForLocalUsage(transaction2.time, transaction.utcOffset, getBrowserTimezoneOffsetMinutes());
}
transaction.sourceAccountId = transaction2.sourceAccountId;
if (transaction2.destinationAccountId) {
transaction.destinationAccountId = transaction2.destinationAccountId;
}
transaction.sourceAmount = transaction2.sourceAmount;
if (transaction2.destinationAmount) {
transaction.destinationAmount = transaction2.destinationAmount;
}
transaction.hideAmount = transaction2.hideAmount;
transaction.tagIds = transaction2.tagIds || [];
transaction.comment = transaction2.comment;
if (!isNew) {
transaction.geoLocation = transaction2.geoLocation;
}
}
}
+21 -145
View File
@@ -350,7 +350,6 @@ import {
getTimezoneOffsetMinutes,
getBrowserTimezoneOffsetMinutes,
getUtcOffsetByUtcOffsetMinutes,
getDummyUnixTimeForLocalUsage,
getActualUnixTimeForStore
} from '@/lib/datetime.js';
import {
@@ -358,11 +357,12 @@ import {
getAllFilteredAccountsBalance
} from '@/lib/account.js';
import {
categoryTypeToTransactionType,
getTransactionPrimaryCategoryName,
getTransactionSecondaryCategoryName
getTransactionSecondaryCategoryName,
getFirstAvailableCategoryId
} from '@/lib/category.js';
import { getMapProvider } from '@/lib/server_settings.js';
import { setTransactionModelByTransaction } from '@/lib/transaction.js';
export default {
props: [
@@ -535,7 +535,7 @@ export default {
return false;
}
const firstAvailableCategoryId = this.getFirstAvailableCategoryId(this.allCategories[this.allCategoryTypes.Expense]);
const firstAvailableCategoryId = getFirstAvailableCategoryId(this.allCategories[this.allCategoryTypes.Expense]);
return firstAvailableCategoryId !== '';
},
hasAvailableIncomeCategories() {
@@ -543,7 +543,7 @@ export default {
return false;
}
const firstAvailableCategoryId = this.getFirstAvailableCategoryId(this.allCategories[this.allCategoryTypes.Income]);
const firstAvailableCategoryId = getFirstAvailableCategoryId(this.allCategories[this.allCategoryTypes.Income]);
return firstAvailableCategoryId !== '';
},
hasAvailableTransferCategories() {
@@ -551,7 +551,7 @@ export default {
return false;
}
const firstAvailableCategoryId = this.getFirstAvailableCategoryId(this.allCategories[this.allCategoryTypes.Transfer]);
const firstAvailableCategoryId = getFirstAvailableCategoryId(this.allCategories[this.allCategoryTypes.Transfer]);
return firstAvailableCategoryId !== '';
},
sourceAccountName() {
@@ -693,119 +693,21 @@ export default {
return;
}
if ((!query.type || query.type === '0') && query.categoryId && query.categoryId !== '0' && self.allCategoriesMap[query.categoryId]) {
const category = self.allCategoriesMap[query.categoryId];
const type = categoryTypeToTransactionType(category.type);
if (isNumber(type)) {
self.transaction.type = type;
}
}
if (self.allCategories[self.allCategoryTypes.Expense] &&
self.allCategories[self.allCategoryTypes.Expense].length) {
if (query.categoryId && query.categoryId !== '0' && self.isCategoryIdAvailable(self.allCategories[self.allCategoryTypes.Expense], query.categoryId)) {
self.transaction.expenseCategory = query.categoryId;
}
if (!self.transaction.expenseCategory) {
self.transaction.expenseCategory = self.getFirstAvailableCategoryId(self.allCategories[self.allCategoryTypes.Expense]);
}
}
if (self.allCategories[self.allCategoryTypes.Income] &&
self.allCategories[self.allCategoryTypes.Income].length) {
if (query.categoryId && query.categoryId !== '0' && self.isCategoryIdAvailable(self.allCategories[self.allCategoryTypes.Income], query.categoryId)) {
self.transaction.incomeCategory = query.categoryId;
}
if (!self.transaction.incomeCategory) {
self.transaction.incomeCategory = self.getFirstAvailableCategoryId(self.allCategories[self.allCategoryTypes.Income]);
}
}
if (self.allCategories[self.allCategoryTypes.Transfer] &&
self.allCategories[self.allCategoryTypes.Transfer].length) {
if (query.categoryId && query.categoryId !== '0' && self.isCategoryIdAvailable(self.allCategories[self.allCategoryTypes.Transfer], query.categoryId)) {
self.transaction.transferCategory = query.categoryId;
}
if (!self.transaction.transferCategory) {
self.transaction.transferCategory = self.getFirstAvailableCategoryId(self.allCategories[self.allCategoryTypes.Transfer]);
}
}
if (self.allVisibleAccounts.length) {
if (query.accountId && query.accountId !== '0') {
for (let i = 0; i < self.allVisibleAccounts.length; i++) {
if (self.allVisibleAccounts[i].id === query.accountId) {
self.transaction.sourceAccountId = query.accountId;
self.transaction.destinationAccountId = query.accountId;
break;
}
}
}
if (!self.transaction.sourceAccountId) {
if (self.defaultAccountId && self.allAccountsMap[self.defaultAccountId]) {
self.transaction.sourceAccountId = self.defaultAccountId;
} else {
self.transaction.sourceAccountId = self.allVisibleAccounts[0].id;
}
}
if (!self.transaction.destinationAccountId) {
if (self.defaultAccountId && self.allAccountsMap[self.defaultAccountId]) {
self.transaction.destinationAccountId = self.defaultAccountId;
} else {
self.transaction.destinationAccountId = self.allVisibleAccounts[0].id;
}
}
}
if (query.id) {
const transaction = responses[3];
if (self.mode === 'edit') {
self.transaction.id = transaction.id;
}
self.transaction.type = transaction.type;
if (self.transaction.type === self.allTransactionTypes.Expense) {
self.transaction.expenseCategory = transaction.categoryId;
} else if (self.transaction.type === self.allTransactionTypes.Income) {
self.transaction.incomeCategory = transaction.categoryId;
} else if (self.transaction.type === self.allTransactionTypes.Transfer) {
self.transaction.transferCategory = transaction.categoryId;
}
if (self.mode === 'edit' || self.mode === 'view') {
self.transaction.utcOffset = transaction.utcOffset;
self.transaction.timeZone = null;
self.transaction.time = getDummyUnixTimeForLocalUsage(transaction.time, self.transaction.utcOffset, getBrowserTimezoneOffsetMinutes());
}
self.transaction.sourceAccountId = transaction.sourceAccountId;
if (transaction.destinationAccountId) {
self.transaction.destinationAccountId = transaction.destinationAccountId;
}
self.transaction.sourceAmount = transaction.sourceAmount;
if (transaction.destinationAmount) {
self.transaction.destinationAmount = transaction.destinationAmount;
}
self.transaction.hideAmount = transaction.hideAmount;
self.transaction.tagIds = transaction.tagIds || [];
self.transaction.comment = transaction.comment;
if (self.mode === 'edit' || self.mode === 'view') {
self.transaction.geoLocation = transaction.geoLocation;
}
}
setTransactionModelByTransaction(
self.transaction,
query.id ? responses[3] : null,
self.allCategories,
self.allCategoriesMap,
self.allVisibleAccounts,
self.allAccountsMap,
self.defaultAccountId,
{
type: query.type,
categoryId: query.categoryId,
accountId: query.accountId
},
(self.mode !== 'edit' && self.mode !== 'view')
);
self.loading = false;
}).catch(error => {
@@ -948,32 +850,6 @@ export default {
this.geoLocationStatus = null;
this.transaction.geoLocation = null;
},
isCategoryIdAvailable(categories, categoryId) {
if (!categories || !categories.length) {
return false;
}
for (let i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].subCategories.length; j++) {
if (categories[i].subCategories[j].id === categoryId) {
return true;
}
}
}
return false;
},
getFirstAvailableCategoryId(categories) {
if (!categories || !categories.length) {
return '';
}
for (let i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].subCategories.length; j++) {
return categories[i].subCategories[j].id;
}
}
},
getFontClassByAmount(amount) {
if (amount >= 100000000 || amount <= -100000000) {
return 'ebk-small-amount';