sms-mail_MainSMS.js

const conf = require("../config");
const {Vonage} = require('@vonage/server-sdk');



/**
 * @memberOf Myintranet.Mail_SMS
 */
class MainSMS {
    /**
     * @param user
     * @param subject
     * @param mission
     * @param consultant
     * @param employeur
     * @param db
     */
    constructor( user, subject = "Bienvenue à My-intranet", mission, consultant, employeur, db, client , prevOrCra ) {
        this.url = conf.siteUrl;
        this.vonage = new Vonage(conf.sms)
        this.user = user;
        this.mission = mission;
        this.employeur = employeur;
        this.consultant = consultant;
        this.db = db;
        this.subject = subject;
        this.client = client
        this.prevOrCra = prevOrCra
    }

    /**
     *
     * @returns {*}
     */
    getTo() {
        return this.user.login;
    }

    /**
     * @abstract
     * @returns {string}
     */
    getHeaderHtml() {
        return `<div  style="text-align: center; width: 100%;">
            <img width="400px" style="width: 400px" src="cid:LOGOMYINTRANET" alt="LogoMyIntranet">
            <h2>${this.subject}</h2>
        </div>`;
    }

    /**
     * @abstract
     * @returns {string}
     */
    getMailHtml(){
        return "SMS with No text"
    }


    /**
     *  transform a phone number to vonage format
     * @param phone the phone number in fr format +33XXXXX 0033XXXXXXX 06XXXXXXXX
     * @return {*|string} the phone number in vonage format
     * @example
     * console.log(getNormalizedPhoneNumber("+33619629393")) //33619629393
     * console.log(getNormalizedPhoneNumber("0033619629393")) //33619629393
     * console.log(getNormalizedPhoneNumber("0619629393")) //33619629393
     */
    getNormalizedPhoneNumber(phone){
        if(phone.indexOf("00")===0){
            return phone.replace(/^00/, "");
        }else if(phone.indexOf("+")===0){
            return phone.substring(1);
        }else if(phone.indexOf("0")===0){
            return phone.replace(/^0/, '33');
        }else{
            return phone;
        }
    }

    /**
     *
     * @returns {Promise<unknown>}
     */
    async send() {
        return new Promise((resolve, reject) => {
            const from = "MY-INTRANET"
            const to = this.getNormalizedPhoneNumber(this.user.phone)
            const text = this.getMailHtml()

            this.vonage.sms.send({to, from, text})
                .then(resp => {
                    if (resp.messages[0]['status'] === "0") {
                        let sms =  this.db.SMSHisto.create({
                            msisdn: from,
                            text,
                            to,
                            type: "from My intranet",
                            UtilisateurId: this.user.id,
                            orphan: false
                        }).then(()=>{
                            console.log('Message sent successfully');
                            resolve(resp)
                        }).catch(err=>{
                            console.log(`SMS renvoyé mais erreur lors de lq creqtion de smsHisto']}`);
                            reject(resp)
                        })

                    } else {
                        console.log(resp.messages[0])
                        console.log(`Message failed with error: ${ ['error-text']}`);
                        reject(resp)
                    }
                })
                .catch(err => {
                    console.log('There was an error sending the messages.');
                    console.error(err);
                    reject(err);
                });
        });
    }

}


module.exports = MainSMS;