flexbiz-server
Version:
Flexible Server
5 lines (4 loc) • 1.83 kB
JavaScript
'use strict';const model=global.getModel("lienhe"),customer=global.getModel("customer"),link=global.getModel("link"),async=require("async"),controller=require("../../controllers/controller"),lienhe=function($router$$){const $createLink$$=function($user$$,$obj$$,$next$$){if($obj$$.id_kh){var $_id$$=$obj$$._id.toString();link.find({$or:[{id_a:$_id$$,id_b:$obj$$.id_kh},{id_a:$obj$$.id_kh,id_b:$_id$$}]}).lean().exec(function($e$$,$rs$$){$rs$$&&0!==$rs$$.length?$next$$(null,$obj$$):(new link({id_app:$obj$$.id_app,
id_a:$obj$$.id_kh,id_b:$_id$$,collection_a:"dmkh",collection_b:"lienhe"})).save(function($e$$){$e$$&&console.error($e$$);$next$$(null,$obj$$)})})}else $next$$(null,$obj$$)};(new controller($router$$,model,"lienhe",{sort:{ten_lien_he:1},unique:["id_kh","ten_lien_he","email","dien_thoai"],onFinding:function($user$$,$condition$$,$next$$){async.series({find_ten_kh:function($callback$$){$condition$$.ten_kh?customer.find({id_app:$user$$.current_id_app,ten_kh:{$regex:$condition$$.ten_kh,$options:"i"}},{_id:1}).lean().exec(function($e$$,
$khs$$){if($e$$)return $callback$$($e$$);delete $condition$$.ten_kh;$e$$=$khs$$.map($k$$=>$k$$._id);$condition$$.id_kh={$in:$e$$};$callback$$()}):$callback$$()}},function($e$$){if($e$$)return $next$$($e$$);$next$$(null,$condition$$)})},onView:function($user$$,$items$$,$next$$){$items$$.joinModel2($user$$.current_id_app,customer,[{where:{id_kh:"_id"},fields:[{name:"ten_kh",value:"ten_kh"}]}],function(){$next$$(null,$items$$)})},onCreating:function($user$$,$item$$,$next$$,$extra$$){$extra$$.data&&$extra$$.data.ma_kh&&
!$item$$.id_kh?customer.findOne({ma_kh:$extra$$.data.ma_kh}).lean().exec(function($e$$,$c$$){$c$$&&($item$$.id_kh=$c$$._id);$next$$(null,$item$$)}):$next$$(null,$item$$)},onCreated:$createLink$$,onUpdated:$createLink$$})).route()};module.exports=lienhe;