Skip to content

Open source personal finance application

feat (currencies): store rates history

ilkome9445ff7f503d
diff --git a/README.md b/README.md
index d82a4b97..0cde467f 100644
--- a/README.md
+++ b/README.md
@@ -59,18 +59,18 @@ $ npm install
".read": "auth != null",
".write": "auth != null"
},
- "users-info": {
- "$uid": {
- ".read": "auth != null && auth.uid == $uid",
- ".write": "auth != null && auth.uid == $uid"
- }
+ "ratesUsd": {
+ ".read": "auth != null",
+ ".write": "auth != null"
}
}
}
```
+
- Go to the Project Overview and find `Get started by adding Firebase to your app` click to `web`.
- Register a app and you will get your config.
- You need to replace config in app's directory `services/firebaseConfig.js` with your config.
+
``` bash
apiKey: 'YOUR_CONFIG',
authDomain: 'YOUR_CONFIG',
diff --git a/store/currencies/actions.js b/store/currencies/actions.js
deleted file mode 100644
index 6f2c5a88..00000000
--- a/store/currencies/actions.js
+++ /dev/null
@@ -1,39 +0,0 @@
-import localforage from 'localforage'
-import dayjs from 'dayjs'
-import { getRatesOf } from './api'
-import { getDataOnce, saveData } from '~/services/firebase/api'
-
-export default {
- async initCurrencies({ rootState, commit }) {
- const uid = rootState.user.user.uid
-
- // user base currency in DB
- const userBaseCurrency = await getDataOnce(`users/${uid}/settings/baseCurrency`) || 'RUB'
-
- // rates of base currency
- const currencyRate = await getDataOnce(`currencies/${userBaseCurrency}`) || {}
-
- let rates = currencyRate.rates
- const today = dayjs().valueOf()
- const isCurrencyUpdatedToday = dayjs(today).isSame(currencyRate.date, 'day')
-
- // get new rates
- if (!currencyRate.rates || !isCurrencyUpdatedToday) {
- rates = await getRatesOf(userBaseCurrency)
- saveData(`currencies/${userBaseCurrency}`, {
- rates,
- date: today,
- })
- }
-
- const currencies = { base: userBaseCurrency, rates }
- commit('setCurrencies', currencies)
- localforage.setItem('finapp.currencies', currencies)
- },
-
- setBaseCurrency({ rootState, dispatch }, baseCurrency) {
- const uid = rootState.user.user.uid
- saveData(`users/${uid}/settings/baseCurrency`, baseCurrency)
- dispatch('initCurrencies')
- },
-}
diff --git a/store/currencies/actions.ts b/store/currencies/actions.ts
new file mode 100644
index 00000000..981802d6
--- /dev/null
+++ b/store/currencies/actions.ts
@@ -0,0 +1,46 @@
+import localforage from 'localforage'
+import dayjs from 'dayjs'
+import { getRatesOfUSD } from './api'
+import { getDataOnce, saveData } from '~/services/firebase/api'
+
+export default {
+ async initCurrencies({ rootState, commit }) {
+ const uid = rootState.user.user.uid
+
+ // User base currency in DB
+ const userBaseCurrency = await getDataOnce(`users/${uid}/settings/baseCurrency`) || 'USD'
+
+ // Rates with date
+ const today = dayjs().startOf('day').valueOf()
+ let ratesBasedOnUsd = await getDataOnce(`ratesUsd/history/${today}`)
+
+ // No rates for today
+ if (!ratesBasedOnUsd) {
+ ratesBasedOnUsd = await getRatesOfUSD()
+ if (!ratesBasedOnUsd) return
+ saveData('ratesUsd/latest', ratesBasedOnUsd)
+ saveData(`ratesUsd/history/${today}`, ratesBasedOnUsd)
+ }
+
+ // Convert rates based on USD to user base currency
+ const ratesConvertedToBase = {}
+ for (const id in ratesBasedOnUsd)
+ ratesConvertedToBase[id] = ratesBasedOnUsd[id] / ratesBasedOnUsd[userBaseCurrency]
+
+ commit('setCurrencies', {
+ base: userBaseCurrency,
+ rates: ratesConvertedToBase,
+ })
+
+ localforage.setItem('finapp.currencies', {
+ base: userBaseCurrency,
+ rates: ratesConvertedToBase,
+ })
+ },
+
+ setBaseCurrency({ rootState, dispatch }, baseCurrency) {
+ const uid = rootState.user.user.uid
+ saveData(`users/${uid}/settings/baseCurrency`, baseCurrency)
+ dispatch('initCurrencies')
+ },
+}
diff --git a/store/currencies/api.js b/store/currencies/api.js
deleted file mode 100644
index c2ef0192..00000000
--- a/store/currencies/api.js
+++ /dev/null
@@ -1,27 +0,0 @@
-import axios from 'axios'
-
-const key = '5976f9fadc5c46e285d764eae24d384b'
-const serviceUrl = `https://openexchangerates.org/api/latest.json?app_id=${key}`
-
-export const getRatesOf = async(baseCurrency) => {
- if (!baseCurrency) {
- console.error('No base currency')
- return
- }
-
- const currencies = await axios.get(serviceUrl)
- const rates = {}
- if (currencies && currencies.data) {
- const ratesBasedOnUsd = currencies.data.rates
- const baseRate = ratesBasedOnUsd[baseCurrency]
-
- // Conver rates to base currency
- for (const id in ratesBasedOnUsd)
- rates[id] = ratesBasedOnUsd[id] / baseRate
-
- return rates
- }
- else {
- console.log('api unavaliable')
- }
-}
diff --git a/store/currencies/api.ts b/store/currencies/api.ts
new file mode 100644
index 00000000..eaed0477
--- /dev/null
+++ b/store/currencies/api.ts
@@ -0,0 +1,17 @@
+import axios from 'axios'
+
+const key = '5976f9fadc5c46e285d7654eae24d384b'
+const serviceUrl = `https://openexchangerates.org/api/latest.json?app_id=${key}`
+
+type Props = Record<string, string>
+
+export const getRatesOfUSD = async(): Promise<Props | false> => {
+ const { data } = await axios.get(serviceUrl)
+
+ if (!data) {
+ console.log('api unavaliable')
+ return false
+ }
+
+ return data.rates
+}
diff --git a/store/currencies/index.js b/store/currencies/index.js
index d6f915ff..c6f47da7 100644
--- a/store/currencies/index.js
+++ b/store/currencies/index.js
@@ -1,9 +1,5 @@
-import dayjs from 'dayjs'
-
export const state = () => ({
- amountSeparator: ' ',
base: 'RUB',
- date: dayjs().valueOf(),
rates: {},
modal: {
show: false,
diff --git a/store/currencies/mutations.js b/store/currencies/mutations.js
index 7fd98af6..d118fa5a 100644
--- a/store/currencies/mutations.js
+++ b/store/currencies/mutations.js
@@ -1,7 +1,6 @@
export default {
- setCurrencies(state, { base, date, rates }) {
+ setCurrencies(state, { base, rates }) {
state.base = base
- state.date = date
state.rates = rates
},