UNPKG

flexbiz-server

Version:

Flexible Server

6 lines (5 loc) 2.5 kB
const model=global.getModel("group"),controller=require("../../controllers/controller"),getSysorder=function($item$$,$callback$$){let $sysorder$$=$item$$._id.toString();$item$$.group_mother&&$item$$.group_mother!=$item$$._id.toString()?model.findOne({_id:$item$$.group_mother}).lean().then(function($item_me$$){$item_me$$?getSysorder($item_me$$,function($rs$$){$sysorder$$=$rs$$+"---"+$sysorder$$;$callback$$($sysorder$$)}):$callback$$($sysorder$$)}).catch($e$$=>{Logger.error($e$$);$callback$$($sysorder$$)}): $callback$$($sysorder$$)},group=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"group",{sort:{sysorder:1},findExact:{group_type:1},onUpdating:async($user$$,$data$$,$obj$$,$next$$)=>{if($data$$.group_mother&&(await model.getAllChildrenGroup($obj$$.id_app,$obj$$._id.toString())).indexOf($data$$.group_mother)>=0)return $next$$("Kh\u00f4ng th\u1ec3 ch\u1ecdn nh\u00f3m n\u00e0y l\u00e0m nh\u00f3m m\u1eb9");$next$$(null,$data$$,$obj$$)}});$contr_router$$.route();$contr_router$$.deleting= function($user$$,$obj$$,$next$$){if($obj$$.loai_nh===0)return $next$$(Error("Kh\u00f4ng \u0111\u01b0\u1ee3c ph\u00e9p x\u00f3a nh\u00f3m m\u1eb9"));$next$$(null,$obj$$)};$contr_router$$.created=function($user$$,$obj$$,$next$$){getSysorder($obj$$,async function($rs$$){$obj$$.sysorder=$rs$$;$obj$$.bac=$rs$$.split("---").length;await model.updateOne({_id:$obj$$._id},{$set:{bac:$obj$$.bac,sysorder:$obj$$.sysorder,loai_nh:1}}).catch(console.error);$next$$(null,$obj$$)})};$contr_router$$.updated=function($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$$;await model.updateOne({_id:$obj$$._id},{$set:{bac:$obj$$.bac,sysorder:$obj$$.sysorder,loai_nh:$loai_nh_rs$$}}).catch(console.error);$next$$(null,$obj$$)})};$contr_router$$.on("saved",async function($obj$$){const $group_mother$$=$obj$$.group_mother; $group_mother$$&&await model.updateOne({id_app:$obj$$.id_app,_id:$group_mother$$},{$set:{loai_nh:0}}).catch(console.error)});$contr_router$$.on("deleted",async function($obj$$){$obj$$.group_mother&&!await model.findOne({group_mother:$obj$$.group_mother,id_app:$obj$$.id_app}).select("_id").catch(console.error)&&await model.updateOne({id_app:$obj$$.id_app,_id:$obj$$.group_mother},{$set:{loai_nh:1}}).catch(console.error)})};module.exports=group;