flexbiz-server
Version:
Flexible Server
27 lines (26 loc) • 13 kB
JavaScript
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);$chil_nh_kh_nh_vt_cons$$.length>0&&($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:{ten_kh:-1},unique:["ma_kh"],cache:!0,onFinding:function($user$$,$condition$$,$next$$){if($condition$$.sinh_nhat_ngay||$condition$$.is_birthday){var $currentDay_today$$=new Date($condition$$.sinh_nhat_ngay);const $currentMonth$$=$currentDay_today$$.getMonth()+1;$currentDay_today$$=$currentDay_today$$.getDate();
const $serverTimezone$$=Intl.DateTimeFormat().resolvedOptions().timeZone;$condition$$.$expr=$condition$$.$expr||{};$condition$$.$expr.$and=$condition$$.$expr.$and||[];$condition$$.$expr.$and.push({$eq:[{$month:{date:"$ngay_sinh",timezone:$serverTimezone$$}},$currentMonth$$]});$condition$$.$expr.$and.push({$eq:[{$dayOfMonth:{date:"$ngay_sinh",timezone:$serverTimezone$$}},$currentDay_today$$]});delete $condition$$.sinh_nhat_ngay;delete $condition$$.is_birthday}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$$=>{Logger.info("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&&typeof $condition$$.nh_kh=="string"?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&&
typeof $condition$$.nh_kh2=="string"?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&&typeof $condition$$.nh_kh3=="string"?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&&typeof $condition$$.nh_kh4=="string"?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&&typeof $condition$$.nh_kh5=="string"?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$$)})},onFound:($user$$,$condition$$,$items$$,$next$$)=>{$next$$(null,$items$$)},onCreating:($user$$,$obj$$,$next$$)=>{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`);
$next$$(null,$obj$$)},onUpdating:($user$$,$data$$,$obj$$,$next$$)=>{if($data$$.dien_thoai&&!isMobilePhone($data$$.dien_thoai,"vi-VN"))return $next$$(`S\u1ed1 \u0111i\u1ec7n tho\u1ea1i ${$data$$.dien_thoai} l\u00e0 kh\u00f4ng h\u1ee3p l\u1ec7`);if($data$$.dien_thoai2&&!isMobilePhone($data$$.dien_thoai2,"vi-VN"))return $next$$(`S\u1ed1 \u0111i\u1ec7n tho\u1ea1i ${$data$$.dien_thoai2} l\u00e0 kh\u00f4ng h\u1ee3p l\u1ec7`);$next$$(null,$data$$,$obj$$)},onView:function($user$$,$items$$,$next$$,$options$$){$items$$.forEach($item$$=>
{$item$$.email&&!validator.isEmail($item$$.email)&&($item$$.email="");delete $item$$.default_password});async.parallel({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$$()})},nh_kh2_3_4_5:$callback$$=>{setImmediate(async()=>{var $ma_nh_kh2_ma_nh_khs_obj_nh_kh$$=$items$$.map($item$$=>$item$$.nh_kh2||[]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);let $ma_nh_kh3$$=$items$$.map($item$$=>$item$$.nh_kh3||[]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]),$ma_nh_kh4$$=$items$$.map($item$$=>
$item$$.nh_kh4||[]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);$ma_nh_kh2_ma_nh_khs_obj_nh_kh$$=[...$ma_nh_kh2_ma_nh_khs_obj_nh_kh$$,...$ma_nh_kh3$$,...$ma_nh_kh4$$];if($ma_nh_kh2_ma_nh_khs_obj_nh_kh$$.length===0)return $callback$$();$ma_nh_kh2_ma_nh_khs_obj_nh_kh$$=[...(new Set($ma_nh_kh2_ma_nh_khs_obj_nh_kh$$))];$ma_nh_kh2_ma_nh_khs_obj_nh_kh$$=await group.find({id_app:$user$$.current_id_app,_id:{$in:$ma_nh_kh2_ma_nh_khs_obj_nh_kh$$}},{group_name:1}).lean();let $set_nh_kh$$={};for(let $nh_kh$$ of $ma_nh_kh2_ma_nh_khs_obj_nh_kh$$)$set_nh_kh$$[$nh_kh$$._id.toString()]=
$nh_kh$$.group_name;for(let $item$$ of $items$$)$item$$.nh_kh2&&($item$$.ten_nh_kh2=$item$$.nh_kh2.map($kh$$=>$set_nh_kh$$[$kh$$])),$item$$.nh_kh3&&($item$$.ten_nh_kh3=($item$$.nh_kh3||[]).map($kh$$=>$set_nh_kh$$[$kh$$])),$item$$.nh_kh4&&($item$$.ten_nh_kh4=($item$$.nh_kh4||[]).map($kh$$=>$set_nh_kh$$[$kh$$]));$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_npp:"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$$=>{$options$$&&$options$$.req&&$options$$.req.query&&(!$options$$.req.query.fields||$options$$.req.query.fields.indexOf("ngay_mua_gan_nhat")>=
0||$options$$.req.query.fields.indexOf("ngay_mua_dau_tien")>=0)&&model.updateActives?async.map($items$$.filter($item$$=>$item$$.ngay_mua_gan_nhat===void 0||!$item$$.ngay_mua_dau_tien||$item$$.ngay_tuong_tac_gan_nhat===void 0),($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$$=>{Logger.error($e$$);$callback$$()})},
()=>{$callback$$()}):$callback$$()},so_don_hang_bds:$callback$$=>{$options$$&&$options$$.req&&$options$$.req.query&&$options$$.req.query.fields&&$options$$.req.query.fields.indexOf("so_don_hang_bds")>=0?async.map($items$$,($item$$,$callback$$)=>{setImmediate(async()=>{$item$$.so_don_hang_bds=await global.getModel("bds_nhadat_moigioi_ban").countDocuments({id_app:$item$$.id_app,ma_kh:$item$$.ma_kh,user_created:$user$$.email});$callback$$()})},()=>{$callback$$()}):$callback$$()},note:$callback$$=>{$options$$&&
$options$$.req&&$options$$.req.query&&$options$$.req.query.fields&&$options$$.req.query.fields.indexOf("latest_note")>=0?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$$){Logger.info("cache customer data",$e$$)}))},()=>{$callback$$()}):$callback$$()}},()=>{$next$$(null,$items$$)})}});$contr_router$$.validating=async($id_app$jscomp$1_user$$,$obj$$,$next$$,$options$$={})=>{if($options$$.action=="add"&&!$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$$){Logger.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$$,$rs$$)=>{if($rs$$)Participant.createParticipant({id_app:$data$$.id_app,email:$data$$.of_user,group_id:$_usergroup$$._id,active:!0},$e$$=>{if($e$$)return Logger.info("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$$=>{if($e$$)return Logger.info($e$$.message);Participant.createParticipant({id_app:$data$$.id_app,email:$result$$.email,group_id:$_usergroup$$._id,active:!0},$e$$=>{if($e$$)return Logger.info("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($custs$$.length<30)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;