flexbiz-server
Version:
Flexible Server
7 lines (6 loc) • 2.45 kB
JavaScript
'use strict';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;delete $g_callData$$.status;if(!$g_callData$$.phone)return $res$$.status(400).send({errors:"Phone number is required"});App.find({voiip_access_token:$req$$.params.token},
{_id:1,name:1}).lean().exec(async($e$$,$apps$$)=>{if($e$$)return $res$$.status(400).send($e$$);async.map($apps$$,($app$$,$callback$$)=>{setImmediate(async()=>{let $callData$$={...$g_callData$$},$id_app$$=$app$$._id.toString();$callData$$.id_app=$id_app$$;var $cus_phone$$=$callData$$.phone.replace("+",""),$contact_where$$={id_app:$id_app$$};6>$cus_phone$$.length?$contact_where$$.dien_thoai=$cus_phone$$:("0"===$cus_phone$$.substring(0,1)&&($cus_phone$$=$cus_phone$$.substring(1)),"84"===$cus_phone$$.substring(0,
2)&&($cus_phone$$=$cus_phone$$.substring(2)),$contact_where$$.dien_thoai={$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$$.ten_kh=$cus_phone$$.ten_kh,$callData$$.dia_chi=$cus_phone$$.dia_chi);(new calllog($callData$$)).save(async($ctrl_e$$,
$rs$$)=>{if($ctrl_e$$)return $callback$$($ctrl_e$$);if("Ring"===$callData$$.call_status){$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();0<$participant$$.length&&($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$$()})})},$e$$=>{if($e$$)return $res$$.status(400).send($e$$);$res$$.send({result:"ok"})})})})};