UNPKG

flexbiz-server

Version:

Flexible Server

8 lines (7 loc) 3.59 kB
'use strict';const model=global.getModel("usergroup"),participant=global.getModel("participant"),controller=require("../../controllers/controller"),{getCodeChildren}=require("../../libs/utils"),getSysorder=function($item$$,$callback$$){let $sysorder$$=$item$$._id.toString();$item$$.group_mother?model.findOne({_id:$item$$.group_mother}).lean().exec(function($e$$,$item_me$$){$item_me$$?getSysorder($item_me$$,function($rs$$){$sysorder$$=$rs$$+"---"+$sysorder$$;$callback$$($sysorder$$)}):$callback$$($sysorder$$)}): $callback$$($sysorder$$)},usergroup=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"usergroup",{sort:{sysorder:1},onView:async($user$$,$items$$,$next$$)=>{await $items$$.asyncJoinModel2($user$$.current_id_app,participant,{where:{phu_trach:"email"},fields:{ten_phu_trach:"name"}});await $items$$.asyncJoinModel2($user$$.current_id_app,model,{where:{group_mother:"_id"},fields:{group_mother_name:"group_name"}});$next$$(null,$items$$)},onUpdating:async($user$$,$data$$,$obj$$, $next$$)=>{if($data$$.group_mother&&0<=(await getCodeChildren(model,$obj$$.id_app,$obj$$._id.toString(),"_id","group_mother")).indexOf($data$$.group_mother))return $next$$("Kh\u00f4ng th\u1ec3 ch\u1ecdn nh\u00f3m n\u00e0y l\u00e0m nh\u00f3m m\u1eb9");$next$$(null,$data$$,$obj$$)},onCreated:async($rights_user$$,$obj$$,$next$$)=>{$obj$$.group_mother&&($rights_user$$=await global.getModel("right").find({id_app:$obj$$.id_app,email:$obj$$.group_mother}).lean(),0<$rights_user$$.length&&($rights_user$$.forEach($r$$=> {delete $r$$._id;delete $r$$.__v;$r$$.email=$obj$$._id.toString()}),await global.getModel("right").create($rights_user$$)));getSysorder($obj$$,function($rs$$){$obj$$.sysorder=$rs$$;$obj$$.bac=$rs$$.split("---").length;model.findOneAndUpdate({_id:$obj$$._id},{$set:{bac:$obj$$.bac,sysorder:$obj$$.sysorder,loai_nh:1}},function($e$$){$e$$&&console.log($e$$)});$next$$(null,$obj$$)})},onUpdated:async($user$$,$obj$$,$next$$)=>{getSysorder($obj$$,async function($loai_nh_rs$$){$obj$$.sysorder=$loai_nh_rs$$; $obj$$.bac=$loai_nh_rs$$.split("---").length;$loai_nh_rs$$=1;await model.findOne({group_mother:$obj$$._id.toString()})&&($loai_nh_rs$$=0);$obj$$.loai_nh=$loai_nh_rs$$;model.findOneAndUpdate({_id:$obj$$._id},{$set:{bac:$obj$$.bac,sysorder:$obj$$.sysorder,loai_nh:$loai_nh_rs$$}},function($e$$){$e$$&&console.log($e$$)});$next$$(null,$obj$$)})}});$contr_router$$.route();$contr_router$$.deleting=function($user$$,$obj$$,$next$$){if(0===$obj$$.loai_nh)return $next$$(Error("Kh\u00f4ng \u0111\u01b0\u1ee3c ph\u00e9p x\u00f3a nh\u00f3m m\u1eb9")); $next$$(null,$obj$$)};$contr_router$$.deleted=async function($user$$,$obj$$,$next$$){await global.getModel("right").deleteMany({id_app:$obj$$.id_app,email:$obj$$._id.toString()});$next$$(null,$obj$$)};$contr_router$$.on("saved",function($obj$jscomp$0$$){const $group_mother$$=$obj$jscomp$0$$.group_mother;$group_mother$$&&model.findOneAndUpdate({id_app:$obj$jscomp$0$$.id_app,_id:$group_mother$$},{$set:{loai_nh:0}},function($error$$,$obj$$){$error$$?console.error($error$$):$obj$$?console.log("updated "+ $obj$$._id):console.log("Not found mother account:"+$group_mother$$)})});$contr_router$$.on("deleted",function($obj$jscomp$0$$){$obj$jscomp$0$$.group_mother&&model.findOne({group_mother:$obj$jscomp$0$$.group_mother},function($error$jscomp$0$$,$acc$$){$error$jscomp$0$$||$acc$$||model.findOneAndUpdate({id_app:$obj$jscomp$0$$.id_app,_id:$obj$jscomp$0$$.group_mother},{$set:{loai_nh:1}},function($error$$,$obj$$){$error$$||$obj$$&&console.log("updated "+$obj$$._id)})})})};module.exports=usergroup;