Skip to content

Open source personal finance application

refactor generateId, change accountId => walletId

Ilya Komichevb6b8f57dc665
diff --git a/package.json b/package.json
index f330c8fe..6193c9a8 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "finapp",
- "version": "2.2.2",
+ "version": "2.2.3",
"scripts": {
"build": "vue-cli-service build --modern",
"dev": "npm run serve",
diff --git a/src/components/budgets/BudgetItem.vue b/src/components/budgets/BudgetItem.vue
index 384f31ad..cd44dbfd 100644
--- a/src/components/budgets/BudgetItem.vue
+++ b/src/components/budgets/BudgetItem.vue
@@ -38,11 +38,14 @@ export default {
if (this.budget.trnsIds) {
return Object.keys(this.budget.trnsIds)
.sort((a, b) => {
+ if (!this.$store.state.trns.items[a]) { console.log('not found', a) }
+ if (!this.$store.state.trns.items[b]) { console.log('not found', b) }
if (this.$store.state.trns.items[a].date > this.$store.state.trns.items[b].date) return -1
if (this.$store.state.trns.items[a].date < this.$store.state.trns.items[b].date) return 1
return 0
})
}
+ return []
},
gotAmount () {
diff --git a/src/components/budgets/store/actions.js b/src/components/budgets/store/actions.js
index 84c10b53..60b98133 100644
--- a/src/components/budgets/store/actions.js
+++ b/src/components/budgets/store/actions.js
@@ -1,6 +1,6 @@
import moment from 'moment'
import { db } from '@/firebase'
-import { generateDateId } from '@/utils/id'
+import generateId from '@/utils/id'
export default {
initBudgets ({ dispatch, rootGetters }) {
@@ -18,7 +18,7 @@ export default {
},
async createBudget ({ rootGetters }, values) {
- const id = generateDateId()
+ const id = generateId(moment().valueOf())
const uid = rootGetters.userUid
const path = `users/${uid}/budgets/${id}`
@@ -57,18 +57,21 @@ export default {
.then(() => { return true })
},
- async removeTrnFromBudget ({ rootGetters }, { budgetId, trnId }) {
+ removeTrnFromBudget ({ rootGetters }, { budgetId, trnId }) {
const uid = rootGetters.userUid
+ console.log('hey')
// remove from budget
- await db.ref(`users/${uid}/budgets/${budgetId}/trnsIds/${trnId}`)
+ db.ref(`users/${uid}/budgets/${budgetId}/trnsIds/${trnId}`)
.remove()
.then(() => { return true })
// remove from trn
- await db.ref(`users/${uid}/trns/${trnId}/budgets/${budgetId}`)
+ db.ref(`users/${uid}/trns/${trnId}/budgets/${budgetId}`)
.remove()
.then(() => { return true })
+
+ console.log('hey2')
},
toogleAddToBudget ({ dispatch, rootState, rootGetters }, { budgetId, trnId }) {
diff --git a/src/components/categories/form/CategoryForm.vue b/src/components/categories/form/CategoryForm.vue
index 031cf4e0..7982e007 100644
--- a/src/components/categories/form/CategoryForm.vue
+++ b/src/components/categories/form/CategoryForm.vue
@@ -1,6 +1,6 @@
<script>
import { db } from '@/firebase'
-import { generateSimpleId } from '@/utils/id'
+import generateId from '@/utils/id'
import colors from '@/components/ui/store/colors'
import icons from '@/components/ui/store/icons'
@@ -99,7 +99,7 @@ export default {
handleSubmit () {
if (this.validateForm()) {
const uid = this.$store.state.user.user.uid
- const id = this.categoryId || generateSimpleId()
+ const id = this.categoryId || generateId()
const categoriesValues = {
color: this.category.color,
diff --git a/src/components/groups/BudgetItem.vue b/src/components/groups/BudgetItem.vue
index 7ab4359c..84a359b9 100644
--- a/src/components/groups/BudgetItem.vue
+++ b/src/components/groups/BudgetItem.vue
@@ -26,6 +26,7 @@ export default {
computed: {
formatedDate () {
+ console.log(this.budget)
return formatDate(this.budget.date, 'number')
},
@@ -42,6 +43,7 @@ export default {
return 0
})
}
+ return []
},
gotAmount () {
@@ -58,6 +60,17 @@ export default {
}
},
+ async mounted () {
+ if (this.budget.trnsIds) {
+ for (const trnId of Object.keys(this.budget.trnsIds)) {
+ if (!this.$store.state.trns.items[trnId]) {
+ console.log('remove', this.budget.id, trnId)
+ await this.$store.dispatch('removeTrnFromGroup', { groupId: this.budget.id, trnId })
+ }
+ }
+ }
+ },
+
methods: {
async removeBudgetId (id) {
// await this.$store.dispatch('removeBudget', id)
diff --git a/src/components/groups/store/actions.js b/src/components/groups/store/actions.js
index 0717fdd9..6497f1a0 100644
--- a/src/components/groups/store/actions.js
+++ b/src/components/groups/store/actions.js
@@ -1,6 +1,6 @@
import moment from 'moment'
import { db } from '@/firebase'
-import { generateDateId } from '@/utils/id'
+import generateId from '@/utils/id'
export default {
initGroups ({ dispatch, rootGetters }) {
@@ -18,7 +18,7 @@ export default {
},
async createGroup ({ rootGetters }, values) {
- const id = generateDateId()
+ const id = generateId(moment().valueOf())
const uid = rootGetters.userUid
const path = `users/${uid}/groups/${id}`
@@ -55,16 +55,16 @@ export default {
.then(() => { return true })
},
- async removeTrnFromGroup ({ dispatch, rootGetters }, { groupId, trnId }) {
+ removeTrnFromGroup ({ dispatch, rootGetters }, { groupId, trnId }) {
const uid = rootGetters.userUid
// remove from group
- await db.ref(`users/${uid}/groups/${groupId}/trnsIds/${trnId}`)
- .remove()
+ console.log(`users/${uid}/groups/${groupId}/trnsIds/${trnId}`)
+ db.ref(`users/${uid}/groups/${groupId}/trnsIds/${trnId}`).set(null)
.then(() => { return true })
// remove from trn
- await db.ref(`users/${uid}/trns/${trnId}/groups/${groupId}`)
+ db.ref(`users/${uid}/trns/${trnId}/groups/${groupId}`)
.remove()
.then(() => { return true })
},
diff --git a/src/components/trnForm/TrnForm.vue b/src/components/trnForm/TrnForm.vue
index e7eb5c23..169b49d9 100644
--- a/src/components/trnForm/TrnForm.vue
+++ b/src/components/trnForm/TrnForm.vue
@@ -1,6 +1,6 @@
<script>
import moment from 'moment'
-import { generateDateId } from '@/utils/id'
+import generateId from '@/utils/id'
import Amount from '@/components/amount/Amount'
import CategoriesView from '@/components/categories/list/CategoriesView'
@@ -94,7 +94,7 @@ export default {
handleSubmitTrn () {
const values = { ...this.$store.state.trnForm.values }
- const id = values.trnId || generateDateId()
+ const id = values.trnId || generateId(moment().valueOf())
this.$store.dispatch('addTrn', {
id,
@@ -111,7 +111,7 @@ export default {
// Income
this.$store.dispatch('addTrn', {
- id: generateDateId(),
+ id: generateId(moment().valueOf()),
values: {
...values,
walletId: this.$store.state.trnForm.transfer.to,
@@ -121,7 +121,7 @@ export default {
// Expense
this.$store.dispatch('addTrn', {
- id: generateDateId(),
+ id: generateId(moment().valueOf()),
values: {
...values,
walletId: this.$store.state.trnForm.transfer.from,
diff --git a/src/components/trnForm/store/actions.js b/src/components/trnForm/store/actions.js
index 5f8f4c64..3952da73 100644
--- a/src/components/trnForm/store/actions.js
+++ b/src/components/trnForm/store/actions.js
@@ -22,7 +22,7 @@ export default {
date: moment().valueOf(),
description: null,
trnId: null,
- walletId: lastTrn.accountId
+ walletId: lastTrn.walletId
})
}
else {
@@ -55,7 +55,7 @@ export default {
date: trn.date,
description: trn.description || null,
trnId,
- walletId: trn.accountId
+ walletId: trn.walletId
})
}
break
@@ -72,7 +72,7 @@ export default {
date: moment().valueOf(),
description: trn.description || null,
trnId: null,
- walletId: trn.accountId
+ walletId: trn.walletId
})
}
break
diff --git a/src/components/trns/list/TrnsList.vue b/src/components/trns/list/TrnsList.vue
index 0f849223..ce4b97d3 100644
--- a/src/components/trns/list/TrnsList.vue
+++ b/src/components/trns/list/TrnsList.vue
@@ -125,7 +125,7 @@ export default {
:key="trnId"
:trn="$store.state.trns.items[trnId]"
:trnId="trnId"
- :wallet="$store.state.wallets.items[$store.state.trns.items[trnId].accountId]")
+ :wallet="$store.state.wallets.items[$store.state.trns.items[trnId].walletId]")
//- stat view
template(v-else)
@@ -136,7 +136,7 @@ export default {
:trn="$store.state.trns.items[trnId]"
:trnId="trnId"
:ui="ui"
- :wallet="$store.state.wallets.items[$store.state.trns.items[trnId].accountId]")
+ :wallet="$store.state.wallets.items[$store.state.trns.items[trnId].walletId]")
.trnsList__pages(v-if="!isShowAllTrns")
Button(
diff --git a/src/components/trns/list/TrnsList2.vue b/src/components/trns/list/TrnsList2.vue
index 162104a0..3cd7a91e 100644
--- a/src/components/trns/list/TrnsList2.vue
+++ b/src/components/trns/list/TrnsList2.vue
@@ -14,7 +14,7 @@ export default {
for (const trnId of trnsIds) {
const trn = this.$store.state.trns.items[trnId]
- const wallet = this.$store.state.wallets.items[trn.accountId]
+ const wallet = this.$store.state.wallets.items[trn.walletId]
const category = this.$store.state.categories.items[trn.categoryId]
trns[trnId] = {
diff --git a/src/components/trns/modal/TrnModal.vue b/src/components/trns/modal/TrnModal.vue
index 2ba90fd4..3cca746b 100644
--- a/src/components/trns/modal/TrnModal.vue
+++ b/src/components/trns/modal/TrnModal.vue
@@ -35,7 +35,7 @@ export default {
return this.$store.state.categories.items[this.$store.state.trns.items[this.trnId].categoryId]
},
wallet () {
- return this.$store.state.wallets.items[this.$store.state.trns.items[this.trnId].accountId]
+ return this.$store.state.wallets.items[this.$store.state.trns.items[this.trnId].walletId]
},
budgets () {
return this.$store.getters.budgets
@@ -56,7 +56,7 @@ export default {
this.$store.commit('hideTrnModal')
this.$store.dispatch('setActiveTab', 'stat')
this.$store.commit('setFilterDateNow')
- this.$store.dispatch('setFilterWalletId', this.$store.state.trns.items[this.trnId].accountId)
+ this.$store.dispatch('setFilterWalletId', this.$store.state.trns.items[this.trnId].walletId)
},
handleDublicateTrn () {
const trnId = this.trnId
@@ -132,7 +132,7 @@ ModalBottom(
icon="mdi mdi-chart-bubble"
v-on:onClick="handleSetFilterCategory")
- Button(
+ Button.marginBottom(
:class="{ marginBottom: budgets }"
className="_grey"
title="Set wallet filter"
@@ -140,6 +140,7 @@ ModalBottom(
v-on:onClick="handleSetFilterWallet")
Button(
+ v-if="budgets && $store.getters.isTester"
:class="{ marginBottom: groups }"
className="_grey"
title="Show budgets"
@@ -147,13 +148,14 @@ ModalBottom(
v-on:onClick="showModalBudgets = true")
Button(
+ v-if="groups && $store.getters.isTester"
className="_grey"
title="Show groups"
icon="mdi mdi-folder-multiple-outline"
v-on:onClick="showModalGroups = true")
//- budgets
- template(v-if="budgets")
+ template(v-if="groups && $store.getters.isTester")
ModalBottom(
:show="showModalBudgets"
title="Budgets"
@@ -171,7 +173,7 @@ ModalBottom(
.item__amount
Amount(:currency="budget.currency" :value="budget.amount")
- template(v-if="groups")
+ template(v-if="groups && $store.getters.isTester")
ModalBottom(
:show="showModalGroups"
title="groups"
diff --git a/src/components/trns/store/actions.js b/src/components/trns/store/actions.js
index 2e9c1863..07a0c110 100644
--- a/src/components/trns/store/actions.js
+++ b/src/components/trns/store/actions.js
@@ -15,17 +15,15 @@ export default {
const uid = rootState.user.user.uid
const trns = rootState.trns.items
let isTrnSavedOnline = false
-
const formatedTrnValues = {
- ...values,
- accountId: values.walletId,
- walletId: values.walletId,
amount: values.amount,
categoryId: values.categoryId,
date: moment(values.date).valueOf(),
description: values.description || null,
- editDate: moment().valueOf(),
- type: values.amountType || 0
+ edited: moment().valueOf(),
+ groups: values.groups || null,
+ type: Number(values.amountType) || 0,
+ walletId: values.walletId
}
localforage.setItem('next.trns', { ...trns, [id]: formatedTrnValues })
@@ -73,13 +71,32 @@ export default {
},
// init
- initTrns ({ rootState, dispatch, commit }) {
+ async initTrns ({ rootState, dispatch, commit }) {
const uid = rootState.user.user.uid
- db.ref(`users/${uid}/trns`).on('value', snapshot => {
+ await db.ref(`users/${uid}/trns`).on('value', snapshot => {
const items = Object.freeze(snapshot.val())
- dispatch('setTrns', items)
+
+ for (const trnId of Object.keys(items)) {
+ if (!items[trnId].walletId || items[trnId].accountId) {
+ commit('setAppStatus', 'loading')
+ const trn = items[trnId]
+ console.log(trnId)
+ db.ref(`users/${uid}/trns/${trnId}`)
+ .set({
+ amount: trn.amount,
+ categoryId: trn.categoryId,
+ date: Number(trn.date),
+ description: trn.description || null,
+ edited: moment().valueOf(),
+ groups: trn.groups || null,
+ type: Number(trn.type),
+ walletId: trn.accountId || trn.walletId
+ })
+ }
+ }
commit('setAppStatus', 'ready')
+ dispatch('setTrns', items)
}, e => console.error(e))
},
diff --git a/src/components/trns/store/getters.js b/src/components/trns/store/getters.js
index c4fa4f10..5184d79c 100644
--- a/src/components/trns/store/getters.js
+++ b/src/components/trns/store/getters.js
@@ -32,7 +32,7 @@ export default {
for (const key of trnsIds) {
const trn = trns[key]
if (trn && (inculdeTrnasfers || trn.categoryId !== transferCategoryId)) {
- const wallet = wallets[trn.accountId]
+ const wallet = wallets[trn.walletId]
if (wallet && currencies) {
let amount = 0
if (wallet.currency !== baseCurrency) {
@@ -58,7 +58,7 @@ export default {
const trns = rootState.trns.items
const trnsIds = []
for (const trnId in trns) {
- if (trns[trnId] && trns[trnId].accountId === walletId) {
+ if (trns[trnId] && trns[trnId].walletId === walletId) {
trnsIds.push(trnId)
}
}
@@ -117,7 +117,7 @@ export default {
// filter wallet
if (filterWalletId) {
- trnsIds = trnsIds.filter(trnId => trns[trnId].accountId === filterWalletId)
+ trnsIds = trnsIds.filter(trnId => trns[trnId].walletId === filterWalletId)
}
// filter category
@@ -220,7 +220,7 @@ export default {
// filter wallet
if (filterWalletId) {
- trnsIds = trnsIds.filter(trnId => trns[trnId].accountId === filterWalletId)
+ trnsIds = trnsIds.filter(trnId => trns[trnId].walletId === filterWalletId)
}
// filter category
diff --git a/src/components/wallets/form/WalletForm.vue b/src/components/wallets/form/WalletForm.vue
index fa36b1f0..a5bf9473 100644
--- a/src/components/wallets/form/WalletForm.vue
+++ b/src/components/wallets/form/WalletForm.vue
@@ -1,6 +1,6 @@
<script>
import { focus } from 'vue-focus'
-import { generateSimpleId } from '@/utils/id'
+import generateId from '@/utils/id'
import colors from '@/components/ui/store/colors'
import Button from '@/components/shared/button/Button'
@@ -75,7 +75,7 @@ export default {
handleSubmit () {
if (this.validateForm()) {
- const id = this.walletId || generateSimpleId()
+ const id = this.walletId || generateId()
const walletsValues = {
color: this.wallet.color,
diff --git a/src/components/wallets/modal/WalletModal.vue b/src/components/wallets/modal/WalletModal.vue
index 664f248c..831cc327 100644
--- a/src/components/wallets/modal/WalletModal.vue
+++ b/src/components/wallets/modal/WalletModal.vue
@@ -25,6 +25,7 @@ export default {
if (this.trnsIds.length > 0) {
return `It's also will delete ${this.trnsIds.length} trns in this wallet`
}
+ return null
},
walletId () {
return this.$store.state.wallets.modal.id
@@ -33,7 +34,7 @@ export default {
const trns = this.$store.state.trns.items
const trnsIds = []
for (const trnId in trns) {
- if (trns[trnId].accountId === this.walletId) trnsIds.push(trnId)
+ if (trns[trnId].walletId === this.walletId) trnsIds.push(trnId)
}
return trnsIds
}
diff --git a/src/utils/id.js b/src/utils/id.js
index d1650435..d7f9a232 100644
--- a/src/utils/id.js
+++ b/src/utils/id.js
@@ -1,13 +1,6 @@
import moment from 'moment'
-export const generateDateId = (date) => {
+export default function (date) {
const string = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
- if (date) {
- return `${moment(date).format('YYMMDD[-]HH:mm:ss')}-${string.substring(1, 5)}`
- }
- return `${moment().format('YYMMDD[-]HH:mm:ss')}-${string.substring(1, 5)}`
-}
-
-export const generateSimpleId = () => {
- return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
+ return `${moment(date).format('YYMMDD')}_${string.slice(0, 6)}`
}