From a42e051f745a470c7b2ec72d11ad42aa37ea8e34 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Wed, 21 Jun 2017 21:53:01 -0400 Subject: [PATCH] Ensure default currency isn't modified to be a non-currency --- js/actions/ErrorActions.js | 12 ++++++++++++ js/components/SecuritiesTab.js | 22 +++++++++++++++++----- js/containers/SecuritiesTabContainer.js | 6 +++++- securities.go | 9 +++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/js/actions/ErrorActions.js b/js/actions/ErrorActions.js index 289a14c..d25289c 100644 --- a/js/actions/ErrorActions.js +++ b/js/actions/ErrorActions.js @@ -32,6 +32,17 @@ function clientError(error) { }; } +function userError(error) { + var e = new Error(); + e.ErrorId = 999; + e.ErrorString = error; + + return { + type: ErrorConstants.ERROR_USER, + error: e + }; +} + function clearError() { return { type: ErrorConstants.CLEAR_ERROR, @@ -42,5 +53,6 @@ module.exports = { serverError: serverError, ajaxError: ajaxError, clientError: clientError, + userError: userError, clearError: clearError }; diff --git a/js/components/SecuritiesTab.js b/js/components/SecuritiesTab.js index 919f5f1..4db29d8 100644 --- a/js/components/SecuritiesTab.js +++ b/js/components/SecuritiesTab.js @@ -136,7 +136,7 @@ class AddEditSecurityModal extends React.Component { symbol: template.Symbol, precision: template.Precision, type: template.Type, - alternateid: template.AlternateId + alternateid: template.AlternateId }); } handleCancel() { @@ -287,13 +287,17 @@ class AddEditSecurityModal extends React.Component { class DeletionFailedModal extends React.Component { render() { + var msg = "We are unable to delete your " + this.props.deletingSecurity.Name + " security because it is in use by " + this.props.securityAccounts.length + " account(s). Please change those accounts to use other securities and try again."; + if (this.props.user.DefaultCurrency == this.props.deletingSecurity.SecurityId) { + msg = "We are unable to delete your default currency: " + this.props.deletingSecurity.Name + ". To delete this security, select another as your default currency under Account Settings."; + } return ( Cannot Delete Security - We are unable to delete this security because it is in use by {this.props.securityAccounts.length} account(s). Please change those accounts to use other securities and try again. + {msg} @@ -366,8 +370,10 @@ class SecuritiesTab extends React.Component { this.setState({editingSecurity: true}); } handleDeleteSecurity() { - if (this.props.selectedSecurityAccounts.length == 0) - this.props.onDeleteSecurity(this.props.securities[this.props.selectedSecurity]); + // check if user has this as their default currency + var security = this.props.securities[this.props.selectedSecurity]; + if (this.props.selectedSecurityAccounts.length == 0 && security.SecurityId != this.props.user.DefaultCurrency) + this.props.onDeleteSecurity(security); else this.setState({deletionFailedModal: true}); } @@ -383,7 +389,12 @@ class SecuritiesTab extends React.Component { } handleEditingSubmit(security) { this.setState({editingSecurity: false}); - this.props.onUpdateSecurity(security); + + if (security.SecurityId == this.props.user.DefaultCurrency && security.Type != SecurityType.Currency) { + this.props.onUserError("Unable to modify the default currency to be a non-currency security"); + } else { + this.props.onUpdateSecurity(security); + } } handleCloseDeletionFailed() { this.setState({deletionFailedModal: false}); @@ -412,6 +423,7 @@ class SecuritiesTab extends React.Component { securityTemplates={this.props.securityTemplates} /> diff --git a/js/containers/SecuritiesTabContainer.js b/js/containers/SecuritiesTabContainer.js index a3228a9..8c95d58 100644 --- a/js/containers/SecuritiesTabContainer.js +++ b/js/containers/SecuritiesTabContainer.js @@ -2,6 +2,8 @@ var connect = require('react-redux').connect; var SecurityActions = require('../actions/SecurityActions'); var SecurityTemplateActions = require('../actions/SecurityTemplateActions'); +var ErrorActions = require('../actions/ErrorActions'); + var SecuritiesTab = require('../components/SecuritiesTab'); function mapStateToProps(state) { @@ -12,6 +14,7 @@ function mapStateToProps(state) { selectedSecurityAccounts.push(state.accounts.map[accountId]); } return { + user: state.user, securities: state.securities.map, security_list: state.securities.list, selectedSecurityAccounts: selectedSecurityAccounts, @@ -26,7 +29,8 @@ function mapDispatchToProps(dispatch) { onUpdateSecurity: function(security) {dispatch(SecurityActions.update(security))}, onDeleteSecurity: function(securityId) {dispatch(SecurityActions.remove(securityId))}, onSelectSecurity: function(securityId) {dispatch(SecurityActions.select(securityId))}, - onSearchTemplates: function(search, type, limit) {dispatch(SecurityTemplateActions.search(search, type, limit))} + onSearchTemplates: function(search, type, limit) {dispatch(SecurityTemplateActions.search(search, type, limit))}, + onUserError: function(error) {dispatch(ErrorActions.userError(error))} } } diff --git a/securities.go b/securities.go index f670824..006b2cb 100644 --- a/securities.go +++ b/securities.go @@ -148,6 +148,15 @@ func UpdateSecurity(s *Security) error { return err } + user, err := GetUserTx(transaction, s.UserId) + if err != nil { + transaction.Rollback() + return err + } else if user.DefaultCurrency == s.SecurityId && s.Type != Currency { + transaction.Rollback() + return errors.New("Cannot change security which is user's default currency to be non-currency") + } + count, err := transaction.Update(s) if err != nil { transaction.Rollback()