UNPKG

kpiplus

Version:

KPI+

1 lines 3.59 kB
var model=require("../../models/task"),controller=require("../../controllers/controller"),customer=require("../../models/customer"),User=require("../../models/user"),dmdt=require("../../models/dmdt"),dokho=require("../../models/dokho"),link=require("../../models/link"),arrays=require("../../libs/array-funcs"),schedule=require("../../libs/schedule"),async=require("async"),underscore=require("underscore");module.exports=function(e){var n=function(e,n,t){n.id_kh&&link.find({$or:[{id_a:n._id,id_b:n.id_kh},{id_a:n.id_kh,id_b:n._id}]}).lean().exec(function(e,t){t&&0!==t.length||new link({id_app:n.id_app,id_a:n.id_kh,id_b:n._id,collection_a:"dmkh",collection_b:"task"}).save(function(e,n){e&&console.error(e)})}),n.id_dt&&link.find({$or:[{id_a:n._id,id_b:n.id_dt},{id_a:n.id_dt,id_b:n._id}]}).lean().exec(function(e,t){t&&0!==t.length||new link({id_app:n.id_app,id_a:n.id_dt,id_b:n._id,collection_a:"dmdt",collection_b:"task"}).save(function(e,n){e&&console.error(e)})}),schedule.createTaskJob(n),t(null,n)};this.contr=new controller(e,model,"task",{sort:{start_date:-1},onFinding:function(e,n,t){async.parallel({find_ten_kh:function(e){n.ten_kh?customer.find({ten_kh:{$regex:n.ten_kh,$options:"i"}},{_id:1}).lean().exec(function(t,i){if(t)return e(t);delete n.ten_kh,id_khs=underscore.pluck(i,"_id"),n.id_kh={$in:id_khs},e()}):e()},find_ten_dt:function(e){n.ten_dt?dmdt.find({ten_dt:{$regex:n.ten_dt,$options:"i"}},{_id:1}).lean().exec(function(t,i){if(t)return e(t);delete n.ten_dt;var d=underscore.pluck(i,"_id");n.id_dt={$in:d},e()}):e()}},function(e,i){if(e)return t(e);t(null,n)})},onView:function(e,n,t){async.parallel({phu_trach:function(e){n.joinModel2(null,User,[{where:{phu_trach:"email"},fields:[{name:"ten_phu_trach",value:"name"}]}],function(t){e(null,n)})},kh:function(t){n.joinModel2(e.current_id_app,customer,[{where:{id_kh:"_id"},fields:[{name:"ten_kh",value:"ten_kh"}]}],function(e){t(null,n)})},dkho:function(t){n.joinModel2(e.current_id_app,dokho,[{where:{id_dokho:"_id"},fields:[{name:"ten_dokho",value:"ten_dokho"}]}],function(e){t(null,n)})},project:function(t){n.joinModel2(e.current_id_app,dmdt,[{where:{id_dt:"_id"},fields:[{name:"ten_dt",value:"ten_dt"}]}],function(e){t(null,n)})}},function(e,i){n.forEach(function(e){void 0===e.ten_kh&&(e.ten_kh=""),e.percent||(e.percent=0)}),t(null,n)})},onCreating:function(e,n,t){1==n.progress&&(n.percent=50),2==n.progress&&(n.percent=100),n.due_date||(n.due_date=new Date),2!==n.progress||n.finished_date||(n.finished_date=new Date),t(null,n)},onUpdating:function(e,n,t,i){n.percent||(n.percent=0),1==n.progress&&(n.percent=50),2==n.progress&&(n.percent=100),n.due_date||(n.due_date=new Date),2!==n.progress||t.finished_date||(n.finished_date=new Date),i(null,n,t)},onCreated:function(e,t,i){n(0,t,function(e,n){if(e)return i(e);var t;(t=n.toObject?n.toObject():n).code="task",t.action="NEW",t.title="Công việc mới",t.body=t.ten_cv,n.phu_trach!==n.user_created&&User.emitEvent(n.phu_trach,"task:new",t),n.attends&&n.attends.forEach(function(e){e!==n.phu_trach&&e!==n.user_created&&User.emitEvent(e,"task:new",t)}),i(null,n)})},onUpdated:function(e,t,i){n(0,t,function(e,n){if(e)return i(e);var t;(t=n.toObject?n.toObject():n).code="task",t.action="UPDATE",t.title="Cập nhật công việc",t.body=t.ten_cv,n.phu_trach!==n.user_updated&&User.emitEvent(n.phu_trach,"task:update",t),n.attends&&n.attends.forEach(function(e){e!==n.phu_trach&&e!==n.user_updated&&User.emitEvent(e,"task:update",t)}),i(null,n)})},onDeleted:function(e,n,t){schedule.deleteTaskJob(n._id,function(e){t(null,n)})}}),this.contr.route()};