flexbiz-server
Version:
Flexible Server
21 lines (20 loc) • 10.2 kB
JavaScript
'use strict';const model=global.getModel("customer"),async=require("async"),dmnhkh=global.getModel("group"),User=global.getModel("user"),Trangthai=global.getModel("trangthai"),Participant=global.getModel("participant"),Usergroup=global.getModel("usergroup"),validator=require("validator"),group=global.getModel("group"),controller=require("../../controllers/controller"),redisCache=require("../../libs/redis-cache"),permission=require("../../libs/permission"),{isMobilePhone}=require("../../libs/utils"),
getAllChildrenUserGroup=async($id_app$$,$chil_nh_kh_nh_vt_cons$$)=>{let $rs$$=[$chil_nh_kh_nh_vt_cons$$];$chil_nh_kh_nh_vt_cons$$=(await Usergroup.find({id_app:$id_app$$,group_mother:$chil_nh_kh_nh_vt_cons$$,status:!0},{_id:1,group_mother:1}).lean()).filter($n$$=>$n$$._id.toString()!=$n$$.group_mother).map($d$$=>$d$$._id);0<$chil_nh_kh_nh_vt_cons$$.length&&($chil_nh_kh_nh_vt_cons$$=await Promise.all($chil_nh_kh_nh_vt_cons$$.map($nh$$=>getAllChildrenUserGroup($id_app$$,$nh$$))),$chil_nh_kh_nh_vt_cons$$=
$chil_nh_kh_nh_vt_cons$$.reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]),$rs$$=[...$chil_nh_kh_nh_vt_cons$$,...$rs$$]);return $rs$$},customer=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"customer",{sort:{date_updated:-1},unique:["ma_kh"],cache:!0,onFinding:function($user$$,$condition$$,$next$$){async.parallel({user_group:$callback$$=>{$condition$$.user_group?getAllChildrenUserGroup($condition$$.id_app,$condition$$.user_group).then($groups$$=>{global.getModel("participant").find({id_app:$user$$.current_id_app,
group_id:{$in:$groups$$}},{email:1}).then($ps$$=>{$condition$$.phu_trach={$in:$ps$$.map($p$$=>$p$$.email)};delete $condition$$.user_group;$callback$$()}).catch($e$$=>{$callback$$($e$$)})}).catch($e$$=>{$callback$$($e$$)}):$callback$$()},user_of_bp:$callback$$=>{$condition$$.ma_bp?global.getModel("dmbp").getUsersByMaBP($user$$.current_id_app,$condition$$.ma_bp).then($users$$=>{console.log("users of department",$users$$);$condition$$.phu_trach={$in:$users$$};delete $condition$$.ma_bp;$callback$$()}).catch($e$$=>
{$callback$$($e$$)}):$callback$$()},nh_kh:$callback$$=>{$condition$$.nh_kh&&"string"==typeof $condition$$.nh_kh?dmnhkh.getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh,{status:!0,group_kind:1}).then($nh_kh$$=>{$condition$$.nh_kh={$in:[...(new Set($nh_kh$$))]};$callback$$()}):$callback$$()},nh_kh2:$callback$$=>{$condition$$.nh_kh2&&"string"==typeof $condition$$.nh_kh2?dmnhkh.getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh2,{status:!0,group_kind:2}).then($nh_kh$$=>{$condition$$.nh_kh2=
{$in:[...(new Set($nh_kh$$))]};$callback$$()}):$callback$$()},nh_kh3:$callback$$=>{$condition$$.nh_kh3&&"string"==typeof $condition$$.nh_kh3?dmnhkh.getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh3,{status:!0,group_kind:3}).then($nh_kh$$=>{$condition$$.nh_kh3={$in:[...(new Set($nh_kh$$))]};$callback$$()}):$callback$$()},nh_kh4:$callback$$=>{$condition$$.nh_kh4&&"string"==typeof $condition$$.nh_kh4?dmnhkh.getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh4,{status:!0,group_kind:4}).then($nh_kh$$=>
{$condition$$.nh_kh4={$in:[...(new Set($nh_kh$$))]};$callback$$()}):$callback$$()},nh_kh5:$callback$$=>{$condition$$.nh_kh5&&"string"==typeof $condition$$.nh_kh5?dmnhkh.getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh5,{status:!0,group_kind:5}).then($nh_kh$$=>{$condition$$.nh_kh5={$in:[...(new Set($nh_kh$$))]};$callback$$()}):$callback$$()}},()=>{$next$$(null,$condition$$)})},onView:function($user$$,$items$$,$next$$,$options$$){$items$$.forEach($item$$=>{$item$$.email&&!validator.isEmail($item$$.email)&&
($item$$.email="");delete $item$$.default_password});async.series({pt:$callback$$=>{$items$$.filter($item$$=>$item$$.phu_trach).joinModel2(null,User,[{where:{phu_trach:"email"},fields:[{ten_phu_trach:"name"}]}],function(){$callback$$()})},nhan_vien_phu_trach:$callback$$=>{$items$$.filter($item$$=>$item$$.phu_trach).joinModel2($user$$.current_id_app,"dmnv",[{where:{phu_trach:"user"},fields:[{ma_nv_phu_trach:"ma_nv"},{ten_nv_phu_trach:"ten_nv"},{ma_bp_nv:"ma_bp"}]}],function(){$callback$$()})},group:$callback$$=>
{$items$$.filter($item$$=>$item$$.nh_kh).joinModel2($user$$.current_id_app,group,[{where:{nh_kh:"_id"},fields:[{ten_nh_kh:"group_name"}]}],function(){$callback$$()})},npp:$callback$$=>{$items$$.filter($item$$=>$item$$.ma_kho_npp).joinModel2($user$$.current_id_app,"dmkho",[{where:{ma_kho_npp:"ma_kho"},fields:[{ten_kho:"ten_kho"}]}],function(){$callback$$()})},u:$callback$$=>{$items$$.filter($item$$=>$item$$.of_user).joinModel2(null,User,[{where:{of_user:"email"},fields:[{user_name:"name"}]}],function(){$callback$$()})},
tt:$callback$$=>{$items$$.filter($item$$=>$item$$.trang_thai).joinModel2(null,Trangthai,[{where:$item$$=>({ma_trang_thai:$item$$.trang_thai,ma_ct:"CUSTOMER"}),fields:["ten_trang_thai","color"]}],function(){$callback$$()})},ngay_mua:$callback$jscomp$0$$=>{$options$$&&$options$$.req&&$options$$.req.query&&(!$options$$.req.query.fields||0<=$options$$.req.query.fields.indexOf("ngay_mua_gan_nhat")||0<=$options$$.req.query.fields.indexOf("ngay_mua_dau_tien"))&&model.updateActives?async.map($items$$.filter($item$$=>
void 0===$item$$.ngay_mua_gan_nhat||!$item$$.ngay_mua_dau_tien||void 0===$item$$.ngay_tuong_tac_gan_nhat),($item$$,$callback$$)=>{model.updateActives($item$$).then($rs$$=>{$item$$.ngay_mua_gan_nhat=$rs$$.ngay_mua_gan_nhat;$item$$.ngay_mua_dau_tien=$rs$$.ngay_mua_dau_tien;$item$$.so_ngay_da_mua=$rs$$.so_ngay_da_mua;$callback$$()}).catch($e$$=>{console.error($e$$);$callback$$()})},()=>{$callback$jscomp$0$$()}):$callback$jscomp$0$$()},note:$callback$$=>{$options$$&&$options$$.req&&$options$$.req.query&&
$options$$.req.query.fields&&0<=$options$$.req.query.fields.indexOf("latest_note")?async.map($items$$.filter($i$$=>!$i$$.latest_note),async $c_item$$=>{let $latest_note$$=await global.getModel("note").findOne({id_app:$c_item$$.id_app,id_link:$c_item$$._id.toString()},{content:1,date_created:1,user_created:1}).sort({date_created:-1}).lean();$latest_note$$&&($c_item$$.latest_note=$latest_note$$,await model.updateOne({_id:$c_item$$._id},{latest_note:$latest_note$$}),$c_item$$=await model.findById($c_item$$._id),
redisCache.set("customer",$c_item$$.toObject(),function($e$$){console.log("cache customer data",$e$$)}))},()=>{$callback$$()}):$callback$$()}},()=>{$next$$(null,$items$$)})}});$contr_router$$.validating=async($id_app$jscomp$1_user$$,$obj$$,$next$$,$options$$={})=>{if($obj$$.dien_thoai&&!isMobilePhone($obj$$.dien_thoai,"vi-VN"))return $next$$(`S\u1ed1 \u0111i\u1ec7n tho\u1ea1i ${$obj$$.dien_thoai} l\u00e0 kh\u00f4ng h\u1ee3p l\u1ec7`);if($obj$$.dien_thoai2&&!isMobilePhone($obj$$.dien_thoai2,"vi-VN"))return $next$$(`S\u1ed1 \u0111i\u1ec7n tho\u1ea1i ${$obj$$.dien_thoai2} l\u00e0 kh\u00f4ng h\u1ee3p l\u1ec7`);
if("add"==$options$$.action&&!$obj$$.ma_kh&&$obj$$.ten_kh){$id_app$jscomp$1_user$$=$id_app$jscomp$1_user$$.current_id_app;try{let $ma_kh$$=await model.asyncCreateCustomerCode($id_app$jscomp$1_user$$,$obj$$.dien_thoai||$obj$$.ten_kh);if(!$ma_kh$$)return $next$$("Kh\u00f4ng th\u1ec3 t\u1ea1o m\u00e3 kh\u00e1ch h\u00e0ng");$obj$$.ma_kh=$ma_kh$$;$next$$(null,$obj$$)}catch($e$$){console.error("Kh\u00f4ng th\u1ec3 t\u1ea1o m\u00e3 kh\u00e1ch h\u00e0ng",$obj$$.ten_kh,$e$$),$next$$(`Kh\u00f4ng th\u1ec3 t\u1ea1o m\u00e3 kh\u00e1ch h\u00e0ng, L\u1ed7i: ${$e$$.message||
$e$$.error||$e$$}`)}}else $next$$(null,$obj$$)};$contr_router$$.route();$contr_router$$.on("saved",async $data$$=>{let $_usergroup$$;try{$_usergroup$$=await Usergroup.getInfo($data$$.id_app,{is_customer_group:!0})}catch($e$$){return}$data$$.of_user&&$_usergroup$$&&User.findOne({email:$data$$.of_user},($e$jscomp$1$$,$rs$$)=>{if($rs$$)Participant.createParticipant({id_app:$data$$.id_app,email:$data$$.of_user,group_id:$_usergroup$$._id,active:!0},$e$$=>{if($e$$)return console.log("can't add user",$data$$.of_user,
"to company")});else{var $result$$=new User;$result$$.email=$data$$.of_user;$result$$.name=$data$$.ten_kh;$result$$.email2=$data$$.email;$result$$.local={};$result$$.local.email=$result$$.email;$result$$.local.name=$result$$.name;$result$$.local.picture="/images/avatar.jpg";$result$$.local.password=$result$$.generateHash($result$$.email+($data$$.default_password||$result$$.email+"@123456"));$result$$.save($e$jscomp$0$$=>{if($e$jscomp$0$$)return console.log($e$jscomp$0$$.message);Participant.createParticipant({id_app:$data$$.id_app,
email:$result$$.email,group_id:$_usergroup$$._id,active:!0},$e$$=>{if($e$$)return console.log("can't add user",$result$$.email,"to company")})})}})});$contr_router$$.createRoute("qc/sms",async($req$$,$next$$)=>{let $body$$=$req$$.body||{};if(!$body$$.id_account)return $next$$("Thi\u1ebfu t\u00e0i kho\u1ea3n sms");if(!$body$$.content)return $next$$("Thi\u1ebfu n\u1ed9i dung tin nh\u1eafn");let $account_sms$$=await global.getModel("smsaccount").findOne({_id:$body$$.id_account,id_app:$req$$.user.current_id_app}).lean();
if(!$account_sms$$)return $next$$("T\u00e0i kho\u1ea3n sms kh\u00f4ng t\u1ed3n t\u1ea1i");permission.hasRight($req$$.user.current_id_app,$req$$.user.email,"smssend","add",async function($error$$,$permission$$){if(!$permission$$)return $next$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n g\u1eedi tin nh\u1eafn qu\u1ea3ng c\u00e1o");$error$$=($req$$.body||{}).q||{};$error$$.dien_thoai||($error$$.dien_thoai={$exists:!0});$error$$.id_app=$req$$.user.current_id_app;let $custs$$=[...(new Set((await model.find($error$$,
{dien_thoai:1}).lean()).map($c$$=>$c$$.dien_thoai)))];if(30>$custs$$.length)return $next$$("C\u1ea7n g\u1eedi cho t\u1ed1i thi\u1ec3u 30 kh\u00e1ch h\u00e0ng");$custs$$=$custs$$.join(",");setImmediate(async()=>{let $result$$;try{await global.getModel("smsaccount").send($account_sms$$,$custs$$,$body$$.content),$result$$={msg:"\u0110\u00e3 g\u1eedi y\u00eau c\u1ea7u th\u00e0nh c\u00f4ng",code:0}}catch($e$$){$result$$={msg:"Kh\u00f4ng th\u1ec3 g\u1eedi",error:$e$$.message||$e$$.error||$e$$,code:-1}}await global.getModel("smssend").create({id_app:$req$$.user.current_id_app,
phone:$custs$$,content:$body$$.content,user_created:$req$$.user.email,result:$result$$});$next$$()})})},{method:"POST"})};module.exports=customer;