Last active
May 21, 2018 16:01
-
-
Save roboli/5b9bf19e196d2226672dcc2fc46da187 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadUtils(opts) { | |
opts = opts || {}; | |
var $ = opts.$; | |
var Promise = opts.Promise; | |
var dbaClient = opts.dbaClient; | |
var tryAgainUri = opts.tryAgainUri; | |
function redirectToSign(leadId) { | |
$('#instructions').modal('show'); | |
var skipped = false; | |
var tmtId = setTimeout(function () { | |
if (!skipped) { | |
location.href = dbaClient.uri + 'sign/' + leadId; | |
} | |
}, 10000); | |
$('#skip-instructions').click(function () { | |
clearTimeout(tmtId); | |
location.href = dbaClient.uri + 'sign/' + leadId; | |
}); | |
} | |
function showAgreement() { | |
return new Promise(function(resolve) { | |
$('#infsheet').modal('show'); // display information sheet | |
fbq('track', 'Lead', { | |
value: 0, | |
currency: 'USD' // not sure if it is neccessary | |
}); | |
gtag_display_info_sheet_conversion(); | |
ga('send', 'event', 'MainPage', 'DisplayInfoSheet'); | |
// Disagree on information sheet | |
$('#infosheet-btn-disagree').click(function () { | |
$('#infsheet').modal('hide'); | |
fbq('track', 'Lead', { | |
value: -1, | |
currency: 'USD' // not sure if it is neccessary | |
}); | |
gtag_dont_accept_info_sheet_conversion(); | |
ga('send', 'event', 'MainPage', 'DontAcceptInfoSheet'); | |
resolve(0); | |
}); | |
// Accept information sheet | |
$('#infosheet-btn-agree').click(function () { | |
$('#infsheet').modal('hide'); | |
fbq('track', 'Lead', { | |
value: 1, | |
currency: 'USD' // not sure if it is neccessary | |
}); | |
gtag_accept_info_sheet_conversion(); | |
ga('send', 'event', 'MainPage', 'AcceptInfoSheet'); | |
resolve(1); | |
}); | |
}); | |
} | |
var _lead; | |
function buildLead() { | |
if(_lead) { | |
return _lead; | |
} | |
var lead = {}; | |
lead.leadgroup = dbaClient.leadGroup; | |
lead.site = 0; | |
lead.introducer = 0; | |
lead.reference = ''; | |
var adsinfo = getUrlVars(window.location.href); | |
lead.source = adsinfo.utm_source; | |
lead.medium = adsinfo.utm_medium; | |
lead.term = adsinfo.utm_term; | |
lead.cost = 120; | |
lead.value = 120; | |
// get address and postcode | |
lead.firstname = $("#first-name").val(); | |
lead.lastname = $("#last-name").val(); | |
lead.email = $("#email").val(); | |
lead.phone1 = $("#phone").val(); | |
lead.address = $("#housenum").val() + " " + $("#streetname").val(); | |
lead.towncity = $("#towncity").val(); | |
lead.postcode = $("#postcode").val(); | |
// get date of birth | |
var dob = new Date($("#dob-calendar").datepicker("getDate")); | |
lead.dobday = dob.getUTCDate(); | |
lead.dobmonth = dob.getUTCMonth() + 1; | |
lead.dobyear = dob.getFullYear(); | |
if (lead.dobday < 10) { | |
lead.dobday = "0" + lead.dobday; | |
} | |
if (lead.dobmonth < 10) { | |
lead.dobmonth = "0" + lead.dobmonth; | |
} | |
lead.data6 = 'Yes'; // permissionAccessInfo | |
lead.data7 = 'Yes'; // gaveConsentData | |
lead.data8 = 'Yes'; // agreedExperianCheck | |
lead.data41 = 'Yes'; // workedHourlyRate | |
var campaignURL = window.location.href; | |
lead.data46 = campaignURL.replace(/&/g, '&' + 'amp' + ';'); | |
_lead = lead; | |
return lead; | |
} | |
function isDuplicate() { | |
var today = new Date(); | |
var dd = today.getDate(); | |
var mm = today.getMonth()+1; //January is 0! | |
var yyyy = today.getFullYear(); | |
if(dd<10){ | |
dd='0'+dd; | |
} | |
if(mm<10){ | |
mm='0'+mm; | |
} | |
var lead = buildLead(); | |
var leadId, agreed; | |
return dbaClient.searchLead({ | |
search: lead.email, | |
leadtypeid: dbaClient.leadGroup, | |
startdate: dd+'/'+mm+'/'+(yyyy-1), | |
enddate: dd+'/'+mm+'/'+yyyy | |
}).then(function(results) { | |
// Filter by email, postcode, phone1, fullname and towncity | |
var match = results.find(function(l) { | |
var fullname = lead.firstname.trim().toLowerCase() + ' ' + lead.lastname.trim().toLowerCase(); | |
return lead.email === l.email && | |
lead.postcode === l.postcode && | |
lead.phone1 === l.phone1 && | |
fullname === l.fullname.trim().toLowerCase() && | |
lead.towncity.trim().toLowerCase() === l.towncity.trim().toLowerCase(); | |
}); | |
if(match) { | |
// Fetch complete lead to obtain data fields | |
return dbaClient.getLead(match.id); | |
} | |
return match; | |
}).catch(function(err) { | |
console.log('Error', err); | |
}); | |
} | |
function transitionFromTo(step1Id, step2Id, setPercent, stepNumber) { | |
$('#' + step1Id).hide(); | |
$('#' + step2Id).show(); | |
$('#progress-bar').progress({ | |
percent: setPercent | |
}); | |
$("#progress-step").text("Step " + stepNumber + " of 3"); | |
} | |
$('#step1').form({ | |
fields: { | |
question1: { | |
identifier: 'question1', | |
rules: [ | |
{ | |
type: 'checked', | |
prompt: 'You must have worked at Morrisons for past 6 years on an hourly rate to submit a claim' | |
} | |
] | |
} | |
}, | |
onSuccess: function (e, fields) { | |
transitionFromTo('step1', 'step2', 33, 2); | |
} | |
}); | |
$("#step1-next").click(function () { | |
$('#step1').form('validate form'); | |
}); | |
// add custom validation for postocde | |
$.fn.form.settings.rules.validPostcode = function(param) { | |
var postcode = $("#postcode").val().replace(/\s/g, ""); | |
var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i; | |
return regex.test(postcode); | |
}; | |
$('#step2').form({ | |
fields: { | |
question1: { | |
identifier: 'postcode', | |
rules: [ | |
{ | |
type: 'validPostcode[]', | |
prompt: 'You need to provide valid postcode to continue' | |
} | |
] | |
}, | |
townCity: { | |
identifier: 'towncity', | |
rules: [ | |
{ | |
type: 'empty', | |
prompt: 'You need to provide city/town name to continue' | |
} | |
] | |
}, | |
streetname: { | |
identifier: "streetname", | |
rules: [ | |
{ | |
type: 'empty', | |
prompt: 'You need to provide street name to continue' | |
} | |
] | |
} | |
}, | |
onSuccess: function (e, fields) { | |
transitionFromTo('step2', 'step3', 66, 3); | |
} | |
}); | |
$("#step2-back").click(function () { | |
transitionFromTo('step2', 'step1', 0, 1); | |
}); | |
$("#step2-next").click(function () { | |
$('#step2').form('validate form'); | |
}); | |
// add custom validation for birth | |
$.fn.form.settings.rules.validBirthDay = function(param) { | |
var currentDate = new Date(); | |
currentDate.setFullYear(currentDate.getFullYear() - 18); | |
var date = Date.parse($("#dob-calendar").datepicker("getDate")); | |
if(isNaN(date)) { | |
return false; | |
} | |
if (new Date($("#dob-calendar").datepicker("getDate")) > currentDate) { | |
return false; | |
} | |
return true; | |
}; | |
$('#step3').form({ | |
fields: { | |
first_name: { | |
identifier: 'first-name', | |
rules: [ | |
{ | |
type: 'empty', | |
prompt: 'You need to provide your first name' | |
} | |
] | |
}, | |
last_name: { | |
identifier: 'last-name', | |
rules: [ | |
{ | |
type: 'empty', | |
prompt: 'You need to provide your last name' | |
} | |
] | |
}, | |
email: { | |
identifier: 'email', | |
rules: [ | |
{ | |
type: 'email', | |
prompt: 'You need to provide your email address' | |
} | |
] | |
}, | |
phone: { | |
identifier: 'phone', | |
rules: [ | |
{ | |
type: 'empty', | |
prompt: 'You need to provide a phone number to continue' | |
} | |
] | |
}, | |
birth_day: { | |
identifier: 'dob-calendar', | |
rules: [ | |
{ | |
type: 'validBirthDay[]', | |
prompt: 'We are currently not taking on claims for individuals under the age of 18. Please contact us again once you have turned 18 and we will consider whether you have a claim' | |
} | |
] | |
} | |
}, | |
onSuccess: function (e, fields) { | |
transitionFromTo('step3', 'step4', 100, 3); | |
$("#progress-bar").hide(); | |
} | |
}); | |
$("#step3-back").click(function () { | |
transitionFromTo('step3', 'step2', 33, 2); | |
}); | |
$("#step3-next").click(function () { | |
$('#step3').form('validate form'); | |
}); | |
$('#step4').form({ | |
fields: { | |
agree1: { | |
identifier: 'agree1', | |
rules: [ | |
{ | |
type: 'checked', | |
prompt: 'You must give permission for Pay Justice to have access to the provided information above.' | |
} | |
] | |
}, | |
agree2: { | |
identifier: 'agree2', | |
rules: [ | |
{ | |
type: 'checked', | |
prompt: 'You must give permission for Pay Justice to have access to the provided information above.' | |
} | |
] | |
}, | |
agree3: { | |
identifier: 'agree3', | |
rules: [ | |
{ | |
type: 'checked', | |
prompt: 'You must give permission for Pay Justice to have access to the provided information above.' | |
} | |
] | |
} | |
}, | |
onSuccess: function (e, fields) { | |
function showAgree(leadId) { | |
var agreed; | |
return showAgreement().then(function(result) { | |
agreed = result; | |
return dbaClient.confirm(leadId, agreed); | |
}).then(function() { | |
if(agreed) { | |
redirectToSign(leadId); | |
} | |
return; | |
}); | |
} | |
function checkExperian(lead) { | |
var valid; | |
var experianDobFormat = "" + lead.dobyear + "-" + lead.dobmonth + "-" + lead.dobday; | |
return dbaClient.checkExperian({ | |
firstName: lead.firstname, | |
lastName: lead.lastname, | |
dob: experianDobFormat, | |
address: lead.address, | |
postcode: lead.postcode | |
}).then(function(result) { | |
valid = result.valid; | |
return dbaClient.updateLead({ id: lead.id, data44: valid, data49: result.reason }); | |
}).then(function() { | |
if(valid === '1') { | |
return showAgree(lead.id); | |
} else { | |
return Promise.reject(); | |
} | |
}).catch(function() { | |
location.href = tryAgainUri; | |
}); | |
} | |
function createLead() { | |
var lead = buildLead(); | |
var leadId, valid, agreed; | |
return dbaClient.createLead(lead) | |
.then(function(result) { | |
lead.id = result.id; | |
return checkExperian(lead); | |
}) | |
.catch(function(err) { | |
console.log('Error', err); | |
}); | |
} | |
$("#loading").addClass("active"); | |
// first check duplicates before submission | |
isDuplicate() | |
.then(function(lead) { | |
if(lead && lead.data44 === '1' && lead.data40 && lead.data40 !== 'false') { | |
// Exists, passed Experian check and agreed | |
return redirectToSign(lead.id); | |
} else if(lead && lead.data44 === '1') { | |
// Exists and passed Experian check | |
return showAgree(lead.id); | |
} else if(lead) { | |
// Exists | |
return checkExperian(lead); | |
} else { | |
// New | |
return createLead(); | |
} | |
}).then(function() { | |
$("#loading").removeClass("active"); | |
}); | |
} | |
}); | |
$("#step4-back").click(function () { | |
transitionFromTo('step4', 'step3', 100, 3); | |
$("#progress-bar").show(); | |
}); | |
$("#step4-next").click(function () { | |
$('#step4').form('validate form'); | |
}); | |
$("#infosheet").scroll(function () { | |
if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) { | |
$("#infosheet-btn-agree").removeClass("disabled"); | |
} | |
}); | |
$('#dob-calendar').datepicker({ | |
changeMonth: true, | |
changeYear: true, | |
yearRange: '1910:2020', | |
// startMode: 'year', | |
type: 'date', | |
useCurrent: true, | |
dateFormat: 'dd/mm/yy' | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment