Skip to content

Commit

Permalink
Add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
JackNeto committed Jan 18, 2020
1 parent 8f4d84c commit ab59651
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 135 deletions.
10 changes: 8 additions & 2 deletions src/store/blockstackStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ export default () => {
const appConfig = new AppConfig()
const userSession = new UserSession({ appConfig })
return {
getItem: (key) => userSession.getFile(key),
setItem: (key, value) => userSession.putFile(key, value)
getItem: (key) => {
if (process.env.NODE_ENV === 'development') console.log('🟢 getItem', key)
return userSession.getFile(key)
},
setItem: (key, value) => {
if (process.env.NODE_ENV === 'development') console.log('🟢 setItem', key)
return userSession.putFile(key, value)
}
}
}
2 changes: 0 additions & 2 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { updateLoginData } from './user/actions'
import types from './user/types'
import avatarImg from '../common/LoginButton/avatar.png'


// Set middlewares
const middlewares = [thunkMiddleware]
if (process.env.NODE_ENV === 'development') {
Expand Down Expand Up @@ -90,7 +89,6 @@ const setPersistor = ({ storageType }) => {
if (process.env.NODE_ENV === 'test') {
store.replaceReducer(rootReducer)
}
// window.location.reload()
} else {
store.replaceReducer(persistReducer(persistConfig, rootReducer))
persistor = persistStore(store)
Expand Down
14 changes: 14 additions & 0 deletions src/store/migrations/0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Add accountType to accounts
export default (state) => {
if (!state.accounts) return state
return {
...state,
accounts: {
...state.accounts,
byId: Object.values(state.accounts.byId).reduce((result, account) => ({
...result,
[account.id]: { ...account, accountType: 'Bank' }
}), {})
}
}
}
11 changes: 11 additions & 0 deletions src/store/migrations/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Cleanup settings (remove snackbarMessage)
export default (state) => {
if (!state.settings) return state
return {
...state,
settings: {
currency: state.settings.currency,
locale: state.settings.locale
}
}
}
21 changes: 21 additions & 0 deletions src/store/migrations/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Update transaction amount
// Remove dates array from exchangeRates
export default (state) => {
if (!state.transactions) return state
return {
...state,
transactions: {
list: state.transactions.list.map((transaction) => ({
...transaction,
amount: { accountCurrency: transaction.amount, localCurrency: null }
}))
},
exchangeRates: Object.keys(state.exchangeRates).reduce((result, currency) => {
const { dates, ...exchangeRates } = state.exchangeRates[currency]
return {
...result,
[currency]: exchangeRates
}
}, {})
}
}
62 changes: 62 additions & 0 deletions src/store/migrations/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import uuid from 'uuid/v4'

export default (state) => {
if (!('budget' in state)) return state
if (Object.keys(state.budget.rules).length === 0) return state
const { transactions, budget: { rules } } = state

const existingRules = {}
const newRules = {}
const changes = {}
transactions.list.forEach((transaction) => {
if (
!transaction.ruleId
&& transaction.categoryId
&& transaction.description in rules
&& rules[transaction.description].categoryId === transaction.categoryId
) {
if (!(transaction.description in existingRules)) {
existingRules[transaction.description] = { }
}
if (!(transaction.accountId in existingRules[transaction.description])) {
existingRules[transaction.description][transaction.accountId] = {
categoryId: transaction.categoryId,
transactions: []
}
}
existingRules[transaction.description][transaction.accountId].transactions.push(transaction)
}
})

Object.keys(existingRules).forEach((match) => {
Object.keys(existingRules[match]).forEach((accountId) => {
const ruleId = uuid()
newRules[ruleId] = {
id: ruleId,
accountId,
attributes: { categoryId: existingRules[match][accountId].categoryId },
filterBy: { description: { type: 'equals', value: match } }
}
existingRules[match][accountId].transactions.forEach((transaction) => {
changes[transaction.id] = { ...transaction, ruleId }
})
})
})

return {
...state,
transactions: {
...state.transactions,
list: state.transactions.list.map((transaction) => {
if (transaction.id in changes) return changes[transaction.id]
return transaction
})
},
budget: {
...state.budget,
rules: Object.values(state.budget.rules).reduce(
(res, rule) => ('id' in rule ? { ...res, [rule.id]: rule } : res), newRules
)
}
}
}
Loading

0 comments on commit ab59651

Please sign in to comment.