All files / src/methods/userRegistration index.js

65.79% Statements 25/38
76.92% Branches 20/26
42.86% Functions 3/7
65.79% Lines 25/38
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193          1x                                                                                                                                                                                             5x             5x       5x 5x 5x   5x   5x     3x 3x 3x 3x 3x         3x 3x 2x 2x     1x       1x 1x           1x                           5x                           5x 5x                 5x                
import { cleanError, manageError } from '../../errorManager';
import { isVoid, isValid } from '../../validation';
import config from '../../config';
import Method from '../Method';
 
const formModel = {
    buttonAddEmail: 'frmAddEmail',
    buttonSkip: 'frmAddEmailSkip',
    buttonCancelAddEmail: 'frmAddEmailReturn',
    email: 'email',
    switchEye: 'add_mail_password_switch_type',
    password: 'password'
};
 
/**
 * @memberOf Useris
 * @function userRegistration
 *
 * @description Validate and recognize the object to add a new newton identity.
 *
 * Remember to check if an identy is already present.
 * If yes you have to delete the old and add the new one
 *
 *
 * It must be used with the **smartaddemail** 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 {String} [options.smsTemplate=''] the template of the sms sent (msisdn add identity)
 * @param {boolean} [options.skipRegister=0] allow a user to have a generic identity
 * @param {boolean} [options.ohterParams=0] params to substitute for mail
 * @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.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.userRegistration({
 *      element: document.getElementById('elementId'),
 *      regexObject: {
 *          msidn: '',
 *          pin: '',
 *          email: '',
 *          password: '',
 *          generic: '',
 *          genericPassword: ''
 *      },
 *      skipRegister: 1,
 *      smsTemplate: 'Hello, welcome to the service!',
 *      otherParams: '{}'
 *  }, {
 *      onSuccess: function(userCredentials){
 *          // remember to check if an identy is already present.
 *          // If yes you have to delete the old and add the new one
 *          callsToNewton.addIdentity(userCredentials);
 *      },
 *      onCancel: function(){
 *          window.location.href = '#!';
 *      }
 *      onError: function(error){
 *          console.log('Error:', error);
 *          manageError(error);
 *      },
 *      onTemplateLoaded: function(tmplInfo){
 *          console.log('Template loaded', tmplInfo);
 *      }
 *  });
 *
 * @returns {object}
 *  **Email case:**
 *  <pre>
 *  {
 *      email: string,
 *      password: string,
 *      skipped: boolean,
 *      type: 'email',
 *      smsTemplate: string,
 *      params: Object
 *  }
 *  </pre>
 *  **Skipped case:**
 *  <pre>
 *  {
 *      skipped: boolean,
 *      type: 'generic',
 *      smsTemplate: string
 *  }
 *  </pre>
 */
export default class UserRegistration extends Method {
    init = (options) => {
        this.smartsectionObj = {
            subsection: 'smartaddemail',
            skip: options.skipRegister || 0,
            loadForm: options.element || false,
            access_token: options.access_token || '',
            doNotLoad: options.doNotLoad || false
        };
        this.initUserView();
    };
 
    attachEvents = () => {
        const buttonAddEmail = this.UserViewInstance.getElement(formModel.buttonAddEmail);
        const buttonSkip = this.UserViewInstance.getElement(formModel.buttonSkip);
        const buttonCancelAddEmail = this.UserViewInstance
            .getElement(formModel.buttonCancelAddEmail);
        const switchEye = this.UserViewInstance.getElement(formModel.switchEye);
 
        this.UserViewInstance.attachEvent(buttonAddEmail, {
            eventName: 'click',
            event: (e) => {
                e.preventDefault();
                cleanError();
                const email = this.UserViewInstance.getValue('email');
                const password = this.UserViewInstance.getValue('password');
                const parObj = {
                    EMAIL: email,
                    USERNAME: email
                };
 
                try {
                    if (isVoid(email) || !isValid(email, this.regexObject.email)) {
                        manageError('useris-error-msg');
                        return;
                    }
 
                    Iif (isVoid(password) || !isValid(password, this.regexObject.password)) {
                        manageError('useris-error-password');
                        return;
                    }
                    let params = {};
                    Iif (this.options.otherParams) {
                        params = Object.keys(this.options.otherParams).reduce((obj, key) => {
                            obj[key] = parObj[this.options.otherParams[key].replace(':', '')];
                            return obj;
                        }, {});
                    }
                    this.callback('onSuccess', {
                        email,
                        password,
                        params,
                        skipped: 0,
                        type: 'email',
                        smsTemplate: config.get('redirectToMipRegisterAtStart') ? this.options.smsTemplate : false,
                    });
                } catch (error) {
                    manageError(error);
                }
            }
        });
 
        Iif (this.options.skipRegister || config.get('skipRegister')) {
            this.UserViewInstance.attachEvent(buttonSkip, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    this.callback('onSuccess', {
                        skipped: 1,
                        type: 'generic',
                        smsTemplate: this.options.smsTemplate
                    });
                }
            });
        }
 
        Eif (switchEye) {
            this.UserViewInstance.attachEvent(switchEye, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    this.UserViewInstance.switchInputType(formModel.password, switchEye.id);
                }
            });
        }
 
        this.UserViewInstance.attachEvent(buttonCancelAddEmail, {
            eventName: 'click',
            event: (e) => {
                e.preventDefault();
                this.callback('onCancel');
            }
        });
    }
}