All files serial-entrepreneur-frontend-handlers.js

93.02% Statements 40/43
100% Branches 2/2
100% Functions 14/14
90.32% Lines 28/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 701x 1x   1x   1x 1x 1x 1x             10x   10x 10x 10x 10x   10x 8x 8x   2x 2x               1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x                            
const fetchLean = require('fetch-lean');
const tickLog = require('tick-log');
 
const keys = {};
 
const init = (p_params) => new Promise(async (resolve, reject) => {
	try {
		keys.apiBackend = p_params.apiBackend;
		return resolve(true);
	} catch (error) /* istanbul ignore next */ {
		return reject("Unknwon error");
	}
});
 
// template for all calls to the backend
const serialEntrepreneurBackendCall = (method, route, props, successCallback, failCallback) => new Promise(async (resolve, reject) => {
	let l_retval;
	let l_url = `${keys.apiBackend}/${route}`;
	tickLog.start(`serialEntrepreneurBackendCall ${method} ${l_url}`);
	try {
		l_retval = await fetchLean(method, l_url, {}, props);
		//  l_retval: {"result":"FAIL","error":"Invalid confirmation code."}
		if (l_retval.result === 'OK') {
			successCallback(props, l_retval);
			return resolve(l_retval);
		};
		failCallback(props, l_retval);
		return reject(l_retval);
	} catch (error) {
		tickLog.error(`serialEntrepreneurBackendCall failed. method: ${method}, route: ${route}, props: ${JSON.stringify(props)}, error: ${JSON.stringify(error)}`);
		failCallback(props, error);
		return reject(error);
	}
});
 
const testHandler = async (name, email, password, successCallback, failCallback) => { await serialEntrepreneurBackendCall('GET', 'testhandler', { name, email, password }, successCallback, failCallback); }
 
const registerUserStep1 = async (name, email, password, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'registeruserstep1', { name, email, password }, successCallback, failCallback); }
 
const registerUserStep2 = async (email, confirmationCode, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'registeruserstep2', { email, confirmationCode }, successCallback, failCallback); }
 
const removeUser = async (email, token, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'removeuser', { email, token }, successCallback, failCallback); }
 
const loginUserViaMail = async (email, password, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'loginuserviamail', { email, password }, successCallback, failCallback); }
 
const loginUserViaToken = async (token, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'loginuserviatoken', { token }, successCallback, failCallback); }
 
const changePassword = async (email, oldPassword, newPassword, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'changepassword', { email, oldPassword, newPassword }, successCallback, failCallback); }
 
const resetPasswordStep1 = async (email, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'resetpasswordstep1', { email }, successCallback, failCallback); }
 
const resetPasswordStep2 = async (email, confirmationCode, newPassword, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'resetpasswordstep2', { email, confirmationCode, newPassword }, successCallback, failCallback); }
 
const updateUserData = async (token, name, successCallback, failCallback) => { await serialEntrepreneurBackendCall('POST', 'updateuserdata', { token, name }, successCallback, failCallback); }
 
module.exports = {
	init: init,
	testHandler: testHandler,
	registerUserStep1: registerUserStep1,
	registerUserStep2: registerUserStep2,
	removeUser: removeUser,
	loginUserViaMail: loginUserViaMail,
	loginUserViaToken: loginUserViaToken,
	changePassword: changePassword,
	resetPasswordStep1: resetPasswordStep1,
	resetPasswordStep2: resetPasswordStep2,
	updateUserData: updateUserData,
	exportedForTesting: {
	}
}