From 8f4d84c2abffc8b93d22054d39932e54e320a67f Mon Sep 17 00:00:00 2001 From: Jack Neto Date: Tue, 14 Jan 2020 17:09:00 -0500 Subject: [PATCH] fix transaction updates --- package.json | 3 +- .../Transactions/TransactionDialog.jsx | 1 + src/store/budget/reducer.js | 1 - src/store/migrations/index.js | 35 +++++++++++++++ .../transactions/__tests__/reducer.test.js | 44 ++++++++----------- src/store/transactions/reducer.js | 10 +---- 6 files changed, 59 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index 2228ffe..cff6e04 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "lint": "eslint src --ext '.js,.jsx' --config .eslintrc", "lint:fix": "npm run lint --fix", "deploy:test": "npm run build && aws s3 sync build/ s3://entaxy-test --delete", + "postdeploy:test": "aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_ID --paths '/*'", "deploy:staging": "npm run build && aws s3 sync build/ s3://staging.entaxy.io --delete", "postdeploy:staging": "aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_ID --paths '/*'", "deploy:production": "npm run build && aws s3 sync build/ s3://entaxy-production --delete", @@ -143,4 +144,4 @@ } } } -} +} \ No newline at end of file diff --git a/src/core/Accounts/Transactions/TransactionDialog.jsx b/src/core/Accounts/Transactions/TransactionDialog.jsx index 28d190b..00a3678 100644 --- a/src/core/Accounts/Transactions/TransactionDialog.jsx +++ b/src/core/Accounts/Transactions/TransactionDialog.jsx @@ -452,6 +452,7 @@ export default compose( } } + console.log({ ruleId: transaction.ruleId, rules: budget.rules }) const ruleAttributes = {} if (transaction.ruleId) { const rule = budget.rules[transaction.ruleId] diff --git a/src/store/budget/reducer.js b/src/store/budget/reducer.js index f8b3ae0..a7b6c09 100644 --- a/src/store/budget/reducer.js +++ b/src/store/budget/reducer.js @@ -156,7 +156,6 @@ export default (state = initialState, { type, payload }) => { return { ...state, rules: { - ...state.rules, [payload.id]: payload } } diff --git a/src/store/migrations/index.js b/src/store/migrations/index.js index 894042e..9954628 100644 --- a/src/store/migrations/index.js +++ b/src/store/migrations/index.js @@ -1,3 +1,5 @@ +import uuid from 'uuid/v4' + const migrations = { // Add accountType to accounts 0: (state) => { @@ -45,6 +47,39 @@ const migrations = { }, {}) } } + // 3: (state) => { + // if (!state.budget) return state + // // collect accountIds + // state.transactions.list.forEach((transaction) => { + // if (transaction.categoryId === categoryId) { + + // } + // }) + + // return { + // ...state, + // budget: { + // ...state.budget, + // rules: Object.keys(state.budget.rules).reduce((res, match) => { + // const ruleId = uuid() + // const { categoryId } = state.budget.rules[match] + // return { + // id: ruleId, + // accountId: account.id, + // attributes: { categoryId }, + // filterBy: { description: { type: 'equals', value: match } } + // } + // }) + // }, + // exchangeRates: Object.keys(state.exchangeRates).reduce((result, currency) => { + // const { dates, ...exchangeRates } = state.exchangeRates[currency] + // return { + // ...result, + // [currency]: exchangeRates + // } + // }, {}) + // } + // } } export default migrations diff --git a/src/store/transactions/__tests__/reducer.test.js b/src/store/transactions/__tests__/reducer.test.js index b4f7376..c6a16fe 100644 --- a/src/store/transactions/__tests__/reducer.test.js +++ b/src/store/transactions/__tests__/reducer.test.js @@ -1,3 +1,4 @@ +import faker from 'faker' import transactionReducer, { initialState } from '../reducer' import types from '../types' @@ -58,20 +59,15 @@ describe('transaction reducer', () => { describe('UPDATE_TRANSACTION', () => { it('should handle UPDATE_TRANSACTION', () => { const type = types.UPDATE_TRANSACTION - const state = { - ...initialState, - list: [ - { ...transaction, id: 1, description: 'old name 1' }, - { ...transaction, id: 2, description: 'old name 2' } - ] - } - const payload = { id: 1, description: 'new name 1' } + const transactions = [ + { ...transaction, id: faker.random.uuid(), description: 'old name 1' }, + { ...transaction, id: faker.random.uuid(), description: 'old name 2' } + ] + const state = { ...initialState, list: transactions } + const payload = { ...transactions[0], description: 'new name 1' } expect(transactionReducer(state, { type, payload })).toEqual({ ...state, - list: [ - { ...transaction, id: 1, description: 'new name 1' }, - { ...transaction, id: 2, description: 'old name 2' } - ] + list: [{ ...transactions[0], description: 'new name 1' }, transactions[1]] }) }) }) @@ -79,24 +75,22 @@ describe('transaction reducer', () => { describe('UPDATE_TRANSACTIONS', () => { it('should handle UPDATE_TRANSACTIONS', () => { const type = types.UPDATE_TRANSACTIONS - const state = { - ...initialState, - list: [ - { ...transaction, id: 1, description: 'old name 1' }, - { ...transaction, id: 2, description: 'old name 2' }, - { ...transaction, id: 3, description: 'old name 3' } - ] - } + const transactions = [ + { ...transaction, id: faker.random.uuid(), description: 'old name 1' }, + { ...transaction, id: faker.random.uuid(), description: 'old name 2' }, + { ...transaction, id: faker.random.uuid(), description: 'old name 3' } + ] + const state = { ...initialState, list: transactions } const payload = { - 1: { description: 'new name 1' }, - 2: { description: 'new name 2' } + [transactions[0].id]: { ...transactions[0], description: 'new name 1' }, + [transactions[1].id]: { ...transactions[1], description: 'new name 2' } } expect(transactionReducer(state, { type, payload })).toEqual({ ...state, list: [ - { ...transaction, id: 1, description: 'new name 1' }, - { ...transaction, id: 2, description: 'new name 2' }, - { ...transaction, id: 3, description: 'old name 3' } + { ...transactions[0], description: 'new name 1' }, + { ...transactions[1], description: 'new name 2' }, + { ...transactions[2], description: 'old name 3' } ] }) }) diff --git a/src/store/transactions/reducer.js b/src/store/transactions/reducer.js index 845b8c2..1f6f42c 100644 --- a/src/store/transactions/reducer.js +++ b/src/store/transactions/reducer.js @@ -18,10 +18,7 @@ export default (state = initialState, { type, payload }) => { return { ...state, list: state.list.map((transaction) => { - if (transaction.id === payload.id) { - return { ...transaction, ...payload } - } - return transaction + return transaction.id === payload.id ? payload : transaction }) } case types.UPDATE_TRANSACTIONS: @@ -29,10 +26,7 @@ export default (state = initialState, { type, payload }) => { return { ...state, list: state.list.map((transaction) => { - if (transaction.id.toString() in payload) { - return { ...transaction, ...payload[transaction.id] } - } - return transaction + return transaction.id in payload ? payload[transaction.id] : transaction }) } case types.DELETE_TRANSACTIONS: