All files / src/methods/changePassword index.js

82.5% Statements 33/40
77.27% Branches 17/22
50% Functions 3/6
82.5% Lines 33/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165        1x                                                                                                                                     5x           5x       5x   5x   5x   5x     5x 5x     5x 5x   5x 5x   5x 5x 1x 1x     4x 1x 1x     3x 1x 1x     2x 1x 1x   1x                     5x 5x                     5x 5x                   5x 5x                  
import { cleanError, manageError } from '../../errorManager';
import { isValid, isVoid } from '../../validation';
import Method from '../Method';
 
const formModel = {
    buttonChange: 'frmChangePsw',
    buttonCancelChangePsw: 'frmChangePswReturn',
    oldPassword: 'OldPassword',
    password: 'password',
    oldPasswordSwitchEye: 'change_psw_old_password_switch_type',
    newPasswordSwitchEye: 'change_psw_password_switch_type'
};
 
/**
 * @memberOf Useris
 * @function changePassword
 *
 * @description Validate and recognize the object to make the user change its password.
 *
 * It must be used with the **newtonsmartchangepassword** template.
 *
 * @param {Object} options Options object
 * @param {Object} [options.element=false] The div element where the html's tmpl will be injected
 * @param {Object} [options.regexObject={}]
 * **Optionals** Contains all the regular expressions for input validation
 * (msisdn, pin, email, password and generic regex are allowed)
 * @param {String} [options.acccess_token='']
 * **Optionals** if true, the tmpl calls will be called with the params access_token filled
 * @param {Boolean} [options.doNotLoad=false] **Optionals** if true,
 * this params will tell useris to not load the template (It doesn't make the apicall)
 * @param {Object} callbackObject Manage the success and error callback
 * @param {function} [callbackObject.onSuccess='no-default']
 * Success callback (code managed by the app)
 * @param {function} [callbackObject.onCancel='no-default']
 * Cancel callback (code managed by the app)
 * @param {function} [callbackObject.onError='no-default']
 * Error callback (code managed by the app)
 * @param {function} [callbackObject.onTemplateLoaded='no-default']
 * Callback executed when the template has been loaded.
 *
 * @example
 *  Useris.changePassword({
 *      element: document.getElementById('elementId'),
 *  }, {
 *      onSuccess: function(userCredentials){
 *          console.log(userCredentials);
 *          callsToNewtonAdapter.changePassword(userCredentials);
 *      },
 *      onCancel: function(){
 *          window.history.back();
 *      },
 *      onError: function(error){
 *          console.log('Error:', error);
 *          manageError(error);
 *      },
 *      onTemplateLoaded: function(tmplInfo){
 *          console.log('Template loaded', tmplInfo);
 *      }
 *  });
 *
 * @returns {object}
 * <pre>
 *  {
 *      oldPassword: string,
 *      newPassword: string,
 *      repeatedPassword: string
 *  }
 * </pre>
 */
export default class ChangePassword extends Method {
    init = (options) => {
        this.smartsectionObj = {
            subsection: 'newtonsmartchangepassword',
            loadForm: options.element || false,
            access_token: options.access_token || '',
            doNotLoad: options.doNotLoad || false
        };
        this.initUserView();
    };
 
    attachEvents = () => {
        const changePasswordButton = this.UserViewInstance
            .getElement(formModel.buttonChange);
        const buttonCancelChangePsw = this.UserViewInstance
            .getElement(formModel.buttonCancelChangePsw);
        const oldPasswordSwitchEye = this.UserViewInstance
            .getElement(formModel.oldPasswordSwitchEye);
        const newPasswordSwitchEye = this.UserViewInstance
            .getElement(formModel.newPasswordSwitchEye);
 
        Eif (changePasswordButton) {
            this.UserViewInstance.attachEvent(changePasswordButton, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    cleanError();
 
                    const oldPassword = this.UserViewInstance.getValue(formModel.oldPassword);
                    const newPassword = this.UserViewInstance.getValue(formModel.password);
 
                    try {
                        if (isVoid(oldPassword)) {
                            manageError('useris-error-old-password');
                            return;
                        }
 
                        if (isVoid(newPassword)) {
                            manageError('useris-error-password');
                            return;
                        }
 
                        if (oldPassword === newPassword) {
                            manageError('useris-error-password-not-changed');
                            return;
                        }
 
                        if (!isValid(newPassword, this.regexObject.password)) {
                            manageError('useris-error-password');
                            return;
                        }
                        this.callback('onSuccess', {
                            oldPassword,
                            newPassword
                        });
                    } catch (error) {
                        manageError(error);
                    }
                }
            });
        }
 
        Eif (oldPasswordSwitchEye) {
            this.UserViewInstance.attachEvent(oldPasswordSwitchEye, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    this.UserViewInstance.switchInputType(
                        formModel.oldPassword, oldPasswordSwitchEye.id
                    );
                }
            });
        }
 
        Eif (newPasswordSwitchEye) {
            this.UserViewInstance.attachEvent(newPasswordSwitchEye, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    this.UserViewInstance
                        .switchInputType(formModel.password, newPasswordSwitchEye.id);
                }
            });
        }
 
        Eif (buttonCancelChangePsw) {
            this.UserViewInstance.attachEvent(buttonCancelChangePsw, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    this.callback('onCancel');
                }
            });
        }
    }
}