UNPKG

flexbiz-server

Version:

Flexible Server

8 lines (7 loc) 3.38 kB
const model=global.getModel("contract"),controller=require("../../controllers/controller"),customer=global.getModel("customer"),User=global.getModel("user"),Trangthai=global.getModel("trangthai"),link=global.getModel("link"),async=require("async"),{onAfterCommit}=require("../../libs/sessionContext"); module.exports=function($router$$){const $createLink$$=function($user$$,$obj$$,$next$$){$obj$$.id_kh&&link.find({$or:[{id_a:$obj$$._id.toString(),id_b:$obj$$.id_kh},{id_a:$obj$$.id_kh,id_b:$obj$$._id.toString()}]}).lean().then(function($rs$$){$rs$$&&$rs$$.length!==0||(new link({id_app:$obj$$.id_app,id_a:$obj$$.id_kh,id_b:$obj$$._id.toString(),collection_a:"dmkh",collection_b:"contract"})).save(function($e$$){$e$$&&Logger.error($e$$)})}).catch($e$$=>{Logger.error($e$$)});$next$$&&$next$$(null,$obj$$)}, $createLinkAndCode$$=function($user$$,$obj$$,$next$$){$createLink$$($user$$,$obj$$);$obj$$.so_hd?$next$$(null,$obj$$):model.findByIdAndUpdate($obj$$._id,{$set:{so_hd:$obj$$._id.toString().toUpperCase()}},function($error$$,$kh$$){return $error$$?$next$$($error$$):$kh$$?($kh$$.so_hd=$obj$$._id.toString().toUpperCase(),$next$$(null,$kh$$)):$next$$(Error("H\u1ee3p \u0111\u1ed3ng kh\u00f4ng t\u1ed3n t\u1ea1i"))})};(new controller($router$$,model,"contract",{sort:{user_created:-1},unique:["so_hd"],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().then(function($khs$$){delete $condition$$.ten_kh;let $id_khs$$=[];$khs$$.forEach(function($kh$$){$id_khs$$.push($kh$$._id.toString())});$condition$$.id_kh={$in:$id_khs$$};$callback$$()}).catch($e$$=>{$callback$$($e$$)}):$callback$$()}},function($e$$){if($e$$)return $next$$($e$$);$next$$(null,$condition$$)})}, onView:async function($user$$,$items$$,$next$$){await $items$$.asyncJoinModel2($user$$.current_id_app,customer,[{where:{id_kh:"_id"},fields:[{name:"ten_kh",value:"ten_kh"},{name:"ma_kh",value:"ma_kh"}]}]);await $items$$.asyncJoinModel2(null,User,[{where:{phu_trach:"email"},fields:[{name:"ten_phu_trach",value:"name"}]}]);await $items$$.asyncJoinModel2(null,Trangthai,[{where:$item$$=>$item$$.progress===void 0?{}:{ma_trang_thai:$item$$.progress.toString(),ma_ct:"CONTRACT"},fields:["ten_trang_thai","color"]}]); $items$$.forEach($item$$=>{$item$$.ten_loai_hd=["H\u1ee3p \u0111\u1ed3ng mua","H\u1ee3p \u0111\u1ed3ng b\u00e1n"][$item$$.loai_hd-1]});$next$$(null,$items$$)},onCreating:function($user$$,$item$$,$next$$,$extra$$){$item$$.phu_trach||($item$$.phu_trach=$user$$.email);if($item$$.id_kh)$next$$(null,$item$$);else if($extra$$.data&&$extra$$.data.ma_kh)customer.findOne({ma_kh:$extra$$.data.ma_kh}).lean().then(function($c$$){if($c$$)$item$$.id_kh=$c$$._id;else return $next$$("M\u00e3 kh\u00e1ch h\u00e0ng '"+ $extra$$.data.ma_kh+"' kh\u00f4ng t\u1ed3n t\u1ea1i");$next$$(null,$item$$)}).catch($e$$=>{$next$$($e$$)});else return $next$$("H\u1ee3p \u0111\u1ed3ng y\u00eau c\u1ea7u m\u1ed9t kh\u00e1ch h\u00e0ng")},onCreated:($user$$,$obj$$,$next$$)=>{onAfterCommit(()=>{$createLinkAndCode$$($user$$,$obj$$,$e$$=>{$e$$&&Logger.error($e$$)})});$next$$(null,$obj$$)},onUpdated:($user$$,$obj$$,$next$$)=>{onAfterCommit(()=>{$createLinkAndCode$$($user$$,$obj$$,$e$$=>{$e$$&&Logger.error($e$$)})});$next$$(null,$obj$$)}})).route()};