"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateSetting = void 0;
const Session_1 = require("../lib/model/Session");
const AppRoute_1 = require("../lib/AppRoute");
const _406_1 = require("../lib/view/406");
const RawQuery_1 = require("../lib/model/RawQuery");
const RawView_1 = require("../lib/view/RawView");
const RawNonQuery_1 = require("../lib/model/RawNonQuery");
const fs = require('fs');
const path = require('path');
class TemplateSetting extends AppRoute_1.AppRoute {
    constructor() {
        super();
    }
    emailTemplate(req, res, next) {
        let sdata = req.body;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let temp = typeof sdata.template != 'undefined' ? sdata.template : "";
                temp = String(temp).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/©/g, '&copy;');
                temp = temp.replace(/'/g, "\\'");
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "INSERT INTO `email_template`(`idaccount`,`subject`,`type`,`template`) VALUES ( '" + sessData.idaccount + "', '" + sdata.subject + "','" + sdata.type + "','" + temp + "')";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Email Template Saved Successfully ", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ message: "Invalid Session trying to access", status: 401 });
                objv.execute();
            }
        });
    }
    updateStatus(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessdata) => {
            if (error == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "UPDATE email_template SET status = '" + req.body.status + "' WHERE idemail_template = '" + req.body.id + "'";
                obj.prepare();
                obj.execute((error, lastid) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Status Update Successfully", data: lastid[0], status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Status Update Successfully.", data: lastid, status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare("No Session data there");
                objv.execute();
            }
        });
    }
    getEmailTemplate(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM email_template WHERE `idemail_template` = '" + sdata.idemail_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: 'Email Template Fatched', data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 503 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    getEmailTemplates(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM email_template WHERE idaccount='" + sessData.idaccount + "' ORDER BY `idemail_template` ASC";
                obj.prepare();
                obj.execute((err, result) => {
                    if (err == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: 'Email Template Fatched', data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ message: "Invalid Authorization!", status: 401 });
                objv.execute();
            }
        });
    }
    updateEmailTemplate(req, res, next) {
        let sdata = req.body;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error) => {
            if (error == 1) {
                // let temp: any = typeof sdata.template != 'undefined' ? sdata.template : ""
                // temp = String(temp).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/©/g, '&copy;')
                // temp = temp.replace(/'/g, "\\'")
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "UPDATE `email_template` SET `subject`= '" + sdata.subject + "', `type`='" + sdata.type + "', `template` ='" + sdata.template + "' WHERE idemail_template= '" + sdata.idemail_template + "'";
                obj.prepare();
                obj.execute((_error, result) => {
                    if (_error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Email Template Updated Successfully!", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 502, error: result });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    deleteEmailTempalte(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((err) => {
            if (err == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "DELETE FROM `email_template` WHERE `idemail_template`='" + sdata.idemail_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Email Tempalate Deleted!", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Database!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    smsTemplate(req, res, next) {
        let sdata = req.body;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "INSERT INTO `sms_template`(`idaccount`, `name`, `type`, `body`) VALUES ('" + sessData.idaccount + "', '" + sdata.name + "','" + sdata.type + "','" + sdata.body + "')";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "SMS Template Saved Successfull", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Somthing Went Wrong With Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ message: "Invalid Session trying to access ", status: 401 });
                objv.execute();
            }
        });
    }
    updateStatusSMS(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessdata) => {
            if (error == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "UPDATE sms_template SET status ='" + req.body.status + "' WHERE idsms_template= '" + req.body.id + "'";
                obj.prepare();
                obj.execute((error, lastid) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Status Updated Successfully", data: lastid[0], status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Status Updated Successfully", data: lastid, status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare("No Session data there");
                objv.execute();
            }
        });
    }
    getSmsTemplate(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM sms_template WHERE `idsms_template` = '" + sdata.idsms_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "SMS Template Fatch", data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Somthing Went Wrong With Connection", status: 503 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    getSmsTemplates(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM sms_template WHERE idaccount = '" + sessData.idaccount + "'";
                obj.prepare();
                obj.execute((err, result) => {
                    if (err == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "SMS Template Fatched", data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong with Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ message: "Invalid Authorization!", status: 401 });
                objv.execute();
            }
        });
    }
    updateSmsTemplate(req, res, next) {
        let sdata = req.body;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessData) => {
            if (error == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "UPDATE `sms_template` SET `name`='" + sdata.name + "' , `type`='" + sdata.type + "', `body`='" + sdata.body + "' WHERE idsms_template = '" + sdata.idsms_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "SMS Template Updated Successfully!", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection", status: 502, error: result });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    twofaTemaplate(req, res, next) {
        let sdata = req.body;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessdata) => {
            if (sessErr == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                let temp_key = '';
                let charactersLength = characters.length;
                for (let i = 0; i < 25; i++) {
                    temp_key += characters.charAt(Math.floor(Math.random() * charactersLength));
                }
                obj.nonqrysql = "INSERT INTO `2fa_template`(`iduser`, `otp_digit`, `type`, `body`, `tmp_type`,`template_key`) VALUES ('" + sessdata.iduser + "','" + sdata.otp_digit + "','" + sdata.type + "','" + sdata.body + "', '" + sdata.tmpType + "','" + temp_key + "')";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "2FA Template Saved Successfully", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ message: "Invalid Session trying to access", status: 401 });
                objv.execute();
            }
        });
    }
    get2FATemplate(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM 2fa_template WHERE `id2fa_template`= '" + sdata.id2fa_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "2FA Template Fetched", data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    get2FATemplates(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM 2fa_template WHERE iduser = '" + sessData.iduser + "' AND tmp_type='" + req.query.type + "'";
                obj.prepare();
                obj.execute((err, result) => {
                    if (err == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: '2FA Template Fetched', data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ message: "Invalid Authorization!", status: 401 });
                objv.execute();
            }
        });
    }
    update2FATemplate(req, res, next) {
        let sdata = req.body;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error) => {
            if (error == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "UPDATE `2fa_template` SET `otp_digit`= '" + sdata.otp_digit + "',`type`= '" + sdata.type + "',`body`= '" + sdata.body + "' WHERE id2fa_template= '" + sdata.id2fa_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "2FA Template Update Successfully", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 502, error: result });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    delete2FATemplate(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((err) => {
            if (err == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "DELETE FROM `2fa_template` WHERE `id2fa_template`='" + sdata.id2fa_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: " 2FA Template Deleted!", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: " Something Went Wrong With Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    mmsTemplate(req, res, next) {
        var sdata = JSON.parse(req.body.data);
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            let file_path = req.file.filename;
            let extension = file_path.split('.').pop();
            let fsize;
            let mesg;
            if (extension == 'jpg' || extension == 'jpeg' || extension == 'png' || extension == 'gif' || extension == 'webp') {
                fsize = 1024 * 740;
                mesg = 'File too Big, please select a file less than 740KB!';
            }
            else if (extension == 'mp4') {
                fsize = 1024 * 1024;
                mesg = 'File too Big, please select a file less than 1MB!';
            }
            else if (extension == 'mp3') {
                fsize = 1024 * 740;
                mesg = 'File too Big, please select a file less than 740KB!';
            }
            else {
                fsize = 1024 * 740;
                mesg = 'File too Big, please select a file less than 740KB!';
            }
            if (req.file.size <= fsize) {
                let text;
                let file;
                text = sdata.body;
                if (req.file) {
                    const fileName = req.file.filename;
                    //file = req.protocol + '://' + req.get('host') + '/uploads/' + fileName;	
                    file = 'https://mms.signalmash.com/content/' + fileName;
                }
                if (sessErr == 1) {
                    let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                    obj.nonqrysql = "INSERT INTO `mms_template`(`idaccount`,`iduser`,`name`,`content_type`, `body`,`file_path`) VALUES ('" + sessData.idaccount + "','" + sessData.iduser + "','" + sdata.name + "','" + sdata.content_type + "','" + text + "','" + file + "')";
                    obj.prepare();
                    obj.execute((error, result) => {
                        if (error == 1) {
                            let objv = new RawView_1.RawView(res);
                            objv.prepare({ message: "MMS Template Saved Successfully", data: result, status: 200 });
                            objv.execute();
                        }
                        else {
                            let objv = new RawView_1.RawView(res);
                            objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                            objv.execute();
                        }
                    });
                }
                else {
                    let objv = new RawView_1.RawView(res);
                    objv.prepare({ message: "Invalid Session trying to access ", status: 401 });
                    objv.execute();
                }
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ status: 400, message: mesg });
                objv.execute();
            }
        });
    }
    updateMmsTemplate(req, res, next) {
        var sdata = JSON.parse(req.body.data);
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessData) => {
            let file_path = req.file.filename;
            let extension = file_path.split('.').pop();
            let fsize;
            let mesg;
            if (extension == 'jpg' || extension == 'jpeg' || extension == 'png' || extension == 'gif' || extension == 'webp') {
                fsize = 1024 * 740;
                mesg = 'File too Big, please select a file less than 740KB!';
            }
            else if (extension == 'mp4') {
                fsize = 1024 * 1024;
                mesg = 'File too Big, please select a file less than 1MB!';
            }
            else if (extension == 'mp3') {
                fsize = 1024 * 740;
                mesg = 'File too Big, please select a file less than 740KB!';
            }
            else {
                fsize = 1024 * 740;
                mesg = 'File too Big, please select a file less than 740KB!';
            }
            if (req.file.size <= fsize) {
                let text;
                let file;
                text = sdata.body;
                if (req.file) {
                    const fileName = req.file.filename;
                    //file = req.protocol + '://' + req.get('host') + '/uploads/' + fileName;
                    file = 'https://mms.signalmash.com/content/' + fileName;
                }
                if (error == 1) {
                    let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                    obj.nonqrysql = "UPDATE `mms_template` SET `name`='" + sdata.name + "',`content_type`='" + sdata.content_type + "' , `body`='" + text + "', `file_path`= '" + file + "' WHERE idmms_template = '" + sdata.idmms_template + "'";
                    obj.prepare();
                    obj.execute((error, result) => {
                        if (error == 1) {
                            let objv = new RawView_1.RawView(res);
                            objv.prepare({ message: "MMS Template Updated Successfully!", data: result, status: 201 });
                            objv.execute();
                        }
                        else {
                            let objv = new RawView_1.RawView(res);
                            objv.prepare({ message: "Something Went Wrong With Connection", status: 502, error: result });
                            objv.execute();
                        }
                    });
                }
                else {
                    let objv = new _406_1.Res406(res);
                    objv.prepare(session);
                    objv.execute();
                }
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ status: 400, message: mesg });
                objv.execute();
            }
        });
    }
    getMmsTemplate(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM mms_template WHERE `idmms_template` = '" + sdata.idmms_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "MMS Template Fetched", data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection", status: 503 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    getMmsTemplates(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM mms_template WHERE `iduser` = '" + sessData.iduser + "'";
                obj.prepare();
                obj.execute((err, result) => {
                    if (err == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "MMS Template Fetched", data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong with Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ message: "Invalid Authorization!", status: 401 });
                objv.execute();
            }
        });
    }
    deleteMMSTemplate(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((err) => {
            if (err == 1) {
                let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj.nonqrysql = "DELETE FROM `mms_template` WHERE `idmms_template`='" + sdata.idmms_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "MMS Template Deleted!", data: result, status: 201 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
    setDfaultTemplate(req, res, next) {
        let sdata = req.body.data;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((err, sessdata) => {
            if (err == 1) {
                let obj1 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                obj1.nonqrysql = "UPDATE `2fa_template` SET `default`= '0' WHERE iduser='" + sessdata.iduser + "'";
                obj1.prepare();
                obj1.execute((___err, result) => {
                    if (___err == 1) {
                        let obj1 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                        obj1.nonqrysql = "UPDATE `2fa_template` SET `default`= '1' WHERE iduser='" + sessdata.iduser + "' AND id2fa_template='" + sdata + "'";
                        obj1.prepare();
                        obj1.execute((__err, result1) => {
                            if (__err == 1) {
                                let objv = new RawView_1.RawView(res);
                                objv.prepare({ message: "Default Template Changed Successfully!", status: 201, data: result1 });
                                objv.execute();
                            }
                            else {
                                let objv = new RawView_1.RawView(res);
                                objv.prepare({ message: "Something Went Wrong With Default Template!", status: 502, error: result1 });
                                objv.execute();
                            }
                        });
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection1!", status: 502, error: result });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ message: "Something Went Wrong With Connection2!", status: 502 });
                objv.execute();
            }
        });
    }
    refactorTemplate(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((sessErr, sessData) => {
            if (sessErr == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT * FROM email_template WHERE `idemail_template` = '" + sdata.idemail_template + "'";
                obj.prepare();
                obj.execute((error, result) => {
                    if (error == 1) {
                        console.log("resulttttttttttt", result);
                        let message = result[0].template;
                        // message = message.replace('&lt;', '<').replace('&gt;','>').replace('&quot;','"').replace('&apos;',/'/g).replace('&amp;','&');
                        message = message.replace(/&lt;/gi, '<').replace(/&gt;/gi, '>').replace(/&quot;/gi, '"').replace(/&apos;/gi, "'").replace(/&amp;/gi, '&');
                        console.log("messageeeeee", message);
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: 'Email Template Fatched', data: result, status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 503 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare(session);
                objv.execute();
            }
        });
    }
}
exports.TemplateSetting = TemplateSetting;
