UNPKG

kpiplus

Version:

KPI+

1 lines 2.17 kB
var model=require("../../models/mailschedule"),Account=require("../../models/mailaccount"),controller=require("../../controllers/controller"),schedule=require("../../libs/schedule"),arr=require("../../libs/array-funcs"),underscore=require("underscore"),async=require("async");let createSchedule=function(e,n){let t={};underscore.extend(t,e),t.to=[],e.address&&t.to.push({address:e.address,name:e.address}),e.cc&&t.to.push({address:e.cc,name:e.cc}),async.map(e.to,function(e,n){if(e.address&&t.to.push(e),e.contact&&e.contact.model){let l=require("../../models/"+e.contact.model),a=e.contact.query?e.contact.query:{},c=e.contact.field_email?e.contact.field_email:"email",o=e.contact.field_name?e.contact.field_name:"name";l.find(a).lean().exec(function(e,l){if(e)return n(e);for(let e of l)e[c]&&t.to.push({address:e[c],name:e[o]});n()})}else n()},function(l,a){schedule.deleteMailJob(e._id,function(e){schedule.createMailJob(t),n(null,t)})})};module.exports=function(e){this.contr=new controller(e,model,"mailschedule",{sort:{date_created:-1},notNeedRight:!0,onView:function(e,n,t){n.joinModel(e.current_id_app,Account,[{where:{_id:"account_id"},fields:[{name:"from_name",value:"fullname"},{name:"from_address",value:"username"}]}],function(e){n.forEach(function(e){e.from=[{name:e.from_name,address:e.from_address}]}),t(null,n)})},onFinding:function(e,n,t){n.user_created=e.email,t(null,n)},onCreating:function(e,n,t){if(n.mail&&n.mail.html){var l=n.mail.html.replace(/<(?:.|\n)*?>/gm,"");n.mail.text=l,n.mail.text.length>128?n.small_text=n.mail.text.substring(0,128)+"...":n.small_text=n.mail.text}t(null,n)},onCreated:function(e,n,t){createSchedule(n,function(e,l){t(null,n)})},onUpdating:function(e,n,t,l){if(t.send)return l("Bạn không thể cập nhật email này vì nó đã được gửi");if(n.mail&&n.mail.html){var a=n.mail.html.replace(/<(?:.|\n)*?>/gm,"");n.mail.text=a,n.mail.text.length>128?n.small_text=n.mail.text.substring(0,128)+"...":n.small_text=n.mail.text}l(null,n,t)},onUpdated:function(e,n,t){createSchedule(n,function(e,l){t(null,n)})},onDeleted:function(e,n,t){schedule.deleteMailJob(n._id,function(e){t(null,n)})}}),this.contr.route()};