UNPKG

client-ui

Version:

Testing implementation of nodeJs Backend, angular frontend, and hopefully in a way that this can be deployed to s3/cloudfront

436 lines (393 loc) 17.2 kB
(function () { 'use strict'; angular.module(moduleName).controller('incomeSummaryController', incomeSummaryController); incomeSummaryController.$inject = ['$scope', 'loan', '$state', '$stateParams', '$interval', 'currentLoan', "errorHandler", "$q", '$rootScope']; function incomeSummaryController($scope, loanService, $state, $stateParams, $interval, currentLoan, errorHandler, $q, $rootScope) { var self = this; var refreshLoan = { interval : undefined, counter : 0, mesg : '' }; self.currentLoan = currentLoan; self.includeProvider = includeProvider; self.excludeProvider = excludeProvider; self.setStatusGoNext = setStatusGoNext; self.goToNextStep = goToNextStep; self.updateLoanGoNext = updateLoanGoNext; self.requestBankLogin = requestBankLogin; self.submitBankName = submitBankName; self.resetBankChoice = resetBankChoice; self.handleSourceChange = handleSourceChange; self.reloadState = reloadState; self.onUploadSuccess = onUploadSuccess; self.generateUploadUrl = generateUploadUrl; self.init = init; self.setVerified = setVerified; self.talxHit = talxHit; self.showAddMoreIncome = false; self.onFileAdded = onFileAdded; self.onRemoveItem = onRemoveItem; function init() { self.allowedDocTypes = null; self.yodleeFailReason = getDescendantProp('incomeSummary.providers.decisionLogic.failReason', self.currentLoan) || null; self.bankLoginError = null; self.refreshLoanError = null; self.searchedBankName = null; self.bankSearchResults = null; self.clientBank = null; self.uploadedDocs = []; for (var i in self.currentLoan.documents) { if (self.currentLoan.documents[i].documentPurpose === "incomeVerification") { self.uploadedDocs.push(self.currentLoan.documents[i]); } } self.hasUploadedDocs = self.uploadedDocs.length > 0; } $scope.$on('income-document-received', function (event, args) { self.currentLoan = loanService.getCurrentLoan(); }); if (!self.currentLoan.incomeSummary.status) { refreshLoanProp('incomeSummary.status', goToNextStep, 15); } function onFileAdded(item) { // self.allowedDocTypes = globalConfig.documentTypes.incomeVerification; self.uploadDocType = "UN"; } function onRemoveItem(item) { self.allowedDocTypes = null; } function talxHit() { var talxHit = self.currentLoan.incomeSummary.providers && self.currentLoan.incomeSummary.providers.theWorkNumber && self.currentLoan.incomeSummary.providers.theWorkNumber.amount > 0; return talxHit; } function automaticIncomeVerification() { refreshLoanProp('incomeSummary.status', handleIncomeSummaryStatusChange); loanService.automaticIncomeVerification() .then(function (res) { }, function (res) { handleError(res); }); } function requestBankLogin(bank) { self.clientBank = bank; self.working = true; self.bankLoginError = null; self.yodleeFailReason = null; loanService.updateIncomeStatus('choseYodlee') .then(function (loan) { self.currentLoan = loan; return loanService.requestBankLogin(self.clientBank.ContentServiceId) }) .then(function (requestCode) { self.working = false; if(config.envName === 'dev') { self.bankLoginIframeSrc = "http://decisionlogic-widget.dev.rockloans.com/Service.aspx?requestCode=" + requestCode; } else { self.bankLoginIframeSrc = "https://widget.decisionlogic.com/Service.aspx?requestCode=" + requestCode; } refreshLoanProp('incomeSummary.status', handleIncomeSummaryStatusChange, 150); // 5 minutes }, function (res) { handleError(res); self.bankLoginError = "Sorry for the inconvenience! We seem to be experiencing technical difficulties."; }); } function submitBankName() { self.bankSearchResults = null; self.working = true; // on dev environment, fake this call if not "decisionlogic.dev." etc if (window.location.hostname.indexOf('.dev.') > 0 && window.location.hostname.indexOf('decisionlogic') < 0) { return automaticIncomeVerification(); } self.yodleeFailReason = null; self.bankLoginError = null; self.clientBank = null; loanService.bankNameSearch(self.searchedBankName) .then(function (foundBanks) { self.working = false; self.bankSearchResults = foundBanks; }, function (res) { handleError(res); self.bankLoginError = "Sorry for the inconvenience! We seem to be experiencing technical difficulties."; }); } function resetBankChoice() { self.bankLoginIframeSrc = null; self.clientBank = null; self.working = false; stopRefreshLoan(); } function handleIncomeSummaryStatusChange(newStatus) { switch (newStatus) { case 'yodleeLoginSuccess': refreshLoanProp('incomeSummary.status', handleIncomeSummaryStatusChange, 15); // 30 seconds break; case 'yodleeReportSuccess': self.working = false; break; case 'choseManual': self.working = false; var p = self.currentLoan.incomeSummary.providers.decisionLogic; if (p.provider === 'decisionLogic' && p.failReason) { self.yodleeFailReason = p.failReason; self.currentLoan.incomeSummary.status = 'choseYodlee'; } default: self.working = false; break; } // unset iframe src self.bankLoginIframeSrc = ''; } function refreshLoanProp(prop, cb, maxRetries) { var unWatch = $scope.$watch( function () { return getDescendantProp(prop, self.currentLoan); }, function (newVal, oldVal) { if (newVal !== oldVal) { stopRefreshLoan(); unWatch(); if (typeof cb === 'function') { return cb(newVal); } return newVal; } } ); autoRefreshLoan(null, maxRetries || 0); } function getDescendantProp(prop, obj) { // default to look in currentLoan var obj = obj || self.currentLoan, prop = prop.split('.'); while (prop.length) { var p = prop.shift(); if (obj && obj[p]) { obj = obj[p]; } else { obj = undefined; } } return obj; } //pass callback to this to go to next page function autoRefreshLoan(ms, maxRetries, cb) { ms = ms || 2000; maxRetries = maxRetries || 0; if (angular.isDefined(refreshLoan.interval)) { return; } else { loanService.refreshCurrentLoanFromDB(); refreshLoan.counter = 1; } refreshLoan.interval = $interval(function refreshLoanInterval() { if (!maxRetries || maxRetries > refreshLoan.counter) { refreshLoan.counter += 1; loanService.refreshCurrentLoanFromDB() .then(function (res) { self.currentLoan = res; if (typeof cb === 'function') { cb(); } }); } else { stopRefreshLoan('time_exceeded'); if (!angular.isDefined(refreshLoan.interval) && refreshLoan.mesg) { if (refreshLoan.mesg === 'time_exceeded') { self.refreshLoanError = 'Pardon the interruption! Things seem to be taking longer than usual. Please click below to refresh this page and try again, or contact one of our amazing customer service agents at ' + globalConfig.company.phone.long + ' to continue your loan application.'; } } } }, ms); } function stopRefreshLoan(mesg) { if (angular.isDefined(refreshLoan.interval)) { $interval.cancel(refreshLoan.interval); refreshLoan = { interval: undefined, counter: 0, mesg: mesg || '' }; } } function goToNextStep(nextState) { self.working = false; if (nextState) { $state.go(nextState); } else { $state.go(loanService.getNextStep()); } } $scope.$on('$destroy', function () { // Make sure interval is destroyed stopRefreshLoan(); }); function handleError(res) { self.working = false; self.workingIncludeProvider = false; self.workingDontAddMore = true; self.workingSetStatusGoNext = false; self.workingExcludeProvider = false; self.workingContinue = false; console.info(res); } function includeProvider(providerName) { self.workingIncludeProvider = true; loanService.toggleIncomeProvider(providerName, true) .then(function (loan) { self.currentLoan = loan; self.workingIncludeProvider = false; if(self.currentLoan.incomeSummary.isVerified) { self.goToNextStep(); } else if ('manualVerification' === providerName) { setVerified(); } else { self.showAddMoreIncome = true; } }, function (res) { handleError(res); }); } function excludeProvider(providerName, nextIncomeStatus) { self.workingExcludeProvider = true; loanService.toggleIncomeProvider(providerName, false) .then(function (loan) { self.currentLoan = loan; return loanService.updateIncomeStatus(nextIncomeStatus); }, function (res) { handleError(res); }) .then(function (loan) { self.currentLoan = loan; self.workingExcludeProvider = false; goToNextStep(); }, function (res) { handleError(res); }); } function setStatusGoNext(status, nextState) { setStatusWorkingFlag(status, true); // if requested status is already the current status, just go to that page if (status === self.currentLoan.incomeSummary.status) { goToNextStep(nextState); } else { loanService.updateIncomeStatus(status) .then(function (loan) { setStatusWorkingFlag(status, false); self.currentLoan = loan; goToNextStep(nextState); }, function (res) { handleError(res); }); } } function setStatusWorkingFlag(status, value) { if (status === 'choseYodlee') { self.workingChoseYodlee = value; } else if (status === 'choseManual') { self.workingChoseManual = value; } else { self.workingSetStatusGoNext = value; } } function updateLoanGoNext() { self.workingContinue = true; loanService.refreshCurrentLoanFromDB() .then(function (loan) { self.workingContinue = false; if(loan.incomeSummary.isVerified){ goToNextStep(); } else { $state.go('loanApplication.verify.bank'); } }, function (res) { handleError(res); }); } function setVerified() { self.workingDontAddMore = true; loanService.verifyIncome() .then(function (loan) { return loanService.getLoanOptions(); }) .then(function (loanOffers) { self.currentLoan = loanService.getCurrentLoan(); //no selected offer after income verification, client needs to choose another loan from loan offers page if (!self.currentLoan.selectedOffer || !self.currentLoan.selectedOffer.amount || !self.currentLoan.selectedOffer.duration) { $state.transitionTo('loanApplication.loanOptions', {selectedOfferUnavailable: 'selectedOfferUnavailable'}); } else { self.goToNextStep(); } }, function (res) { handleError(res); loanService.getDenialReasons() .then(function (reasons) { errorHandler.checkSorry(reasons, function () { $state.go("sorry"); }); }, function (denialError) { $state.go("sorry"); }); }); } function handleSourceChange(providerName, sIdx) { self.working = true; var source = self.currentLoan.incomeSummary.providers[providerName].source[sIdx]; loanService.toggleIncomeSource(providerName, sIdx, source.include) .then(function (loan) { self.working = false; self.currentLoan = loan; }, function (res) { handleError(res); }); } function reloadState() { self.currentLoan.incomeSummary.status = null; $state.transitionTo($state.current, $stateParams, { reload: true, inherit: false, notify: true }); } function onUploadSuccess(item) { var deferred = $q.defer(); loanService.uploadCallback(item.callbackURL) .then(function(response) { self.working = false; loanService.setCurrentLoan(response); self.currentLoan = response; init(); config.scrollToTop($('h3').offset().top); deferred.resolve(response); }, function(error) { deferred.reject(error); self.working = false; }); return deferred.promise; } function generateUploadUrl(item) { var deferred = $q.defer(); var form = { documentName: item.file.name, documentPurpose: "incomeVerification", documentType: self.uploadDocType }; loanService.generateUploadUrl(form) .then(function(response) { deferred.resolve(response.data); }, function(error) { deferred.reject(error.data); }); return deferred.promise; } $scope.logout = function () { AuthStateFactory.logout() .then(function(success) { $state.go('sign-out'); }, function(error) { console.log(error); }); }; } })();