UNPKG

kpiplus

Version:

KPI+

1 lines 1.68 kB
let async=require("async"),fs=require("fs"),waitevent=require("./waitevent"),campaignSchema=new Schema({id_app:{type:String,required:!0},title:{type:String,required:!0},steps:Schema.Types.Mixed,exfields:Schema.Types.Mixed,status:{type:Boolean,default:!0},date_created:{type:Date,default:Date.now},date_updated:{type:Date,default:Date.now},user_created:{type:String,default:""},user_updated:{type:String,default:""},visible_to:{type:Number,default:0},visible_to_users:[String],dataview_to:{type:Number,default:0},dataview_to_users:[String]});campaignSchema.index({id_app:1,title:1});let model=mongoose.model("campaign",campaignSchema);model.getEvents=function(e){let t=[];return e&&e.children&&e.children.length>0&&e.children.forEach(e=>{"waitevent"==e.step_style&&e.model_id&&t.push(e),t=t.concat(model.getEvents(e))}),t},model.deleteEvents=((e,t)=>{let i=model.getEvents(e.steps);async.map(i,(t,i)=>{waitevent.findOne({_id:t.model_id}).lean().exec((n,a)=>{a&&a.type&&globalEvents.removeAllListeners(a.type+"_"+e._id+"_"+("schedule"==a.type?t._id:a.exfields[a.type])),i()})},(e,i)=>{t()})}),model.listenerEvents=((e,t)=>{let i=model.getEvents(e.steps);async.map(i,(t,i)=>{waitevent.findOne({_id:t.model_id}).lean().exec((n,a)=>{a&&a.type&&globalEvents.on(a.type+"_"+e._id+"_"+("schedule"==a.type?t._id:a.exfields[a.type]),function(i){t.children&&t.children.forEach(t=>{t.children&&t.children.forEach(t=>{if(t.action_model&&t.action_func){let n=rootDir+"/actions/"+t.action_model+".js";if(fs.existsSync(n)){let n=require("../actions/"+t.action_model);n.actions&&n.actions[t.action_func]&&n.actions[t.action_func](e,i,t,e=>{})}}})})}),i()})},(e,i)=>{t()})}),module.exports=model;