sms-mail_Slack.js

const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));


const url = "https://hooks.slack.com/services/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";
const bodyMessage = {
    text: "sdffdfsdf"
};

let DocumentBlock = {
    "type": "section",
    "text": {
        "type": "mrkdwn",
        "text": "<https://example.com|Bates Motel> :star::star:"
    },
    "accessory": {
        "type": "button",
        "text": {
            "type": "plain_text",
            "text": "View",
            "emoji": true
        },
        "value": "view_alternate_1"
    }
};


/**
 * @extends Myintranet.Mail_SMS.MainMail
 * @memberOf Myintranet.Mail_SMS
 */
class Slack {
    getFileFolderName(typeDoc) {

        switch (typeDoc) {
            case "CV":
                return  "CV";
            case "PI":
                return  "piece_identite";
            case "JD":
                return "justificatif_domicile";

            case "ASS":
                return "ATT_Securite_social";
            case "SI":
                return  "Simulation";
            case "CG":
                return  "Carte_grise";
            case "IB":
                return "Informations_banquaire";
            case "CM":
                return  "Contrat_mutuelle";
            case "PC":
                return "Permis_de_conduire";
            case "MU":
                return  "mutuelle";
            case "AP":
                return  "ATT_prevoyance";
            default: return "notypeDoc"
        }
    }
    getFileDisplayName(typeDoc) {

        switch (typeDoc) {
            case "CV":
                return  "CV";
            case "PI":
                return  "piece d'identite";
            case "JD":
                return "justificatif de domicile";
            case "ASS":
                return "Attestation de sécurité social";
            case "SI":
                return  "Simulation";
            case "CG":
                return  "Carte grise";
            case "IB":
                return "Informations banquaire (rib, iban)";
            case "CM":
                return  "Contrat de mutuelle";
            case "PC":
                return "Permis de conduire";
            case "MU":
                return  "mutuelle";
            case "AP":
                return  "Attestation de prevoyance";
            default: return "no name (ask the developper)"
        }
    }
    constructor(user, files, consultant, mission, request) {
        this.user = user;
        this.files = files;
        this.consultant = consultant;
        this.mission = mission;
        this.baseUrl = request.protocol + '://' + request.get('host');
        //+ request.originalUrl;
        this.prepareJSON();
    }


    prepareJSON() {
        bodyMessage.text = "Nouveau consultant Créer sur My-intranet '" + this.user.firstname + " " + this.user.lastname + "'";
        bodyMessage.attachments = [];
        bodyMessage.attachments.push({
            "fallback": "Plain-text summary of the attachment.",
            "color": "#2563eb",
            // "pretext": "Optional text that appears above the attachment block",
            "author_name": "Voir la fiche de : " + this.user.firstname + " " + this.user.lastname,
            "author_link": this.baseUrl + "/consultant/create/" + this.consultant.id,
            "author_icon": this.baseUrl + "/img/minilogo.png",
            // "title": "Slack API Documentation",
            // "title_link": this.baseUrl,
            // "text": "Optional text that appears within the attachment",
            "fields": [
                {
                    "title": "Nouveau consultant à valider",
                    "value": this.user.firstname + " " + this.user.lastname,
                    "short": true
                },
                {
                    "title": "date de création",
                    "value": new Date(this.user.createdAt).toLocaleDateString(),
                    "short": true
                },
                {
                    "title": "Email",
                    "value": this.user.login,
                    "short": true
                },
                {
                    "title": "Téléphone",
                    "value": this.user.phone,
                    "short": true
                },
                {
                    "title": "ADDRESS",
                    "value": this.consultant.address + "\n " + this.consultant.zipCode + " " + this.consultant.city,
                    "short": true
                }

            ]
        })

        bodyMessage.attachments.push({
            color: "#2563eb",
            text: "Les documents chargées " + this.user.firstname + " " + this.user.lastname,
        })


        let blocks = [];
        let consultantFolder = "/data/" + this.consultant.id + "-" + this.user.firstname + "-" + this.user.lastname
        // consultantFolder += "/mission-" + this.mission.id + '-' + this.mission.title
        for (const key in this.files) {
            if (Object.hasOwnProperty.call(this.files, key)) {
                const file = this.files[key];
                let filePath = consultantFolder +"/" + this.getFileFolderName(file.typeDoc) + '/' + file.name

                blocks.push({
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "<" + this.baseUrl + filePath +" |" + this.getFileDisplayName(file.typeDoc) + "> "
                    }
                })
            }
        }


        bodyMessage.attachments.push({
            color: "#2563eb",
            blocks
        })
        this.data = bodyMessage;
    }


    async send() {

        const response = await fetch(url, {
            method: 'POST',
            body: JSON.stringify(this.data)
        });
        const data = await response.text();

        console.log(data);
    }

}


module.exports = Slack;