UNPKG

flexbiz-server

Version:

Flexible Server

8 lines (7 loc) 3.07 kB
const async=require("async"),calllog=global.getModel("calllog"),customer=global.getModel("customer"),lienhe=global.getModel("lienhe"),App=global.getModel("app"),Controller=require("../../controllers/controller"); module.exports=function($router$$){$router$$.route("/:id_app/call/:token").get(async function($req$$,$res$$){let $g_callData$$={...$req$$.query,...$req$$.body};$g_callData$$.phone=$g_callData$$.phone||$g_callData$$.src;$g_callData$$.extend=$g_callData$$.extend||$g_callData$$.dst;$g_callData$$.call_status=$g_callData$$.state||$g_callData$$.status||$g_callData$$.call_status;delete $g_callData$$.status;if(!$g_callData$$.phone)return $res$$.status(400).send({errors:"Phone number is required"});let $token$$= $req$$.params.token,$id_app$$=$req$$.params.id_app;if(!global.mongoose.Types.ObjectId.isValid($id_app$$))return $res$$.status(400).send({error:"ID c\u00f4ng ty l\u00e0 kh\u00f4ng h\u1ee3p l\u1ec7"});App.find({voiip_access_token:$token$$,_id:$id_app$$},{_id:1,name:1}).lean().then(async $apps$$=>{async.map($apps$$,($app$$,$callback$$)=>{setImmediate(async()=>{let $callData$$={...$g_callData$$};$callData$$.id_app=$id_app$$;var $cus_phone$$=$callData$$.phone.replace("+",""),$contact_where$$={id_app:$id_app$$}; $cus_phone$$.length<6?$contact_where$$.dien_thoai=$cus_phone$$:($cus_phone$$.substring(0,1)==="0"&&($cus_phone$$=$cus_phone$$.substring(1)),$cus_phone$$.substring(0,2)==="84"&&($cus_phone$$=$cus_phone$$.substring(2)),$contact_where$$.$or=[{dien_thoai:{$regex:$cus_phone$$,$options:"i"}},{dien_thoai2:{$regex:$cus_phone$$,$options:"i"}},{ds_dien_thoai_khac:{$regex:$cus_phone$$,$options:"i"}}]);$cus_phone$$=await customer.findOne($contact_where$$).lean();$cus_phone$$||($contact_where$$=lienhe.findOne($contact_where$$).lean(), $contact_where$$.id_kh&&($cus_phone$$=await customer.findOne({_id:$contact_where$$.id_kh}).lean()));$cus_phone$$&&($callData$$.id_kh=$cus_phone$$._id,$callData$$.ma_kh=$cus_phone$$.ma_kh,$callData$$.ten_kh=$cus_phone$$.ten_kh,$callData$$.dia_chi=$cus_phone$$.dia_chi,$callData$$.dien_thoai=$cus_phone$$.dien_thoai,$callData$$.dien_thoai2=$cus_phone$$.dien_thoai2,typeof $cus_phone$$.ds_dien_thoai_khac=="string"&&($callData$$.ds_dien_thoai_khac=$cus_phone$$.ds_dien_thoai_khac));(new calllog($callData$$)).save(async($ctrl_e$$, $rs$$)=>{if($ctrl_e$$)return $callback$$($ctrl_e$$);$callData$$._id=$rs$$._id?.toString();if($callData$$.call_status==="Ring"){$ctrl_e$$=global.controllers.CALLLOG;let $users_will_receive$$;if($callData$$.extend){let $participant$$;$participant$$=await global.getModel("participant").find({id_app:$id_app$$,exts:$callData$$.extend},{email:1}).lean();$participant$$.length>0&&($users_will_receive$$=$participant$$.map($p$$=>$p$$.email))}$users_will_receive$$&&Controller.pushNotification($ctrl_e$$,$rs$$.toObject(), "new",void 0,void 0,{data:$callData$$,users_will_receive:$users_will_receive$$})}$callback$$(null,$callData$$)})})},($e$$,$log$$)=>{if($e$$)return $res$$.status(400).send($e$$);$res$$.send({result:"ok",log:$log$$})})}).catch($e$$=>{$res$$.status(400).send($e$$)})})};