Guest User

Untitled

a guest
Jul 31st, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 9.28 KB | None | 0 0
  1. var helper = require('sendgrid').mail,
  2.   ejs = require('ejs'),
  3.   config = require('../config/transport');
  4.  
  5. var sendgrid = require('sendgrid')(config.sendgrid.key);
  6. var request = require('request');
  7. var fs = require('fs');
  8. var path = require('path');
  9. var shortid = require('shortid');
  10.  
  11. module.exports = {
  12.   mails: {
  13.     devis: (container, mail, file) => {
  14.       let data_mail_client = {
  15.         from: "contact@tontongaston.fr",
  16.         to: mail,
  17.         subject: " [" + container.name + "] Devis",
  18.         template: container.template_mail_devis
  19.       };
  20.       return sendFile(Object.assign({}, data_mail_client, { values: {} }), file, 'devis.pdf');
  21.     },
  22.     facture: (container, mail, file) => {
  23.       let data_mail_client = {
  24.         from: "contact@tontongaston.fr",
  25.         to: mail,
  26.         subject: " [" + container.name + "] Facture",
  27.         template: container.template_mail_facture
  28.       };
  29.       return sendFile(Object.assign({}, data_mail_client, { values: {} }), file, 'facture.pdf');
  30.     },
  31.     acompte: (container, mail, file, values) => {
  32.       let data_mail_client = {
  33.         from: "contact@tontongaston.fr",
  34.         to: mail,
  35.         subject: " [" + container.name + "] Facture",
  36.         template: container.template_mail_facture,
  37.         values
  38.       };
  39.       return sendFile(Object.assign({}, data_mail_client, { values: {} }), file, 'facture.pdf');
  40.     },
  41.     artisan_artisan: (container, mail, values) => {
  42.       let data_mail = {
  43.         from: "contact@tontongaston.fr",
  44.         to: mail,
  45.         subject: "[" + container.name + " Pro]",
  46.         template: container.template_mail_pro,
  47.         values
  48.       };
  49.  
  50.       return send(Object.assign({}, data_mail));
  51.     },
  52.     recovery: (template, email, values) => {
  53.       let data_mail = {
  54.         from: "contact@tontongaston.fr",
  55.         to: email,
  56.         subject: "TontonGaston - Initialisation du mot de passe",
  57.         template,
  58.         values
  59.       };
  60.  
  61.       return send(Object.assign({}, data_mail));
  62.     },
  63.     master_artisan_proposition: (template, email) => {
  64.       let data_mail = {
  65.         from: "contact@tontongaston.fr",
  66.         to: email,
  67.         subject: "TontonGaston - Nouvelle offre !",
  68.         template,
  69.         values: {}
  70.       };
  71.  
  72.       return send(Object.assign({}, data_mail));
  73.     },
  74.     master_artisan_proposition_won: (template, email) => {
  75.       let data_mail = {
  76.         from: "contact@tontongaston.fr",
  77.         to: email,
  78.         subject: "TontonGaston - Offre good :)",
  79.         template,
  80.         values: {}
  81.       };
  82.  
  83.       return send(Object.assign({}, data_mail));
  84.     },
  85.     master_artisan_proposition_lost: (template, email) => {
  86.       let data_mail = {
  87.         from: "contact@tontongaston.fr",
  88.         to: email,
  89.         subject: "TontonGaston - Offre perdu :(",
  90.         template,
  91.         values: {}
  92.       };
  93.  
  94.       return send(Object.assign({}, data_mail));
  95.     },
  96.     welcome: (container, email, values) => {
  97.       let data_mail = {
  98.         from: "contact@tontongaston.fr",
  99.         to: email,
  100.         subject: "[" + container.name + "] ",
  101.         template: container.template_mail_welcome,
  102.         values
  103.       };
  104.  
  105.       return send(Object.assign({}, data_mail));
  106.     },
  107.     newsletter: (container, email, values) => {
  108.       let data_mail = {
  109.         from: "contact@tontongaston.fr",
  110.         to: email,
  111.         subject: "[" + container.name + "] Newletters",
  112.         template: container.template_mail_newsletter,
  113.         values
  114.       };
  115.  
  116.       return send(Object.assign({}, data_mail));
  117.     },
  118.     recovery_app: (container, email, values) => {
  119.       let data_mail = {
  120.         from: "contact@tontongaston.fr",
  121.         to: email,
  122.         subject: "[" + container.name + "] Réinitialisation du mot de passe",
  123.         template: container.template_mail_password,
  124.         values
  125.       };
  126.  
  127.       return send(Object.assign({}, data_mail));
  128.     },
  129.     contact_app: (container, email) => {
  130.       let data_mail = {
  131.         from: "contact@tontongaston.fr",
  132.         to: email,
  133.         subject: "[" + container.name + "] demande de contact",
  134.         template: container.template_mail_confirmation
  135.       };
  136.  
  137.       return send(Object.assign({}, data_mail));
  138.     },
  139.     contact_container_app: (container, body) => {
  140.       let data_mail = {
  141.         from: "contact@tontongaston.fr",
  142.         to: container.mail,
  143.         subject: "[" + container.name + " - CONTACT] " + body.email,
  144.         template: container.template_mail_contact,
  145.         values: {
  146.           message: body.message,
  147.           email: body.email,
  148.           firstname: body.firstname,
  149.           lastname: body.lastname,
  150.           phone: body.phone
  151.         }
  152.       };
  153.  
  154.       return send(Object.assign({}, data_mail));
  155.     },
  156.     devis_app: (container, email, service) => {
  157.       let data_mail = {
  158.         from: "contact@tontongaston.fr",
  159.         to: email,
  160.         subject: " [" + container.name + "] Demande de devis - " + service,
  161.         template: container.template_mail_confirmation
  162.       };
  163.  
  164.       return send(Object.assign({}, data_mail));
  165.     },
  166.     devis_container_app: (container, body) => {
  167.       let data_mail = {
  168.         from: "contact@tontongaston.fr",
  169.         to: container.mail,
  170.         subject: "[" + container.name + " - " + body.service + "] " + body.email,
  171.         template: container.template_mail_contact,
  172.         values: {
  173.           message: body.message,
  174.           email: body.email,
  175.           firstname: body.firstname,
  176.           lastname: body.lastname,
  177.           phone: body.phone
  178.         }
  179.       };
  180.  
  181.       return send(Object.assign({}, data_mail));
  182.     },
  183.     order_app: (container, email, service) => {
  184.       let data_mail = {
  185.         from: "contact@tontongaston.fr",
  186.         to: email,
  187.         subject: "[" + container.name + " - " + service + "]",
  188.         template: container.template_mail_order
  189.       };
  190.  
  191.       return send(Object.assign({}, data_mail));
  192.     },
  193.     pro_validate_ttg: (email) => {
  194.       let data_mail = {
  195.         from: "contact@tontongaston.fr",
  196.         to: email,
  197.         subject: "[TontonGaston] Devis en attente",
  198.         template: "Nouveau devis proposé par Tontongaston"
  199.       };
  200.  
  201.       return send(Object.assign({}, data_mail));
  202.     },
  203.     pro_finished: (email, url) => {
  204.       let data_mail = {
  205.         from: "contact@tontongaston.fr",
  206.         to: email,
  207.         subject: "[TontonGaston] Intervention terminée",
  208.         template: "Intervention terminée, Facture en PJ"
  209.       };
  210.  
  211.       return sendFile(Object.assign({}, data_mail), url, 'facture.pdf');
  212.     },
  213.   }
  214. };
  215.  
  216. var download = function (url, dest, cb) {
  217.   var file = fs.createWriteStream(dest);
  218.   var sendReq = request.get(url);
  219.  
  220.   // verify response code
  221.   sendReq.on('response', function (response) {
  222.     if (response.statusCode !== 200) {
  223.       return cb('Response status was ' + response.statusCode);
  224.     }
  225.   });
  226.  
  227.   // check for request errors
  228.   sendReq.on('error', function (err) {
  229.     fs.unlink(dest);
  230.     return cb(err.message);
  231.   });
  232.  
  233.   sendReq.pipe(file);
  234.  
  235.   file.on('finish', function () {
  236.     file.close(cb);  // close() is async, call cb after close completes.
  237.   });
  238.  
  239.   file.on('error', function (err) { // Handle errors
  240.     fs.unlink(dest); // Delete the file async. (But we don't check the result)
  241.     return cb(err.message);
  242.   });
  243. };
  244.  
  245. var send = function (data) {
  246.   return new Promise((resolve, reject) => {
  247.     var str = ejs.render(data.template, data.values);
  248.     var template = ejs.render(fs.readFileSync(path.join(__dirname, "mail.ejs"), 'utf8'), { template: str });
  249.  
  250.     let
  251.       from = new helper.Email(data.from),
  252.       to = new helper.Email(data.to),
  253.       content = new helper.Content('text/html', template);
  254.  
  255.     let mail = new helper.Mail(from, data.subject, to, content);
  256.  
  257.     let request = sendgrid.emptyRequest({
  258.       method: 'POST',
  259.       path: '/v3/mail/send',
  260.       body: mail.toJSON()
  261.     });
  262.  
  263.     sendgrid.API(request, (error, response) => {
  264.       console.log('EMAIL : ' + response.statusCode, error ? response.body.errors : null);
  265.       resolve();
  266.     });
  267.   })
  268. }
  269.  
  270. var sendFile = function (data, file, filename) {
  271.   return new Promise((resolve, reject) => {
  272.     var str = ejs.render(data.template, data.values);
  273.     var template = ejs.render(fs.readFileSync(path.join(__dirname, "mail.ejs"), 'utf8'), { template: str });
  274.     let from = new helper.Email(data.from),
  275.       to = new helper.Email(data.to),
  276.       content = new helper.Content('text/html', template);
  277.  
  278.     let mail = new helper.Mail(from, data.subject, to, content);
  279.     var id = shortid.generate();
  280.     download(file, __dirname + '/../tmp/' + id + '.pdf', () => {
  281.       var attachment = new helper.Attachment();
  282.       var file = fs.readFileSync(__dirname + '/../tmp/' + id + '.pdf');
  283.       var base64File = new Buffer(file).toString('base64');
  284.       attachment.setContent(base64File);
  285.       attachment.setType('application/pdf');
  286.       attachment.setFilename(filename);
  287.       attachment.setDisposition('attachment');
  288.       mail.addAttachment(attachment);
  289.  
  290.       let request = sendgrid.emptyRequest({
  291.         method: 'POST',
  292.         path: '/v3/mail/send',
  293.         body: mail.toJSON()
  294.       });
  295.  
  296.       sendgrid.API(request, (error, response) => {
  297.         console.log('EMAIL : ' + response.statusCode);
  298.         resolve();
  299.       });
  300.     })
  301.   })
  302. }
Add Comment
Please, Sign In to add comment