Notification Body Creator

by Roydon DSouza

JavaScript

var msg = "";
var pnTemplate = {
type: "Booking",
title: "Booking Notification",
templateBody: "type:test,ttt:rtre"

};
var replacement = [];
var showNotification = true;

var c = createPnBody('messaage in body',pnTemplate, replacement, showNotification );
console.log(c);


function createPnBody(msg, pnTemplate,replacement, isShowNotification){
console.log(msg);

 //convert & save
        var json = {};
        json['type'] = pnTemplate.type;
        alert = {};
        alert['title'] = pnTemplate.title;
        alert['body'] = msg;
        json['alert'] = alert;

        // $body = $pnTemplate->getTemplateBody();
        // $body = $this->replaceVariables($replacement, $body); 
     
        var body = pnTemplate.templateBody;
        //convert text to array
        var arr = body.split(',');
        var param = {};
				var tmp = null;
        for (let v of arr) {
            tmp = v.split(':');
            param[tmp[0]] = tmp[1];
        }
        json['parameters'] = param;

        if (isShowNotification === true) {
            json['isShowNotification'] = true;
        } else {
            json['isShowNotification'] = false;
        }

        return JSON.stringify(json);
}


function replaceVariables (replacement, text) {
		var variables = [];
	    //text no null
	    variables = text.match(/<<\w+:\d{1,3}>>|<<\w+>>/g);
	    console.log('placeholders ', variables);
	    var variablesFound = variables ? variables.length : 0;
	    console.log('variables', variables);
	    console.log('here');
	    if (variablesFound > 0) {
	    	console.log('length',variablesFound);
	        //$toReplace = array();
	        for (let key in variables) {
	            console.log('---');
	            var len = 0;
	            var variable = variables[key];
	            //$toReplace[] = $variables[0][0];
	            var matches = variable.match(/\d{1,2}/g);
	            console.log('m', matches);
	            if (matches && matches.length > 0) {
	               ...