Ensure default currency isn't modified to be a non-currency

This commit is contained in:
Aaron Lindsay 2017-06-21 21:53:01 -04:00
parent 4e73e8b508
commit a42e051f74
4 changed files with 43 additions and 6 deletions

View File

@ -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
};

View File

@ -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 (
<Modal show={this.props.show} onHide={this.props.onClose}>
<Modal.Header closeButton>
<Modal.Title>Cannot Delete Security</Modal.Title>
</Modal.Header>
<Modal.Body>
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}
</Modal.Body>
<Modal.Footer>
<ButtonGroup className="pull-right">
@ -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} />
<DeletionFailedModal
show={this.state.deletionFailedModal}
user={this.props.user}
deletingSecurity={selectedSecurity}
onClose={this.onCloseDeletionFailed}
securityAccounts={this.props.selectedSecurityAccounts} />

View File

@ -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))}
}
}

View File

@ -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()