UNPKG

flexbiz-server

Version:

Flexible Server

142 lines (133 loc) 4.3 kB
var email = require('../libs/email'); var parseTemplate = require('../libs/parse-template').parseTemplate; const async =require('async'); var Mailsent = global.getModel('mailsent'); var Customer = global.getModel('customer'); var Lienhe = global.getModel('lienhe'); var Link = global.getModel('link'); const underscore =require("underscore"); var mailtemplate = global.getModel("mailtemplate"); let model = { } model.send = (obj,done)=>{ //begin send email email.getAccount(obj.account_id, function(e, account) { if (e) { console.error("Find email account",e); //return done(); } if(!obj.to) obj.to =[] if(obj.address){ obj.to.push({address:obj.address,name:obj.address}); } async.mapSeries(obj.to, function(to, callback) { var obj4parse = {}; underscore.extend(obj4parse, obj); if(obj.data){ underscore.extend(obj4parse, obj.data); } obj4parse.receiver = to; if(!obj4parse.cid) obj4parse.cid ="1"; if(!obj4parse.receiver._id) obj4parse.receiver._id = "1"; parseTemplate(obj4parse.mail.html, obj4parse, function(error, html) { if (error) return callback(error); email.sendHtml({account: account, to: to,cc:obj.cc,bcc:obj.bcc, subject: obj4parse.subject, html: html,attachments:obj4parse.attachments},function(error, info) { if (error) { error = error.toString(); if (error.indexOf('ENOTFOUND') >= 0) { callback(error); }else { callback(); } }else { //save mail sent obj4parse.mail.html = html; obj4parse.mail.text = obj4parse.mail.html.replace(/<(?:.|\n)*?>/gm, ''); if (obj4parse.mail.text) { if (obj4parse.mail.text.length > 128) { obj4parse.small_text = obj4parse.mail.text.substring(0, 128) + '...'; }else { obj4parse.small_text = obj4parse.mail.text; } } obj4parse.to = [to]; obj4parse.date_created = new Date(); obj4parse.date_updated = new Date(); delete obj4parse._id; if(!obj4parse.account_id){ obj4parse.account_id = "SYSTEM"; } var mailsent = new Mailsent(obj4parse); mailsent.save(function(e, sent) { if (e) return console.log(e); //create link async.parallel({ contact: function(callback) { Lienhe.find({email: to.address, id_app: sent.id_app}).lean().exec(function(e, lhs) { if (lhs) { async.mapSeries(lhs, function(lh, callback) { var link = new Link({id_app: sent.id_app, collection_a: 'lienhe', id_a: lh._id, collection_b: 'mailsent', id_b: sent._id}); link.save(function(e) { if (e) console.log(e); callback(); }); },function(e, rs) { callback(); }); }else { callback(); } }); }, cust: function(callback) { Customer.find({email: to.address, id_app: sent.id_app}).lean().exec(function(e, lhs) { if (lhs) { async.mapSeries(lhs, function(lh, callback) { var link = new Link({id_app: sent.id_app, collection_a: 'customer', id_a: lh._id, collection_b: 'mailsent', id_b: sent._id}); link.save(function(e) { if (e) console.log(e); callback(); }); },function(e, rs) { callback(); }); }else { callback(); } }); } },function(e, r) { //callback(e,r) }); }); callback(); } }); }); },function(e, rs) { done(e,rs); }); }); } model.actions ={ send:(campaign,obj_ev,step,next)=>{ setImmediate(()=>{ if(obj_ev && obj_ev.lienhe){ mailtemplate.findById(step.model_id).lean().exec((e,template)=>{ if(template){ template.to = [{name:obj_ev.lienhe.ten_lien_he,address:obj_ev.lienhe.email,_id:obj_ev.lienhe._id}]; template.cid = campaign._id.toString(); model.send(template,(e)=>{ next(); }) }else{ next(); } }) }else{ next(); } }) } } module.exports = model;