requirejs([ 'login/changepassword', 'login/forgotpassword', 'login/onetimepin', 'login/register' ]); define(['angular', 'lib/json-formatter', 'utilities', 'validators'], function (require) { return angular.module('login', ['utilities', 'validators', 'jsonFormatter']) .controller('Login', ['$scope', '$http', '$q', "$rootScope", "$timeout", function ($scope, $http, $q, $rootScope, $timeout) { $scope.LoginAction = "/Login"; $scope.LoginReferenceNumber = false; function validateReferenceNumber(v) { if ($scope.LoginForm.ReferenceNumber) { var isValid = /^[0-9]{9}(?:\|[a-zA-Z0-9-_+/=]{23})?$/.test(v); $scope.LoginForm.ReferenceNumber.$setValidity("referenceNumber", isValid); if (!isAdmin && /^[0-9]{9}$/.test(v)) { $scope.LoginForm.ReferenceNumber.$setValidity("adminOnly", false); isValid = false; } else { $scope.LoginForm.ReferenceNumber.$setValidity("adminOnly", true); } if (isValid) { function callback(response) { if (v == $scope.ReferenceNumber) { if (response && response.data && response.data.EmailAddress) { $scope.EmailAddress = response.data.EmailAddress; $scope.Password = "Password@1"; $scope.ReferenceNumber = response.data.ReferenceNumber; $scope.LoginForm.ReferenceNumber.$setValidity("found", true); } else { $scope.EmailAddress = null; $scope.Password = null; $scope.LoginForm.ReferenceNumber.$setValidity("found", false); } } } $http.post("/Login/GetReferenceNumberInfo", { referenceNumber: v }).then(callback); } } } $scope.$watch("EmailAddress", function (EmailAddress) { if (EmailAddress) { $http({ method: 'POST', url: '/preloadlogin/PreloadLoginEmailAddress', data: { EmailAddress: EmailAddress } }); } }); $scope.$watch("ReferenceNumber", validateReferenceNumber); $scope.$watch("LoginReferenceNumber", function (v) { if (v) { validateReferenceNumber($scope.ReferenceNumber); $scope.LoginAction = "/Login/DebugLogin"; } else { if ($scope.LoginForm.ReferenceNumber) { $scope.LoginForm.ReferenceNumber.$setValidity("referenceNumber", true); $scope.LoginForm.ReferenceNumber.$setValidity("found", true); } $scope.LoginAction = "/Login"; } }); $scope.selectEmail = function selectEmail() { $scope.LoginReferenceNumber = false; } $scope.selectPolicyNumber = function selectPolicyNumber() { $scope.LoginReferenceNumber = $scope.isDebugMode || $scope.isAdmin; } $scope.SubmitLogin = function () { $timeout(function () { $scope.QuoteStarting = true; }, 0); }; $scope.QuoteStarting = false; }]) .controller('OTPLogin', ['$scope', '$http', '$q', '$mdDialog', '$window', 'Helpers', function ($scope, $http, $q, $mdDialog, $window, Helpers) { $scope.otpLogin = { IsIdnumber: true, IdNumber: null, Error: false }; $scope.$watch('otpLogin.IdNumber', function (newValue, oldValue) { if (newValue != oldValue) { $scope.otpLogin.Error = false; if ($scope.otpLogin.IdNumber && $scope.otpLogin.IdNumber.length === 13) { $scope.LoadDashboard(); } } }); $scope.LoadDashboard = function () { $http({ method: 'POST', url: '/preloadlogin/PreloadLoginIdNumber', data: { idNumber: $scope.otpLogin.IdNumber } }); }; $scope.SendOTP = function (number, callback) { $scope.otpLogin.OTPError = false; $scope.otpLogin.OTPSuccess = false; $http.post('/Login/SendOTP', number).then(function (data) { $scope.otpLogin.OTPSent = true; $scope.otpLogin.SelectedNumber = number; if (data.data) { $scope.otpLogin.OTPSentMessage = data.data; } if (callback) callback(data); }); }; if (typeof (_redirectUrl) != 'undefined') var redirectTo = _redirectUrl; $scope.VerifyOTP = function () { $scope.otpLogin.OTPError = false; $scope.otpLogin.OTPSuccess = false; $scope.otpLogin.VerifyingOTP = true; $http.post('/Login/VerifyOTP', $scope.otpLogin.SelectedNumber).then(function (data) { $scope.otpLogin.OTPError = !data.data.Success; $scope.otpLogin.OTPSuccess = !$scope.otpLogin.OTPError; if ($scope.otpLogin.OTPError) { $scope.otpLogin.OTPErrorMessage = data.data.Message; } else { if (redirectTo != null) window.location = redirectTo; else $window.location.href = data.data.RedirectUrl; } $scope.otpLogin.VerifyingOTP = false; }); }; $scope.ResetOTP = function () { $scope.otpLogin.OTPError = false; $scope.otpLogin.OTPSuccess = false; if ($scope.otpLogin.ContactNumbers.length <= 1) { $scope.SendOTP($scope.otpLogin.SelectedNumber); } else { $scope.otpLogin.SelectedNumber = null; $scope.otpLogin.OTPSent = false; } }; $scope.GetLinkedContactNumbers = function () { $scope.otpLogin.SendingOTP = true; $scope.otpLogin.Error = false; $scope.otpLogin.OTPSent = false; $scope.getDisplayScn(); $http.post('/Login/GetLinkedContactNumbers', { idNumber: $scope.otpLogin.IdNumber }).then( function (data) { $scope.otpLogin.SendingOTP = false; if (data.data === "virseker") window.location = "/login?reminder=virseker"; else if (data.data.length > 0) { $scope.otpLogin.ContactNumbers = data.data; if ($scope.otpLogin.ContactNumbers.length <= 1) { $scope.SendOTP($scope.otpLogin.ContactNumbers[0], function (data) { $scope.otpLogin.SelectedNumber = $scope.otpLogin.ContactNumbers[0]; dialogFunction(otpDialogTemplate)(); }); } else { dialogFunction(otpDialogTemplate)(); } } else { $scope.otpLogin.Error = true; } }, function () { $scope.otpLogin.SendingOTP = false; }); }; var otpDialogTemplate = Helpers.dialog("otpLoginDialog"); function dialogFunction(template, accept, reject) { return function dialogFunctionFunction(event) { $scope.otpLogin.Dialog = $mdDialog.show({ clickOutsideToClose: false, disableParentScroll: false, scope: $scope, preserveScope: true, targetEvent: event, template: template, controller: ['$scope', '$mdDialog', function ($scope, $mdDialog) { $scope.otpLogin.closeDialog = $mdDialog.hide; }] }).then(accept, reject); }; } $scope.getDisplayScn = function () { $http.post('/Login/GetDisplayScn', {}).then(function (data) { $scope.DisplayScn = data.data; }); }; $scope.GACollect = function (ReferenceNumber, TotalPremium) { $http({ method: 'POST', url: "/Login/SendtoGACollect", params: { referencenumber: ReferenceNumber, totalpremium: TotalPremium } }); }; $scope.IncrementBrokerSales = function () { $http({ method: 'POST', url: "/Login/IncrementBrokerSales" }); }; }]); });