flexbiz-server 12.1.28 → 12.1.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server/controllers/controller.js +21 -20
- package/server/global.js +1 -1
package/package.json
CHANGED
|
@@ -77,19 +77,19 @@ if(!$req$$.permission)return $res$$.send([]);$next$$()});else{if(!$permission$$)
|
|
|
77
77
|
$condition$$.id_app&&$condition$$.$and.push({id_app:$condition$$.id_app})}else underscore.has($model$$.schema.paths,"id_app")&&!1!==$ctrl$$.require_id_app&&($condition$$.id_app=$req$$.user.current_id_app);"public"===$req$$.user.email&&underscore.has($model$$.schema.paths,"session_created")&&($condition$$.session_created=($req$$.cookies||{}).uid);underscore.extend($condition$$,$ctrl$$.keyValues);$condition$$.$and&&0===$condition$$.$and.length&&delete $condition$$.$and;$condition$$.$or&&0===$condition$$.$or.length&&
|
|
78
78
|
delete $condition$$.$or;$condition$$.$nor&&0===$condition$$.$nor.length&&delete $condition$$.$nor;$id_sellers_query$$=null;if($req$$.query.count)$condition$$.$where?(console.log("phai loai bo $where trong condition"),$id_sellers_query$$=$model$$.count($condition$$,function($e$$,$data$$){if($e$$)return console.error($e$$),$e$$.error?$res$$.status(400).send($e$$):$res$$.status(400).send({error:$e$$.message||$e$$});$res$$.send({rows_number:$data$$})})):$id_sellers_query$$=$model$$.countDocuments($condition$$,
|
|
79
79
|
function($e$$,$data$$){if($e$$)return console.error($e$$),$e$$.error?$res$$.status(400).send($e$$):$res$$.status(400).send({error:$e$$.message||$e$$});$res$$.send({rows_number:$data$$})});else{let $field_not_right$$;try{$field_not_right$$=await permission.getFieldNotRight($req$$.user.current_id_app,$req$$.user.email,$ctrl$$.name)}catch($e$$){$field_not_right$$=[]}var $fields$$=$req$$.query.fields;let $gfields$$={};$fields$$&&(underscore.isArray($fields$$)?$fields$$.forEach($f$$=>{"_id"!==$f$$&&($gfields$$[$f$$]=
|
|
80
|
-
1)}):underscore.isObject($fields$$)?$gfields$$={...$gfields$$,...$fields$$}:$fields$$.split(",").filter($f$$=>$f$$&&0>$field_not_right$$.indexOf($f$$)).forEach($f$$=>{"_id"!==$f$$&&($gfields$$[$f$$]=1)}));$field_not_right$$.forEach($f$$=>{$gfields$$[$f$$]=0});!$fields$$&&(($fields$$=$req$$.query.notfields)&&(underscore.isArray($fields$$)?$fields$$.forEach($f$$=>{$f$$&&"_id"!==$f$$&&(
|
|
81
|
-
{$f$$&&"_id"!==$f$$&&($gfields$$[$f$$]=0)})),$shareRoute$$||"public"===$req$$.user.email)&&($gfields
|
|
82
|
-
|
|
83
|
-
$id_sellers_query$$.
|
|
84
|
-
$
|
|
85
|
-
($
|
|
86
|
-
$obj4view$$)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
"File m\u1eabu excel ch\u01b0a \u0111\u01b0\u1ee3c khai b\u00e1o t\u1ea1i '"+$_rs_msg_templatePath$$+"'",$res$$.status(400).send({error:$_rs_msg_templatePath$$});$app$jscomp$1_dir_root_template_e$$={};$_groupBy_func_string_rs$$.e_view.forEach($item$$=>{for(let $_key$$ in $item$$){let $_v
|
|
91
|
-
new Date($__v$$)}catch($e$$){$item$$[`${$_key$$}_${$__key$$}`]=$__v$$}else $item$$[`${$_key$$}_${$__key$$}`]=$__v$$}else if($_v$$&&0<=$_key$$.indexOf("ngay")||0<=$_key$$.indexOf("date"))try{let $d$$=moment($_v$$,"YYYY-MM-DD");$d$$.isValid()&&($item$$[`${$_key$$}`]=$d$$.toDate()
|
|
92
|
-
`return (async ()=>{
|
|
80
|
+
1)}):underscore.isObject($fields$$)?$gfields$$={...$gfields$$,...$fields$$}:$fields$$.split(",").filter($f$$=>$f$$&&0>$field_not_right$$.indexOf($f$$)).forEach($f$$=>{"_id"!==$f$$&&($gfields$$[$f$$]=1)}));$field_not_right$$.forEach($f$$=>{Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])?delete $gfields$$[$f$$]:$gfields$$[$f$$]=0});!$fields$$&&(($fields$$=$req$$.query.notfields)&&(underscore.isArray($fields$$)?$fields$$.forEach($f$$=>{$f$$&&"_id"!==$f$$&&(Object.keys($gfields$$).find($k$$=>
|
|
81
|
+
1==$gfields$$[$k$$])?delete $gfields$$[$f$$]:$gfields$$[$f$$]=0)}):underscore.isObject($fields$$)?$gfields$$={...$gfields$$,...$fields$$}:$fields$$.split(",").forEach($f$$=>{$f$$&&"_id"!==$f$$&&(Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])?delete $gfields$$[$f$$]:$gfields$$[$f$$]=0)})),$shareRoute$$||"public"===$req$$.user.email)&&(Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])?(delete $gfields$$.dataview_to,delete $gfields$$.dataview_to_users):($gfields$$.dataview_to=0,$gfields$$.dataview_to_users=
|
|
82
|
+
0));$condition$$.$text&&($gfields$$||($gfields$$={}),$gfields$$.textScore={$meta:"textScore"});if($req$$.query.group||$req$$.query.project||$req$$.query.match||$req$$.query.aggregate)try{$id_sellers_query$$=$model$$.aggregate([{$match:$condition$$}]);if($req$$.query.group){var $group_page$$=JSONParser($req$$.query.group);$id_sellers_query$$.group($group_page$$)}if($req$$.query.match){let $match$$=JSONParser($req$$.query.match);$id_sellers_query$$.match($match$$)}if($req$$.query.project){var $limit_project$$=
|
|
83
|
+
JSONParser($req$$.query.project);$id_sellers_query$$.project($limit_project$$)}else if($gfields$$&&0<Object.keys($gfields$$).length)$limit_project$$=$gfields$$;else if($limit_project$$={_id:1},$group_page$$)for(let $key$$ in $group_page$$)$limit_project$$[$key$$]=1;$id_sellers_query$$.project($limit_project$$);if($req$$.query.aggregate){let $aggregate$$=JSONParser($req$$.query.aggregate);$id_sellers_query$$.append($aggregate$$)}}catch($e$$){return $res$$.status(400).send({error:$e$$.message||$e$$.error||
|
|
84
|
+
$e$$})}else underscore.has($model$$.schema.paths,"id_app")&&0<Object.keys($gfields$$).length&&Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])&&($gfields$$.id_app=1),$id_sellers_query$$=$model$$.find($condition$$,$gfields$$);!$sort$$&&$gfields$$.textScore?$sort$$={textScore:{$meta:"textScore"}}:$sort$$&&($sort$$._id=1);$sort$$&&$id_sellers_query$$.sort($sort$$);"xlsx"!==$req$$.query.type_data&&"1"!=$req$$.query.rpt&&($group_page$$=$req$$.query.page,($limit_project$$=$req$$.query.limit)&&($limit_project$$=
|
|
85
|
+
Number($limit_project$$)),$limit_project$$||($limit_project$$=!1!==$ctrl$$.require_id_app&&"right"!==$ctrl$$.name?20:1E5),$limit_project$$&&500<$limit_project$$&&!$req$$.query.print&&!1!==$ctrl$$.require_id_app&&"right"!==$ctrl$$.name&&($limit_project$$=500),$group_page$$?$id_sellers_query$$.skip((Number($group_page$$)-1)*$limit_project$$).limit($limit_project$$):$id_sellers_query$$.limit($limit_project$$));$id_sellers_query$$.lean&&($id_sellers_query$$=$id_sellers_query$$.lean());$id_sellers_query$$.exec(function($e$jscomp$0$$,
|
|
86
|
+
$obj4view$$){if($e$jscomp$0$$)return console.error($e$jscomp$0$$,$condition$$,$gfields$$),$res$$.status(400).send({error:$e$jscomp$0$$.error||$e$jscomp$0$$.message||$e$jscomp$0$$});$obj4view$$||($obj4view$$=[]);async.series({found:function($callback$$){$ctrl$$.found?$ctrl$$.found($req$$.user,$condition$$,$obj4view$$,function($error$$,$items$$){setImmediate(()=>{$callback$$($error$$,$items$$)})}):$callback$$(null,$obj4view$$)},set_collection_name:function($callback$$){$obj4view$$.forEach($obj$$=>{$obj$$.collection_name||
|
|
87
|
+
($obj$$.collection_name=$ctrl$$.collection_name)});$callback$$(null)}},function($error$jscomp$0$$,$rs$jscomp$0$$){setImmediate(()=>{if($error$jscomp$0$$)return $res$$.status(400).send({error:$error$jscomp$0$$.error||$error$jscomp$0$$.message||$error$jscomp$0$$});$obj4view$$=$rs$jscomp$0$$.found;async.series({e_view:function($callback$$){$ctrl$$.onView($req$$.user,$obj4view$$,($error$$,$viewValue$$)=>{setImmediate(()=>{if($error$$)return console.error("Erron on view:",$error$$),$callback$$($error$$);
|
|
88
|
+
$callback$$(null,$viewValue$$)})},{req:$req$$,shareRoute:$shareRoute$$})}},async function($app$jscomp$1_dir_root_template_e$$,$_groupBy_func_string_rs$$){if($error$jscomp$0$$)return $res$$.status(400).send({error:$error$jscomp$0$$.error||$error$jscomp$0$$.message||$error$jscomp$0$$});if($req$$.query.type_data&&"xlsx"==$req$$.query.type_data){$app$jscomp$1_dir_root_template_e$$=(configs.paths||{}).templates;let $template_id$$,$excelTemplate$$;if($template_id$$=$req$$.query.id_template){if(!global.mongoose.Types.ObjectId.isValid($template_id$$))return $res$$.status(400).send({error:"M\u1eabu excel kh\u00f4ng t\u1ed3n t\u1ea1i"});
|
|
89
|
+
$excelTemplate$$=await global.getModel("exportexceltemplate").findById($template_id$$);if(!$excelTemplate$$||!$excelTemplate$$.file)return $res$$.status(400).send({error:"M\u1eabu excel kh\u00f4ng t\u1ed3n t\u1ea1i"});var $_rs_msg_templatePath$$=$app$jscomp$1_dir_root_template_e$$?`${path.dirname($app$jscomp$1_dir_root_template_e$$)}${$excelTemplate$$.file}`:`${path.dirname(__dirname)}${$excelTemplate$$.file}`}else $app$jscomp$1_dir_root_template_e$$&&($_rs_msg_templatePath$$=`${$app$jscomp$1_dir_root_template_e$$}/excels/lists/${$ctrl$$.name}.xlsx`),
|
|
90
|
+
$_rs_msg_templatePath$$&&fs.existsSync($_rs_msg_templatePath$$)||($_rs_msg_templatePath$$=`${path.dirname(__dirname)}/templates/excels/lists/${$ctrl$$.name}.xlsx`);if(!fs.existsSync($_rs_msg_templatePath$$))return $_rs_msg_templatePath$$="File m\u1eabu excel ch\u01b0a \u0111\u01b0\u1ee3c khai b\u00e1o t\u1ea1i '"+$_rs_msg_templatePath$$+"'",$res$$.status(400).send({error:$_rs_msg_templatePath$$});$app$jscomp$1_dir_root_template_e$$={};$_groupBy_func_string_rs$$.e_view.forEach($item$$=>{for(let $_key$$ in $item$$){let $_v$$=
|
|
91
|
+
$item$$[$_key$$];if(underscore.isObject($_v$$))for(let $__key$$ in $_v$$){let $__v$$=$_v$$[$__key$$];if(!underscore.isFunction($__v$$))if(0<=$__key$$.indexOf("ngay")||0<=$__key$$.indexOf("date"))try{$item$$[`${$_key$$}_${$__key$$}`]=new Date($__v$$)}catch($e$$){$item$$[`${$_key$$}_${$__key$$}`]=$__v$$}else $item$$[`${$_key$$}_${$__key$$}`]=$__v$$}else if($_v$$&&0<=$_key$$.indexOf("ngay")||0<=$_key$$.indexOf("date"))try{let $d$$=moment($_v$$,"YYYY-MM-DD");$d$$.isValid()&&($item$$[`${$_key$$}`]=$d$$.toDate(),
|
|
92
|
+
$item$$[`${$_key$$}_string`]=$d$$.format("DD/MM/YYYY"))}catch($e$$){console.error("Error convert to date",$_v$$,$e$$)}}});$app$jscomp$1_dir_root_template_e$$.detail=$_groupBy_func_string_rs$$.e_view;if($excelTemplate$$&&$excelTemplate$$.handle_data){$_groupBy_func_string_rs$$=`return (async ()=>{
|
|
93
93
|
try{
|
|
94
94
|
${$excelTemplate$$.handle_data}
|
|
95
95
|
}catch(e){
|
|
@@ -103,13 +103,14 @@ $_groupBy_func_string_rs$$),$res$$.status(400).send({error:$e$$.message||$e$$.er
|
|
|
103
103
|
"/excel").get(...$findRoute$$)):(this.router.route($_route_name_route_name$$).post(...$findRoute$$),this.router.route($_route_name_route_name$$+"/excel").post(...$findRoute$$))};
|
|
104
104
|
controller.prototype.get=function($route_name$$,$shareRoute$$=!1){let $model$$=this.model,$ctrl$$=this;this.router.route(`${$route_name$$||this.route_name}/:id`).get(function($req$$,$res$$,$next$$){let $id$$=$req$$.params.id;if(!global.mongoose.Types.ObjectId.isValid($id$$))return $res$$.status(404).send({error:ERR_ID_INVALID,code:ERR_ID_INVALID_CODE});async.series({c:$callback$$=>{global.clientRedis.get("stp:"+$id$$,async($err_fields$jscomp$2_notfields$$,$obj$jscomp$0$$)=>{if(!$err_fields$jscomp$2_notfields$$&&
|
|
105
105
|
$obj$jscomp$0$$&&$obj$jscomp$0$$._id===$id$$)$callback$$(null,JSONParser($obj$jscomp$0$$));else{let $field_not_right$$;try{$field_not_right$$=await permission.getFieldNotRight($req$$.user.current_id_app,$req$$.user.email,$ctrl$$.name)}catch($e$$){$field_not_right$$=[]}$err_fields$jscomp$2_notfields$$=$req$$.query.fields;let $gfields$$={};$err_fields$jscomp$2_notfields$$&&(underscore.isArray($err_fields$jscomp$2_notfields$$)?$err_fields$jscomp$2_notfields$$.filter($f$$=>0>$field_not_right$$.indexOf($f$$)).forEach($f$$=>
|
|
106
|
-
{"_id"!=$f$$&&$f$$&&($gfields$$[$f$$]=1)}):underscore.isObject($err_fields$jscomp$2_notfields$$)?$gfields$$={...$gfields$$,...$err_fields$jscomp$2_notfields$$}:$err_fields$jscomp$2_notfields$$.split(",").filter($f$$=>0>$field_not_right$$.indexOf($f$$)).forEach($f$$=>{"_id"!=$f$$&&$f$$&&($gfields$$[$f$$]=1)}));if(!$err_fields$jscomp$2_notfields$$)try{$field_not_right$$.forEach($f$$=>{$f$$&&($gfields$$[$f$$]=0)})}catch($e$$){console.error("Error get field not right"
|
|
107
|
-
(($err_fields$jscomp$2_notfields$$=$req$$.query.notfields)&&(underscore.isArray($err_fields$jscomp$2_notfields$$)?$err_fields$jscomp$2_notfields$$.forEach($f$$=>{"_id"!=$f$$&&$f$$&&($gfields$$[$f$$]=0)}):underscore.isObject($err_fields$jscomp$2_notfields$$)?$gfields$$={...$gfields$$,...$err_fields$jscomp$2_notfields$$}:$err_fields$jscomp$2_notfields$$.split(",").forEach($f$$=>{"_id"
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
$res$$.status(
|
|
111
|
-
$
|
|
112
|
-
|
|
106
|
+
{"_id"!=$f$$&&$f$$&&($gfields$$[$f$$]=1)}):underscore.isObject($err_fields$jscomp$2_notfields$$)?$gfields$$={...$gfields$$,...$err_fields$jscomp$2_notfields$$}:$err_fields$jscomp$2_notfields$$.split(",").filter($f$$=>0>$field_not_right$$.indexOf($f$$)).forEach($f$$=>{"_id"!=$f$$&&$f$$&&($gfields$$[$f$$]=1)}));if(!$err_fields$jscomp$2_notfields$$)try{$field_not_right$$.forEach($f$$=>{$f$$&&(Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])?delete $gfields$$[$f$$]:$gfields$$[$f$$]=0)})}catch($e$$){console.error("Error get field not right",
|
|
107
|
+
$e$$)}!$err_fields$jscomp$2_notfields$$&&(($err_fields$jscomp$2_notfields$$=$req$$.query.notfields)&&(underscore.isArray($err_fields$jscomp$2_notfields$$)?$err_fields$jscomp$2_notfields$$.forEach($f$$=>{"_id"!=$f$$&&$f$$&&(Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])?delete $gfields$$[$f$$]:$gfields$$[$f$$]=0)}):underscore.isObject($err_fields$jscomp$2_notfields$$)?$gfields$$={...$gfields$$,...$err_fields$jscomp$2_notfields$$}:$err_fields$jscomp$2_notfields$$.split(",").forEach($f$$=>{"_id"!=
|
|
108
|
+
$f$$&&$f$$&&(Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])?delete $gfields$$[$f$$]:$gfields$$[$f$$]=0)})),$shareRoute$$||"public"===$req$$.user.email)&&(Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])?(delete $gfields$$.dataview_to,delete $gfields$$.dataview_to_users):($gfields$$.dataview_to=0,$gfields$$.dataview_to_users=0));underscore.has($model$$.schema.paths,"id_app")&&0<Object.keys($gfields$$).length&&Object.keys($gfields$$).find($k$$=>1==$gfields$$[$k$$])&&($gfields$$.id_app=
|
|
109
|
+
1);$model$$.findById($id$$,$gfields$$,function($err$$,$obj$$){setImmediate(()=>{$callback$$($err$$,$obj$$)})})}})}},async($c_app_e$$,$rs$$)=>{if($c_app_e$$)return console.error($c_app_e$$),$c_app_e$$.error?$res$$.status(400).send($c_app_e$$):$res$$.status(400).send({error:$c_app_e$$.message||$c_app_e$$});let $obj$$=$rs$$.c;if(!$obj$$)return $res$$.status(404).send({error:ERR_NOT_FOUND,code:ERR_NOT_FOUND_CODE});if($shareRoute$$&&$obj$$.shared)underscore.has($model$$.schema.paths,"id_app")&&(($c_app_e$$=
|
|
110
|
+
await app.findById($obj$$.id_app))&&!0===$c_app_e$$.sale_online||$res$$.status(403).send({error:ERR_NOT_PERMIT,code:ERR_NOT_PERMIT_CODE}));else if(!0===underscore.has($model$$.schema.paths,"id_app")&&$req$$.user.current_id_app!=$obj$$.id_app)return $res$$.status(403).send({error:ERR_NOT_PERMIT,code:ERR_NOT_PERMIT_CODE});$ctrl$$.getting?$ctrl$$.getting($req$$.user,$id$$,function($e$$){setImmediate(()=>{if($e$$)return console.error($e$$),$res$$.status(400).send({error:$e$$.message||$e$$.error||$e$$});
|
|
111
|
+
$req$$.obj=$obj$$;$next$$()})},$obj$$):($req$$.obj=$obj$$,$next$$())})},function($req$$,$res$$,$next$$){let $obj$$=$req$$.obj;if($obj$$&&$obj$$.toObject)$obj$$=$obj$$.toObject();else return $next$$();if($obj$$.shared&&$shareRoute$$)return $next$$();permission.hasRight($obj$$.id_app,$req$$.user.email,$ctrl$$.module,"view",function($error$$,$permission$$){setImmediate(()=>{if($ctrl$$.options.onQuery)$req$$.permission=$permission$$,$ctrl$$.options.onQuery($req$$,$e$$=>{setImmediate(()=>{if($e$$)return $res$$.status(403).send({error:$e$$||
|
|
112
|
+
ERR_NOT_PERMIT,code:ERR_NOT_PERMIT_CODE});if($req$$.permission)$next$$();else return $res$$.status(403).send({error:$error$$||ERR_NOT_PERMIT,code:ERR_NOT_PERMIT_CODE})})});else if($permission$$)$next$$();else return $res$$.status(403).send({error:$error$$||ERR_NOT_PERMIT,code:ERR_NOT_PERMIT_CODE})})},{data:$obj$$,obj:$obj$$,notNeedRight:$ctrl$$.options.notNeedRight})},function($req$$,$res$$){let $obj$$=$req$$.obj;$obj$$&&$obj$$.toObject&&($obj$$=$obj$$.toObject());$obj$$.collection_name=$ctrl$$.collection_name;
|
|
113
|
+
$ctrl$$.onView($req$$.user,[$obj$$],function($e$$,$viewValue$$){setImmediate(()=>{if($e$$)return console.error($e$$),$res$$.status(400).send({error:$e$$.message||$e$$.error||$e$$});$res$$.send($viewValue$$[0])})},{req:$req$$,shareRoute:$shareRoute$$})})};
|
|
113
114
|
controller.prototype.postAgain=function(){let $model$$=this.model,$ctrl$$=this;this.router.route(`${this.route_name}/action/postagain`).get(function($req$$,$res$$,$next$$){console.log("reposting...",$ctrl$$.name,$req$$.query);if(!$ctrl$$.post)return $res$$.send({message:"Ch\u01b0\u01a1ng tr\u00ecnh \u0111\u00e3 th\u1ef1c hi\u1ec7n xong"});let $condition$jscomp$1$$={};for(let $k$$ in $req$$.query)if("id_app"!=$k$$&&"access_token"!=$k$$)if("_id"==$k$$&&global.mongoose.Types.ObjectId.isValid($req$$.query._id))try{let $id$$=
|
|
114
115
|
global.mongoose.Types.ObjectId($req$$.query._id);$condition$jscomp$1$$._id=$id$$}catch($error$$){console.error($error$$)}else{if("q"==$k$$&&$req$$.query[$k$$])try{$condition$jscomp$1$$=JSONParser($req$$.query[$k$$]);continue}catch($e$$){return console.error($e$$),$res$$.status(400).send($e$$)}if("tu_ngay"==$k$$&&!0===underscore.has($model$$.schema.paths,"ngay_ct")){var $val_val$$=moment($req$$.query.tu_ngay).startOf("date").toDate();$condition$jscomp$1$$.ngay_ct?$condition$jscomp$1$$.ngay_ct.$gte=
|
|
115
116
|
$val_val$$:$condition$jscomp$1$$.ngay_ct={$gte:$val_val$$}}else"den_ngay"==$k$$&&!0===underscore.has($model$$.schema.paths,"ngay_ct")?($val_val$$=moment($req$$.query.den_ngay).endOf("date").toDate(),$condition$jscomp$1$$.ngay_ct?$condition$jscomp$1$$.ngay_ct.$lte=$val_val$$:$condition$jscomp$1$$.ngay_ct={$lte:$val_val$$}):!0===underscore.has($model$$.schema.paths,$k$$)&&($condition$jscomp$1$$[$k$$]=$req$$.query[$k$$])}!0===underscore.has($model$$.schema.paths,"id_app")&&($condition$jscomp$1$$.id_app=
|
package/server/global.js
CHANGED
|
@@ -14,6 +14,6 @@ global.alertNotification=function($email$$){setImmediate(()=>{Notification.count
|
|
|
14
14
|
global.asyncCountNotifications=async function($email$$){return new Promise($resolve$$=>{setImmediate(()=>{Notification.countDocuments({email_receiver:$email$$,email_owner:$email$$,read:!1},function($error$$,$count$$){setImmediate(()=>{$error$$?(console.log($error$$),$resolve$$(0)):$resolve$$($count$$)})})})})};const {RateLimiterRedis,RateLimiterQueue,RateLimiterMemory}=require("rate-limiter-flexible");
|
|
15
15
|
global.createRateLimiterMiddleware=async($config$$,$keyPrefix$$)=>{const $total_points$$=$config$$.points||1E3;var $duration_limiterFlexible$$=$config$$.duration||1;const $insuranceLimiter$$=new RateLimiterMemory({points:$total_points$$,duration:$duration_limiterFlexible$$});$duration_limiterFlexible$$=new RateLimiterRedis({keyPrefix:$keyPrefix$$,storeClient:global.clientRedis,points:$total_points$$,duration:$duration_limiterFlexible$$,blockDuration:$config$$.blockDuration||0,inmemoryBlockOnConsumed:$total_points$$+
|
|
16
16
|
1,inmemoryBlockDuration:10,insuranceLimiter:$insuranceLimiter$$});const $rateLimiter$$=new RateLimiterQueue($duration_limiterFlexible$$,{maxQueueSize:$config$$.maxQueueSize||1E4});return async($req$$,$res$$,$next$$)=>{let $key$$=($req$$.ip||$req$$.headers["x-forwarded-for"]||$req$$.connection.remoteAddress).replace("::ffff:","");const $originalUrl$$=$req$$.originalUrl,$origin$$=($req$$.headers.origin||$req$$.headers.referer||"").toString().toLowerCase().replace("https://","").replace("http://","").replace("www.",
|
|
17
|
-
"").split("/").join("").trim(),$trust_domains$$=((global.configs||{}).trust_domains||"").toString().toLowerCase(),$trust_Key$$=$req$$.headers["x-trust-key"]||$req$$.query.trustkey||$req$$.query.trust_key||"",$trust_keys$$=(global.configs||{}).trust_keys||[];$req$$.user&&($key$$=`${$key$$}-${$req$$.user.email}`);$key$$=`${$key$$}-${$origin$$||"unknow"}-${$trust_Key$$||"untrustkey"}`;let $points$$;!$trust_domains$$&&0==$trust_keys$$.length||$origin$$&&$trust_domains$$&&0<=$trust_domains$$.indexOf($origin$$)||
|
|
17
|
+
"").split("/").join("").trim(),$trust_domains$$=((global.configs||{}).trust_domains||"").toString().toLowerCase(),$trust_Key$$=$req$$.headers["x-trust-key"]||$req$$.query.trustkey||$req$$.query.trust_key||"",$trust_keys$$=(global.configs||{}).trust_keys||[];$req$$.user&&($key$$=`${$key$$}-${$req$$.user.email}`);$key$$=`${$key$$}-${$origin$$||"unknow"}-${$trust_Key$$||"untrustkey"}`;let $points$$;!$origin$$||!$trust_domains$$&&0==$trust_keys$$.length||$origin$$&&$trust_domains$$&&0<=$trust_domains$$.indexOf($origin$$)||
|
|
18
18
|
0<$trust_keys$$.length&&0<=$trust_keys$$.indexOf($trust_Key$$)?$points$$=1:($points$$=Math.roundBy($total_points$$/2),console.log("This request is limit rate at",$points$$,"/",$total_points$$,{origin:$origin$$,originalUrl:$originalUrl$$,trust_domains:$trust_domains$$,trust_keys:$trust_keys$$,trust_Key:$trust_Key$$,key:$key$$,header:$req$$.headers}));setImmediate(async()=>{try{await $rateLimiter$$.removeTokens($points$$,$key$$),$next$$()}catch($err$$){console.error("Error rate limiter",$keyPrefix$$,
|
|
19
19
|
$err$$),$err$$ instanceof Error?$res$$.status(400).send({error:$err$$.message||"Error rate limiter"}):$res$$.status(429).send({error:"Too Many Requests. Please try again later"})}})}};
|