flexbiz-server
Version:
Flexible Server
35 lines (34 loc) • 16.2 kB
JavaScript
'use strict';const model=global.getModel("task"),controller=require("../../controllers/controller"),customer=global.getModel("customer"),Participant=global.getModel("participant"),User=global.getModel("user"),UserGroup=global.getModel("usergroup"),Message2setting=global.getModel("message2setting"),Message2=global.getModel("message2"),Group=global.getModel("group"),App=global.getModel("app"),dmdt=global.getModel("dmdt"),Dmbp=global.getModel("dmbp"),Dmnv=global.getModel("dmnv"),dokho=global.getModel("dokho"),
link=global.getModel("link"),Schedule=global.getModel("schedule"),Trangthai=global.getModel("trangthai"),File=global.getModel("file"),so3Controller=global.controllers.SO3,SO3=global.getModel("so3"),async=require("async"),PostSocai=require("../../libs/post-socai"),ma_ct="TASK",Moment=require("moment");
async function createSchedule($data_obj$$){await Schedule.removeScheduleLink($data_obj$$._id.toString());let $time_run$$=new Date($data_obj$$.start_date);$data_obj$$.nhac_nho_truoc=$data_obj$$.nhac_nho_truoc||0;$time_run$$.setMinutes($time_run$$.getMinutes()-$data_obj$$.nhac_nho_truoc);if(!(0!==$data_obj$$.progress||$time_run$$<new Date)&&$data_obj$$.nhac_nho_truoc){$data_obj$$=$data_obj$$.toObject?$data_obj$$.toObject():{...$data_obj$$};var $users_will_receive$$=$data_obj$$.attends||[];$data_obj$$.phu_trach&&
$users_will_receive$$.push($data_obj$$.phu_trach);0===$users_will_receive$$.length&&$users_will_receive$$.push($data_obj$$.user_created);$users_will_receive$$=[...(new Set($users_will_receive$$))];$data_obj$$.title="Nh\u1eafc c\u00f4ng vi\u1ec7c: "+$data_obj$$.ten_cv;$data_obj$$.users_will_receive=$users_will_receive$$;$data_obj$$.code="task";Schedule.addSchedule({id_link:$data_obj$$._id.toString(),action:"sendNotification",data:$data_obj$$,title:$data_obj$$.title,time_run:$time_run$$,trang_thai:"0",
status:!0,user_created:$data_obj$$.user_created,user_updated:$data_obj$$.user_created})}}
const createLink=async($attends_user$$,$obj$$,$next$$)=>{$obj$$.id_kh&&link.find({$or:[{id_a:$obj$$._id,id_b:$obj$$.id_kh},{id_a:$obj$$.id_kh,id_b:$obj$$._id}]}).lean().exec(function($e$$,$rs$$){$rs$$&&0!==$rs$$.length||(new link({id_app:$obj$$.id_app,id_a:$obj$$.id_kh,id_b:$obj$$._id,collection_a:"dmkh",collection_b:"task"})).save(function($e$$){$e$$&&console.error($e$$)})});$obj$$.id_dt&&link.find({$or:[{id_a:$obj$$._id,id_b:$obj$$.id_dt},{id_a:$obj$$.id_dt,id_b:$obj$$._id}]}).lean().exec(function($e$jscomp$0$$,
$rs$$){$rs$$&&0!==$rs$$.length||(new link({id_app:$obj$$.id_app,id_a:$obj$$.id_dt,id_b:$obj$$._id,collection_a:"dmdt",collection_b:"task"})).save(function($e$$){$e$$&&console.error($e$$)})});if($obj$$.nh_cv&&global.mongoose.Types.ObjectId.isValid($obj$$.nh_cv)){$attends_user$$=$obj$$.attends||[];let $_group$$=await Group.findOne({_id:$obj$$.nh_cv});$_group$$&&($_group$$.attends=[...(new Set([...($_group$$.attends||[]),...$attends_user$$]))],$_group$$.save($e$$=>{$e$$&&console.error("Can't update task group",
$e$$,$_group$$)}))}createSchedule($obj$$);$next$$(null,$obj$$)};
module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{sort:{start_date:-1},users_receive_notification_default:[{trang_thai:"0",content:"C\u00f4ng vi\u1ec7c m\u1edbi: {{ten_cv}}",send_phu_trach:!0,send_attends:!0,send_to_user_created:!1},{trang_thai:"1",content:'C\u00f4ng vi\u1ec7c "{{ten_cv}}" \u0111ang \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n',send_phu_trach:!0,send_attends:!0,send_to_customer:!0,send_to_user_created:!1},{trang_thai:"3",
content:'C\u00f4ng vi\u1ec7c "{{ten_cv}}" \u0111\u00e3 ho\u00e0n th\u00e0nh',send_phu_trach:!0,send_attends:!0,send_to_customer:!0,send_to_user_created:!1},{trang_thai:"4",content:'C\u00f4ng vi\u1ec7c "{{ten_cv}}" \u0111\u00e3 t\u1ea1m d\u1eebng',send_phu_trach:!0,send_attends:!0,send_to_user_created:!1},{trang_thai:"5",content:'C\u00f4ng vi\u1ec7c "{{ten_cv}}" \u0111ang ch\u1edd',send_phu_trach:!0,send_attends:!0,send_to_user_created:!1}],onFinding:function($user$$,$condition$$,$next$$){async.series({find_ten_kh:function($callback$$){setImmediate(()=>
{$condition$$.ten_kh?customer.find({id_app:$user$$.current_id_app,ten_kh:{$regex:$condition$$.ten_kh,$options:"i"}},{_id:1}).lean().exec(function($e$$,$khs$$){if($e$$)return $callback$$($e$$);delete $condition$$.ten_kh;$e$$=$khs$$.map($k$$=>$k$$._id);$condition$$.id_kh={$in:$e$$};$callback$$()}):$callback$$()})},find_ten_dt:function($callback$$){setImmediate(()=>{$condition$$.ten_dt?dmdt.find({id_app:$user$$.current_id_app,ten_dt:{$regex:$condition$$.ten_dt,$options:"i"}},{_id:1}).lean().exec(function($e$jscomp$6_id_khs$$,
$khs$$){if($e$jscomp$6_id_khs$$)return $callback$$($e$jscomp$6_id_khs$$);delete $condition$$.ten_dt;$e$jscomp$6_id_khs$$=$khs$$.map($k$$=>$k$$._id);$condition$$.id_dt={$in:$e$jscomp$6_id_khs$$};$callback$$()}):$callback$$()})}},function($e$$){if($e$$)return $next$$($e$$);$next$$(null,$condition$$)})},onFound:($user$$,$condition$$,$items$$,$next$$)=>{$next$$(null,$items$$)},onView:function($user$$,$items$$,$next$$){async.series({trangthai:function($callback$$){setImmediate(()=>{$items$$.filter($item$$=>
void 0!==$item$$.progress).joinModel2(null,Trangthai,[{where:$a$$=>({ma_trang_thai:$a$$.progress.toString(),ma_ct:"TASK"}),fields:["ten_trang_thai","color","allow_update"]}],function(){$callback$$()})})},nh_cv:function($callback$$){setImmediate(()=>{$items$$.filter($item$$=>$item$$.nh_cv).joinModel2($user$$.current_id_app,Group,[{where:{nh_cv:"_id"},fields:[{name:"ten_nh_cv",value:"group_name"}]}],function(){$callback$$(null,$items$$)})})},phu_trach:function($callback$$){setImmediate(()=>{(async()=>
{await $items$$.filter($item$$=>$item$$.phu_trach).asyncJoinModel2($user$$.current_id_app,Participant,[{where:{phu_trach:"email"},fields:[{name:"nhom_phu_trach",value:"group_id"},{name:"ten_phu_trach",value:"name"}]}]);await $items$$.filter($item$$=>$item$$.nhom_phu_trach).asyncJoinModel2($user$$.current_id_app,UserGroup,[{where:{nhom_phu_trach:"_id"},fields:[{name:"ten_nhom_phu_trach",value:"group_name"}]}]);await $items$$.filter($item$$=>$item$$.nguoi_kiem_tra).asyncJoinModel2(null,User,[{where:{nguoi_kiem_tra:"email"},
fields:[{name:"ten_nguoi_kiem_tra",value:"name"}]}]);await $items$$.filter($item$$=>$item$$.nguoi_thuc_hien).asyncJoinModel2(null,User,[{where:{nguoi_thuc_hien:"email"},fields:[{name:"ten_nguoi_thuc_hien",value:"name"}]}])})().then(()=>{$callback$$(null,$items$$)}).catch($e$$=>{$callback$$($e$$)})})},kh:function($callback$$){setImmediate(()=>{$items$$.filter($item$$=>$item$$.id_kh).joinModel2($user$$.current_id_app,customer,[{where:{id_kh:"_id"},fields:[{name:"ten_kh",value:"ten_kh"}]}],function(){$callback$$(null,
$items$$)})})},dkho:function($callback$$){setImmediate(()=>{$items$$.filter($item$$=>$item$$.id_dokho).joinModel2($user$$.current_id_app,dokho,[{where:{id_dokho:"_id"},fields:[{name:"ten_dokho",value:"ten_dokho"}]}],function(){$callback$$(null,$items$$)})})},project:function($callback$$){setImmediate(()=>{$items$$.filter($item$$=>$item$$.id_dt).joinModel2($user$$.current_id_app,dmdt,[{where:{id_dt:"_id"},fields:[{name:"ten_dt",value:"ten_dt"}]}],function(){$callback$$(null,$items$$)})})},bp:function($callback$$){setImmediate(()=>
{$items$$.filter($item$$=>$item$$.id_bp).joinModel2($user$$.current_id_app,Dmbp,[{where:{id_bp:"_id"},fields:[{name:"ten_bp",value:"ten_bp"}]}],function(){$callback$$(null,$items$$)})})},so3:$callback$$=>{setImmediate(()=>{$items$$.filter($item$$=>$item$$.id_ct).joinModel2($user$$.current_id_app,SO3,[{where:$a$$=>({_id:$a$$.id_ct}),fields:["so_ct","ngay_ct","ma_kh","ten_kh"]}],function(){$callback$$(null,$items$$)})})},file:$callback$$=>{setImmediate(()=>{$items$$.filter($item$$=>$item$$.id_link).joinModel2($user$$.current_id_app,
File,[{where:$item$$=>({id_link:$item$$._id}),setFields:($item$$,$data$$)=>{$item$$.avatarFile=$data$$}}],function(){$callback$$(null,$items$$)})})}},function(){setImmediate(()=>{const $last_week$$=new Date;$last_week$$.setDate($last_week$$.getDate()-7);$items$$.forEach(function($item$$){void 0===$item$$.ten_kh&&($item$$.ten_kh="");!$item$$.saved&&$item$$.finished_date&&3===$item$$.progress&&(new Date($item$$.finished_date)).getTime()<=$last_week$$.getTime()&&model.findOneAndUpdate({_id:$item$$._id},
{saved:!0},()=>{})});$next$$(null,$items$$)})})},onCreating:async($user$$,$obj$$,$next$$)=>{$obj$$.attends&&($obj$$.attends=[...(new Set($obj$$.attends))]);setImmediate(async()=>{$obj$$.ma_ct=ma_ct;$obj$$.due_date=$obj$$.due_date||$obj$$.start_date;if(!$obj$$.nh_cv&&$obj$$.group_id_link){var $TY_LE_HOA_HONG_g$$=await Group.findOne({id_link:$obj$$.group_id_link,id_app:$user$$.current_id_app,group_type:"TASK"});$TY_LE_HOA_HONG_g$$?$obj$$.nh_cv=$TY_LE_HOA_HONG_g$$._id.toString():$obj$$.group_name_link&&
($TY_LE_HOA_HONG_g$$=await Group.create({group_type:"TASK",id_link:$obj$$.group_id_link,id_app:$user$$.current_id_app,group_name:$obj$$.group_name_link,phu_trach:$obj$$.phu_trach||$user$$.email,attends:$obj$$.attends}),$obj$$.nh_cv=$TY_LE_HOA_HONG_g$$._id.toString())}$TY_LE_HOA_HONG_g$$=((await App.findOne({_id:$user$$.current_id_app})).exfields||{}).ty_le_hoa_hong||0;var $hoa_hong_tong_tien_hang$$=0;if($obj$$.id_ct){let $so3Obj$$=await SO3.findOne({_id:$obj$$.id_ct});$so3Obj$$&&($so3Obj$$=$so3Obj$$.toObject(),
$hoa_hong_tong_tien_hang$$=$so3Obj$$.details.map($d$$=>$d$$.tien_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,0),$hoa_hong_tong_tien_hang$$=Math.roundBy($hoa_hong_tong_tien_hang$$*$TY_LE_HOA_HONG_g$$/100))}$obj$$.ty_le_hoa_hong=$TY_LE_HOA_HONG_g$$;$obj$$.tien_hoa_hong=$hoa_hong_tong_tien_hang$$;3===$obj$$.progress&&($obj$$.finished_date=$obj$$.finished_date||new Date,$obj$$.da_thu=$obj$$.da_thu||$obj$$.thu_phi);$obj$$.da_thu&&($obj$$.ngay_ct=$obj$$.ngay_ct||new Date);if(!$obj$$.id_bp&&$obj$$.phu_trach)try{let $nv$$=
await Dmnv.findOne({id_app:$obj$$.id_app,user:$obj$$.phu_trach},{ma_bp:1,bo_phan:1}).lean();if($nv$$){let $bo_phan$$=($nv$$.bo_phan||[]).filter($b$$=>!$b$$.den_ngay||$b$$.den_ngay>=new Date);if(0<$bo_phan$$.length){let $id_bp$$=await Dmbp.getInfo($obj$$.id_app,{ma_bp:$bo_phan$$[0].ma_bp},"_id");$obj$$.id_bp=$id_bp$$;await model.findOneAndUpdate({_id:$obj$$._id},{id_bp:$id_bp$$})}else if($nv$$.ma_bp){let $id_bp$$=await Dmbp.getInfo($obj$$.id_app,{ma_bp:$nv$$.ma_bp},"_id");$obj$$.id_bp=$id_bp$$;await model.findOneAndUpdate({_id:$obj$$._id},
{id_bp:$id_bp$$})}}}catch($e$$){console.error($e$$)}$next$$(null,$obj$$)})},onUpdating:async($user$$,$data$$,$obj$$,$next$$)=>{$data$$.attends=$data$$.attends||$obj$$.attends;$data$$.attends&&($data$$.attends=[...(new Set($data$$.attends))]);setImmediate(async()=>{$data$$.ma_ct=$obj$$.ma_ct||ma_ct;$data$$.due_date=$data$$.due_date||$obj$$.due_date||$data$$.start_date||$obj$$.start_date;if(!$obj$$.tien_hoa_hong){var $TY_LE_HOA_HONG$$=((await App.findOne({_id:$obj$$.id_app})).exfields||{}).ty_le_hoa_hong||
0;let $phi_dich_vu_1_lan$$=0,$hoa_hong$$=0;if($obj$$.id_ct){let $so3Obj$$=await SO3.findOne({_id:$obj$$.id_ct});$so3Obj$$&&($so3Obj$$=$so3Obj$$.toObject(),$phi_dich_vu_1_lan$$=$so3Obj$$.details.map($d$$=>$d$$.tien_nt/($d$$.so_lan||1)).reduce(($a$$,$b$$)=>$a$$+$b$$,0),$hoa_hong$$=Math.roundBy($phi_dich_vu_1_lan$$*$TY_LE_HOA_HONG$$/100))}$data$$.phi_dich_vu_1_lan=$phi_dich_vu_1_lan$$;$data$$.ty_le_hoa_hong=$TY_LE_HOA_HONG$$;$data$$.tien_hoa_hong=$hoa_hong$$}1!==$data$$.progress&&2!==$data$$.progress||
$data$$.nguoi_thuc_hien||$obj$$.nguoi_thuc_hien||($data$$.nguoi_thuc_hien=$user$$.email);3===$data$$.progress&&($data$$.nguoi_thuc_hien||$obj$$.nguoi_thuc_hien||($data$$.nguoi_thuc_hien=$user$$.email),2!==$obj$$.progress||$data$$.nguoi_kiem_tra||($data$$.nguoi_kiem_tra=$user$$.email));$TY_LE_HOA_HONG$$=$data$$.progress||$obj$$.progress;3===$TY_LE_HOA_HONG$$&&$obj$$.progress!==$TY_LE_HOA_HONG$$&&($data$$.finished_date=new Date,$data$$.da_thu=$obj$$.da_thu||$data$$.da_thu||$obj$$.thu_phi);if($obj$$.da_thu||
$data$$.da_thu)$data$$.ngay_ct=$obj$$.ngay_ct||$data$$.ngay_ct||new Date;$next$$(null,$data$$,$obj$$)})},onCreated:function($user$$,$obj$jscomp$0$$,$next$$){setImmediate(()=>{createLink($user$$,$obj$jscomp$0$$,function($e$$,$obj$$){if($e$$)return $next$$($e$$);$next$$(null,$obj$$)})})},onUpdated:function($user$$,$obj$jscomp$0$$,$next$$){setImmediate(()=>{createLink($user$$,$obj$jscomp$0$$,async($e$jscomp$11_m2g_so3Obj$$,$obj$$)=>{if($e$jscomp$11_m2g_so3Obj$$)return $next$$($e$jscomp$11_m2g_so3Obj$$);
if($e$jscomp$11_m2g_so3Obj$$=await Message2setting.findOne({id_link:$obj$$._id.toString()})){$obj$$.phu_trach!==$e$jscomp$11_m2g_so3Obj$$.phu_trach&&($e$jscomp$11_m2g_so3Obj$$.phu_trach=$obj$$.phu_trach,$e$jscomp$11_m2g_so3Obj$$.attends.push($obj$$.user_created));var $attends$$=[...(new Set([...$e$jscomp$11_m2g_so3Obj$$.attends,...$obj$$.attends]))];$e$jscomp$11_m2g_so3Obj$$.attends=$attends$$;$e$jscomp$11_m2g_so3Obj$$.save();Message2.updateMany({id_link:$obj$$._id.toString()},{$set:{attends:$attends$$}}).then($rs$$=>
{console.log("update message2",$rs$$)})}if($obj$$.id_ct&&($e$jscomp$11_m2g_so3Obj$$=await SO3.findOne({_id:$obj$$.id_ct}))){$e$jscomp$11_m2g_so3Obj$$=$e$jscomp$11_m2g_so3Obj$$.toObject();$attends$$=await model.countDocuments({id_ct:$obj$$.id_ct,progress:3});const $task_doing$$=await model.countDocuments({id_ct:$obj$$.id_ct,progress:1}),$task_cancel$$=await model.countDocuments({id_ct:$obj$$.id_ct,progress:5}),$total_task$$=await model.countDocuments({id_ct:$obj$$.id_ct});let $so3Obj_views$$=await so3Controller.asyncView($user$$,
[$e$jscomp$11_m2g_so3Obj$$],{});$attends$$!==$total_task$$||"5"==$e$jscomp$11_m2g_so3Obj$$.trang_thai||"4"!=$e$jscomp$11_m2g_so3Obj$$.trang_thai&&"3"!=$e$jscomp$11_m2g_so3Obj$$.trang_thai?(($attends$$||$task_doing$$)&&"3"==$e$jscomp$11_m2g_so3Obj$$.trang_thai&&"4"!=$e$jscomp$11_m2g_so3Obj$$.trang_thai&&(await SO3.findOneAndUpdate({_id:$obj$$.id_ct},{trang_thai:"4"}),$e$jscomp$11_m2g_so3Obj$$.trang_thai="4",controller.pushNotification(so3Controller,$so3Obj_views$$[0],"update")),$total_task$$===$task_cancel$$&&
(await SO3.findOneAndUpdate({_id:$obj$$.id_ct},{trang_thai:"9"}),$e$jscomp$11_m2g_so3Obj$$.trang_thai="9",controller.pushNotification(so3Controller,$so3Obj_views$$[0],"update"))):(await SO3.findOneAndUpdate({_id:$obj$$.id_ct},{trang_thai:"5"}),$e$jscomp$11_m2g_so3Obj$$.trang_thai="5",controller.pushNotification(so3Controller,$so3Obj_views$$[0],"update"))}$next$$(null,$obj$$)})})},onDeleted:async function($user$$,$obj$$,$next$$){setImmediate(async()=>{await Schedule.removeScheduleLink($obj$$._id.toString());
Message2.deleteMany({id_link:$obj$$._id.toString()});Message2setting.deleteOne({id_link:$obj$$._id.toString()});$next$$(null,$obj$$)})},onSaving:async($sl_ht_user$$,$obj$$)=>{3==$obj$$.progress?($obj$$.percent=100,$obj$$.todos&&0<$obj$$.todos.length&&$obj$$.todos.forEach($t$$=>{$t$$.sel=!0})):9==$obj$$.progress?$obj$$.percent=0:$obj$$.todos&&0<$obj$$.todos.length?($sl_ht_user$$=$obj$$.todos.filter($t$$=>$t$$.sel).length,$obj$$.percent=Math.min(100,$sl_ht_user$$/$obj$$.todos.length*100)):$obj$$.percent=
0}});$contr_router$$.post=function($obj$$,$fn$$){$fn$$||($fn$$=function(){});async.series({post_socai:$callback$jscomp$0$$=>{setImmediate(()=>{let $_obj$$;$_obj$$=$obj$$.toObject?$obj$$.toObject():Object.assign({},$obj$$);$_obj$$.so_ct=$_obj$$.so_ct||`TDV${Moment().format("YYMMDDhh:mmss")}`;$_obj$$.ngay_ct=$_obj$$.ngay_ct||new Date;$_obj$$.trang_thai=$_obj$$.progress.toString();$_obj$$.dien_giai=`${$_obj$$.ten_cv}`;$_obj$$.ma_kh=$_obj$$.phu_trach;$_obj$$.ma_nt=$_obj$$.ma_nt||"VND";$_obj$$.ty_gia=
$_obj$$.ty_gia||1;$_obj$$.ma_dvcs=$_obj$$.ma_dvcs||"--";$_obj$$.details=[];$_obj$$.da_thu&&$_obj$$.details.push({dien_giai:`Thu h\u1ed9 c\u00f4ng ty d\u1ecbch v\u1ee5 ${$_obj$$.ten_cv}`,tien_nt:$_obj$$.da_thu||0,tien:$_obj$$.da_thu||0,tk_no:$_obj$$.tk_no||"131111",tk_co:$_obj$$.tk_co||"51131"});$_obj$$.tien_hoa_hong&&$_obj$$.details.push({dien_giai:`Hoa h\u1ed3ng cho d\u1ecbch v\u1ee5 ${$_obj$$.ten_cv}`,tien_nt:$_obj$$.tien_hoa_hong||0,tien:$_obj$$.tien_hoa_hong||0,tk_no:$_obj$$.tk_no_hh||"51131",
tk_co:$_obj$$.tk_co_hh||"131111"});(new PostSocai($_obj$$,$_obj$$.details,function($detail$$,$callback$$){$callback$$($detail$$)})).run(($e$$,$rs$$)=>{$callback$jscomp$0$$($e$$,$rs$$)})})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};$contr_router$$.route()};