UNPKG

km-web-plugin

Version:

ICE Web Plugin Initializer

174 lines (147 loc) 5.07 kB
let loan = null; let appName = null; let elli = null; let http = null; let auth = null; let token = null; let user = null; export function init(appNameParam, elliParam, subscriptions) { appName = appNameParam; elli = elliParam; if (subscriptions) { subscriptions.forEach((subscription) => { if ( !subscription.event || !subscription.type || !subscription.callback ) { log('Invalid subscription', subscription); return; } elli.script.subscribe( subscription.event, subscription.type, subscription.callback, ); }); } } export async function getLoanAll() { let ln = await getLoan(); return await ln.all(); } export async function getUser() { let auth = await getAuth(); return (user = user ? user : await auth.getUser()); } export async function getHttp() { return (http = http ? http : await elli.script.getObject('http')); } export async function getAuth() { return (auth = auth ? auth : await elli.script.getObject('auth')); } export async function getLoan() { return (loan = loan ? loan : await elli.script.getObject('loan')); } export async function getAccessToken() { if (token != null && token.eol > Date().valueOf()) return token?.access_token; let auth = await getAuth(); let response = await auth.getAccessToken(); token = { ...response, eol: new Date().valueOf() + 1000 * 60 * 4 }; return token?.access_token; } export async function setField(fieldId, value) { return await loan?.setFields({ [fieldId]: value }); } export async function setFields(obj) { return await loan?.setFields(obj); } export async function getField(fieldId) { return await loan?.getField(fieldId); } export async function getFields(fieldsArr) { if (fieldsArr == null || fieldsArr.length == 0) return null; let result = {}; let promises = []; fieldsArr.forEach((element) => { promises.push( new Promise(async (resolve) => { await getLoan(); result[element] = await loan.getField(element); resolve(); }), ); }); await Promise.allSettled(promises); return result; } export async function log(...args) { return console.log.apply(window.console, [ `%c ${appName}: `, 'color:limegreen;font-weight:bold;background-color: black;', ...args, ]); //return window.console.log.bind(window.console, `%c ${appName}: `, "color:limegreen;font-weight:bold;background-color: black;", msg); if (typeof msg == 'object') return console.log.apply(window.console, [ `%c ${appName}: `, 'color:limegreen;font-weight:bold;background-color: black;', ...args, ]); //return console.log(appName+':', msg); console.log.apply(window.console, [ `%c ${appName}: ${msg}`, 'color:limegreen;font-weight:bold;background-color: black;', ]); } export function useLogger(appName, ...args) { return console.log.bind( window.console, `%c ${appName}: `, 'color:limegreen;font-weight:bold;background-color: black;', ...args, ); } export async function getVerifCounts() { await getLoan(); if (loan == null) return null; const loanAll = await loan.all(); const applications = loanAll.loan.applications; if (applications == null || applications.length == 0) return null; const response = { vodcount: 0, volcount: 0, vomcount: 0, vorcount: 0, vorbcount: 0, vorccount: 0, voecount: 0, voebcount: 0, voeccount: 0, voalcount: 0, vooicount: 0, vooacount: 0, voggcount: 0, voolcount: 0, }; applications.forEach((vos) => { response.vodcount += vos?.vods?.length ?? 0; response.volcount += vos?.vols?.length ?? 0; response.vomcount += vos?.reoProperties?.length ?? 0; response.vorcount += vos?.borrower?.residences?.length ?? 0; response.vorcount += vos?.coborrower?.residences?.length ?? 0; response.vorbcount += vos?.borrower?.residences?.length ?? 0; response.vorccount += vos?.coborrower?.residences?.length ?? 0; response.voecount += vos?.borrower?.employment?.length ?? 0; response.voecount += vos?.coborrower?.employment?.length ?? 0; response.voebcount += vos?.borrower?.employment?.length ?? 0; response.voeccount += vos?.coborrower?.employment?.length ?? 0; response.voalcount += vos?.additionalLoans?.length ?? 0; response.vooicount += vos?.otherIncomeSources?.length ?? 0; response.vooacount += vos?.otherAssets?.length ?? 0; response.voggcount += vos?.giftsGrants?.length ?? 0; response.voolcount += vos?.otherLiabilities?.length ?? 0; }); return response; }