UNPKG

flexbiz-server

Version:

Flexible Server

13 lines (12 loc) 6.04 kB
'use strict';const async=require("async"),_=require("lodash"),redisCache=require("../libs/redis-cache"),view=async($user$$,$self$$,$rows$$,$next$$,$options$$)=>{const $model$$=$self$$.model;let $fields_load$$="";$options$$&&$options$$.req&&$options$$.req.query&&$options$$.req.query.fields&&($fields_load$$=$options$$.req.query.fields);let $schema_paths$$=$model$$.schema.paths;for(let $index$$=0;$index$$<$rows$$.length;$index$$++){const $item$$=$rows$$[$index$$];delete $item$$.__v;$item$$.stt_sorted= $index$$;delete $item$$.from_cached;_.has($schema_paths$$,"exfields")&&!$item$$.exfields&&($item$$.exfields={})}let $items$$=[];$options$$&&$options$$.req&&$options$$.req.query_cache&&!$options$$.req.query.refresh_required?await new Promise($resolve$$=>{setImmediate(async()=>{let $cacheData$$=await redisCache.getCacheReport($options$$.req.user.email,($options$$.req.user.current_id_app||"system").toString(),$self$$.name,$options$$.req.query_cache,0,$options$$.req.query.page);if($cacheData$$)for(let $index$$= 0;$index$$<$rows$$.length;$index$$++){const $item$$=$rows$$[$index$$];let $cacheds$$=$cacheData$$.filter($r$$=>$r$$._id===$item$$._id.toString());if(0<$cacheds$$.length)for(let $cached$$ of $cacheds$$)$cached$$.from_cached=!0,$items$$.push($cached$$);else $items$$.push($item$$)}else $items$$=$rows$$;$resolve$$()})}):$items$$=$rows$$;async.parallel({_Participant_created:$callback$$=>{setImmediate(async()=>{_.has($schema_paths$$,"id_app")&&await $items$$.filter($item$$=>$item$$.user_created&&!$item$$.from_cached).asyncJoinModel2($user$$.current_id_app, "participant",{where:{user_created:"email"},fields:{name_user_created:"name"}});await $items$$.filter($item$$=>$item$$.user_created&&!$item$$.name_user_created&&!$item$$.from_cached).asyncJoinModel2(null,"user",{where:{user_created:"email"},fields:{name_user_created:"name"}});$callback$$(null)})},_Participant_updated:$callback$$=>{setImmediate(async()=>{_.has($schema_paths$$,"id_app")&&await $items$$.filter($item$$=>$item$$.user_updated&&!$item$$.from_cached).asyncJoinModel2($user$$.current_id_app, global.getModel("participant"),{where:{user_updated:"email"},fields:{name_user_updated:"name"}});await $items$$.filter($item$$=>$item$$.user_updated&&!$item$$.name_user_updated&&!$item$$.from_cached).asyncJoinModel2(null,global.getModel("user"),{where:{user_updated:"email"},fields:{name_user_updated:"name"}});$callback$$(null)})},_approve:$callback$$=>{!$fields_load$$||0<=$fields_load$$.indexOf("approve_data")?async.map($items$$,($item$$,$next$$)=>{setImmediate(()=>{global.getModel("approve").find({id_ct:$item$$._id.toString()}, {title:1,trang_thai:1,user_approved:1,user_approved_name:1,date_approved:1,user_denied:1,user_denied_name:1,date_denied:1}).lean().exec(async($e$$,$rs$$)=>{setImmediate(async()=>{$item$$.approve_data=$rs$$||[];await $item$$.approve_data.filter($r$$=>$r$$.user_approved).asyncJoinModel2($item$$.id_app,global.getModel("participant"),{where:{user_approved:"email"},fields:{user_approved_name:"name",signature_image:"signature_image"}});await $item$$.approve_data.filter($r$$=>$r$$.user_denied).asyncJoinModel2($item$$.id_app, global.getModel("participant"),{where:{user_denied:"email"},fields:{user_denied_name:"name",signature_image:"signature_image"}});$next$$(null)})})})},()=>{$callback$$(null)}):$callback$$(null)},transfer_ct:$callback$$=>{if(_.has($schema_paths$$,"id_ct_chuyen")&&$user$$.current_app_info&&$user$$.current_app_info.options&&$user$$.current_app_info.options.id_app_thue)async.map($items$$,($item$$,$next$$)=>{setImmediate(()=>{$model$$.findOne({id_app:$user$$.current_app_info.options.id_app_thue,id_ct_chuyen:$item$$._id.toString()}, {_id:1}).lean().exec(async($e$$,$rs$$)=>{$rs$$&&($item$$.id_ct_nhan=$rs$$._id);$next$$(null)})})},()=>{$callback$$(null)});else return $callback$$()},posted:$callback$$=>{$options$$&&$options$$.req&&($options$$.req.query.show_posted_book||$options$$.req.query.show_not_post_book)?async.map($items$$,($item$$,$next$$)=>{setImmediate(async()=>{let $book$$=$options$$.req.query.show_posted_book||$options$$.req.query.show_not_post_book,$exists$$=await global.getModel($book$$).findOne({id_ct:$item$$._id.toString()}, {_id:1});$item$$[`${$book$$}_posted`]=!!$exists$$;$next$$()})},()=>{$callback$$(null)}):$callback$$()}},$e$jscomp$0$$=>{if($e$jscomp$0$$)return $next$$($e$jscomp$0$$);if($options$$&&$options$$.req&&($options$$.req.query.show_posted_book||$options$$.req.query.show_not_post_book)){let $book$$=$options$$.req.query.show_posted_book||$options$$.req.query.show_not_post_book;$items$$=$items$$.filter($r$$=>$options$$.req.query.show_posted_book?$r$$[`${$book$$}_posted`]:!$r$$[`${$book$$}_posted`])}let $items_need_view$$= $items$$.filter($i$$=>!$i$$.from_cached),$items_cached$$=$items$$.filter($i$$=>$i$$.from_cached);async.series({view:$callback$$=>{if($self$$.view)try{$self$$.view($user$$,$items_need_view$$,function($error$$,$rs$$){setImmediate(()=>{if($error$$)return $callback$$($error$$);$items_need_view$$=$rs$$||$items_need_view$$;$callback$$(null,$items_need_view$$)})},$options$$)}catch($e$$){console.error("Error on view of",name,$e$$),$callback$$(null,$items_need_view$$)}else $callback$$(null,$items_need_view$$)}, dynamicView:$callback$$=>{$self$$.dynamicView?$self$$.dynamicView($user$$,$items_need_view$$,function($error$$,$rs$$){if($error$$)return $callback$$($error$$);$items_need_view$$=$rs$$||$items_need_view$$;$callback$$(null,$items_need_view$$)}):$callback$$(null,$items_need_view$$)},viewRequired:$callback$$=>{$items$$=[...$items_cached$$,...$items_need_view$$].sort(($a$$,$b$$)=>$a$$.stt_sorted-$b$$.stt_sorted);if($self$$.viewRequired)try{$self$$.viewRequired($user$$,$items$$,function($error$$,$rs$$){if($error$$)return $callback$$($error$$); $items$$=$rs$$||$items$$;$callback$$(null,$items$$)},$options$$)}catch($e$$){console.error("Error on viewRequired of",name,$e$$),$callback$$(null,$items$$)}else $callback$$(null,$items$$)}},$error$$=>{if($error$$)return $next$$($error$$);$next$$(null,$items$$)})})};module.exports=view;