UNPKG

flexbiz-server

Version:

Flexible Server

96 lines (94 loc) 23.1 kB
const App=require("./app"),Options=require("./options"),ngcompile=require("ng-node-compile"),utils=require("../libs/utils"),File=require("./file"),underscore=require("underscore"),moment=require("moment"),participant=require("./participant"),_=require("lodash"),{onAfterCommit,executeInTransaction}=require("../libs/sessionContext"),approveSchema=new Schema({id_app:{type:String,required:!0,maxlength:1024},id_ct:{type:String,required:!0,maxlength:1024},ma_ct:{type:String,required:!0,maxlength:32,trim:!0}, trang_thai:{type:String},user_request:{type:String,required:!0,maxlength:128,trim:!0},user_request_name:{type:String,required:!0,maxlength:1024},template_content:String,request_content:String,user_approves:[{email:{type:String,maxlength:128,trim:!0},name:{type:String,maxlength:1024}}],user_approved:{type:String,maxlength:128,trim:!0},user_approved_name:{type:String,maxlength:1024},date_approved:Date,approved_reason:String,user_denied:{type:String,maxlength:128,trim:!0},user_denied_name:{type:String, maxlength:1024},date_denied:Date,denied_reason:{type:String,maxlength:4E3},update_after_approve:Schema.Types.Mixed,update_after_deny:Schema.Types.Mixed,title:{type:String,maxlength:1024},data:Schema.Types.Mixed,status:{type:Boolean,default:!1},date_created:{type:Date,default:Date.now},date_updated:{type:Date,default:Date.now},user_created:{type:String,default:""},user_updated:{type:String,default:""},visible_to:{type:Number,default:2},visible_to_users:[String]}); (global.configs||{}).createIndexes&&(approveSchema.index({id_ct:1,id_app:1}),approveSchema.index({id_ct:1}),approveSchema.index({id_app:1,trang_thai:1}),approveSchema.index({id_app:1,user_approved:1}),approveSchema.index({id_app:1,user_denied:1}),approveSchema.index({id_app:1,status:1}),approveSchema.index({id_app:1,date_created:1}),approveSchema.index({id_app:1,user_request:"text",user_request_name:"text"},{name:"approve_index"}));const model=mongoose.models.approve||mongoose.model("approve",approveSchema); let renderRequests=$requests$$=>$requests$$&&$requests$$.length!==0?` <p style='margin-top:50px'> <hr/> <h5>Danh s\u00e1ch \u0111\u00e3 duy\u1ec7t ho\u1eb7c t\u1eeb ch\u1ed1i</h5> ${$requests$$.map($rq$$=>{if($rq$$.user_approved)return` <div style="color:green"> <b>${$rq$$.data.ten_trang_thai||$rq$$.title}</b> <div>Ng\u01b0\u1eddi duy\u1ec7t: ${$rq$$.user_approved_name||$rq$$.user_approved}</div> <div>Ng\u00e0y: ${moment($rq$$.date_approved).format("DD/MM/YYYY")}</div> </div> `;if($rq$$.user_denied)return` <div style="color:red"> <b>${$rq$$.data.ten_trang_thai||$rq$$.title}</b> <div>Ng\u01b0\u1eddi t\u1eeb ch\u1ed1i: ${$rq$$.user_denied_name||$rq$$.user_denied}</div> <div>Ng\u00e0y: ${moment($rq$$.date_denied).format("DD/MM/YYYY")}</div> <div>L\u00fd do: ${$rq$$.denied_reason}</div> </div> `}).join("")} </p> `:"";const footerEmailTemplate=$requests$$=>` ${renderRequests($requests$$)} <p style='margin-top:20px'> <hr/> <div><span style='color:#16726D'>Created by:</span> <span style='color:#397A3A'>{{datasource.name_user_created || datasource.user_created}} at {{datasource.date_created|date:'dd/MM/yyyy HH:mm'}}<span></div> <div><span style='color:#16726D'>Last Update by:</span> <span style='color:#397A3A'>{{datasource.name_user_updated || datasource.user_updated}} at {{datasource.date_created|date:'dd/MM/yyyy HH:mm'}}</span></div> </p> `; model.request=async function($user_request$$,$user_approve$$,$title$$,$data_request_template_gen$$,$callback$$,$update_after_approve$$,$template_content$$,$update_after_deny$$){try{let $user_approves$$=$user_approve$$;underscore.isArray($user_approves$$)||($user_approves$$=[$user_approve$$]);let $data$$;$data$$=$data_request_template_gen$$.toObject?$data_request_template_gen$$.toObject():_.cloneDeep($data_request_template_gen$$);$data$$._id=($data$$._id||"").toString();let $_c$$={id_ct:$data$$._id,id_app:$data$$.id_app}; Object.keys($data$$).includes("trang_thai")&&($_c$$.trang_thai=$data$$.trang_thai);const $old_request$$=await model.findOne($_c$$);if($old_request$$&&$old_request$$.user_approved)return $callback$$(`Kh\u00f4ng th\u1ec3 t\u1ea1o y\u00eau c\u1ea7u duy\u1ec7t cho tr\u1ea1ng th\u00e1i ${$_c$$.trang_thai} c\u1ee7a ch\u1ee9ng t\u1eeb ${$data$$._id} v\u00ec \u0111\u00e3 \u0111\u01b0\u1ee3c duy\u1ec7t`);await model.deleteOne($_c$$);const $m$$={id_app:$data$$.id_app,id_ct:$data$$._id,ma_ct:$data$$.ma_ct,trang_thai:$data$$.trang_thai, user_request:$user_request$$.email,user_request_name:$user_request$$.name,user_approves:$user_approves$$,update_after_approve:$update_after_approve$$,update_after_deny:$update_after_deny$$,title:$title$$,template_content:$template_content$$,visible_to_users:$user_approves$$.map($u$$=>$u$$.email)};let $app_info$$=await App.findOne({_id:$data$$.id_app}).lean();if($app_info$$){var $option$$;$data$$.ma_ct&&($option$$=await Options.findOne({id_app:$data$$.id_app,id_func:{$regex:"^"+$data$$.ma_ct+"$",$options:"i"}}).lean()); var $ngEnviorment$$=new ngcompile([]),$template$$=$template_content$$;!$template$$&&$option$$&&$option$$.option&&$option$$.option.system&&($template$$=($template$$=((($option$$.option||{}).system||{}).templateEmail||(($option$$.option||{}).system||{}).templateRpt||"").split("<em>").join("").split("</em>").join(""))||"Ch\u01b0a khai b\u00e1o n\u1ed9i dung duy\u1ec7t");if($template$$){$data_request_template_gen$$=`<h3>${$title$$}</h3> <div>Ng\u01b0\u1eddi y\u00eau c\u1ea7u: {{user_request.name}}</div> <div>Ng\u00e0y y\u00eau c\u1ea7u: {{datasource.date_created|date:'dd/MM/yyyy HH:mm:ss'}}</div> <div>N\u1ed9i dung:</div><hr/>${$template$$}<hr/> `;let $_data$$={user_request:$user_request$$,user_approve:$user_approve$$,datasource:_.cloneDeep($data$$),company:$app_info$$,app_info:$app_info$$,doc_so:utils.numberToWord};try{$m$$.request_content=$ngEnviorment$$.$compile($data_request_template_gen$$)($_data$$)}catch($e$$){Logger.error($e$$)}}$m$$.data=_.cloneDeep($data$$);delete $m$$.data.$$hashKey;if($m$$.data.details){const $details$$=$m$$.data.details.map($d$$=>{$d$$.toObject&&($d$$=$d$$.toObject());delete $d$$.$$hashKey;delete $d$$._id; return{...$d$$}});$m$$.data.details=$details$$}var $rs$$=await model.create($m$$);$user_approves$$.forEach(async $user_approve$$=>{$user_approve$$.id_request=$rs$$._id.toString();$rs$$.trang_thai!==void 0&&$rs$$.trang_thai!==null&&($user_approve$$.trang_thai=$rs$$.trang_thai);var $_data$$=utils.encrypt($user_approve$$);let $rq_approve$$=$rs$$.toObject();$rq_approve$$.url_approve=$_data$$;$rq_approve$$.url_deny=$_data$$;if($template$$){var $email_content_requests$$=await model.find({id_ct:$data$$._id, id_app:$data$$.id_app}).sort({trang_thai:-1}).lean();await $email_content_requests$$.asyncJoinModel2($data$$.id_app,participant,{where:{user_approved:"email"},fields:{user_approved_name:"name"}});await $email_content_requests$$.asyncJoinModel2($data$$.id_app,participant,{where:{user_denied:"email"},fields:{user_denied_name:"name"}});$email_content_requests$$=`<h3>${$title$$}</h3> <div>Ng\u01b0\u1eddi y\u00eau c\u1ea7u: {{user_request.name}}</div> <div>Ng\u00e0y y\u00eau c\u1ea7u: {{datasource.date_created|date:'dd/MM/yyyy HH:mm:ss'}}</div> <div>N\u1ed9i dung:</div><hr/>${$template$$}<hr/> <div> <b><a href='{{url_approve}}'>Duy\u1ec7t y\u00eau c\u1ea7u n\u00e0y</a></b> <b style='margin-left:20px'>|<a style='color:silver;margin-left:20px' href='{{url_deny}}'>Kh\u00f4ng duy\u1ec7t y\u00eau c\u1ea7u n\u00e0y</a></b> </div> ${footerEmailTemplate($email_content_requests$$.filter($r$$=>$r$$.user_approved||$r$$.user_denied))} `;$_data$$={user_request:$user_request$$,user_approve:$user_approve$$,datasource:$data$$,url_approve:(configs.api_url||configs.domain)+"/public/approve/"+$_data$$,url_deny:(configs.api_url||configs.domain)+"/public/deny-approve/"+$_data$$,company:$app_info$$,app_info:$app_info$$,doc_so:utils.numberToWord};$email_content_requests$$=$ngEnviorment$$.$compile($email_content_requests$$)($_data$$)}else Logger.info("[approve][createRequest] don't find template or report template to send notify to approve user"); try{const $atts$$=await File.find({id_link:$data$$._id,id_app:$data$$.id_app}).lean();App.emitEvent($user_request$$.current_id_app,"request_approve",{_id:$data$$._id,id_app:$data$$.id_app,title:$title$$,body:$title$$,rq_approve:$rq_approve$$,code:$data$$.ma_ct,email_content:$email_content_requests$$,allow_unsubcribe:!1,attachments:$atts$$},null,[$user_approve$$.email],!0,!0)}catch($e$$){Logger.error("[approve][createRequest] Error send the request approve",$e$$.message||$e$$)}});$callback$$(null, $rs$$)}}catch($e$$){$callback$$($e$$)}}; model.sendNotifyApproved=async function($rq$$){if($rq$$.ma_ct){var $title$$=`[\u0110\u00e3 duy\u1ec7t] ${$rq$$.title}`;Options.findOne({id_app:$rq$$.id_app,id_func:{$regex:"^"+$rq$$.ma_ct+"$",$options:"i"}}).lean().then(async $option$$=>{$rq$$.template_content||$option$$&&$option$$.option&&$option$$.option.system&&($option$$.option.system.templateEmail||$option$$.option.system.templateRpt)?App.findOne({_id:$rq$$.id_app}).lean().then(async $_data$jscomp$2_app_info$$=>{if(!$_data$jscomp$2_app_info$$)return Logger.info("khong the tim thay thong tin cong ty nay"); let $requests$$=await model.find({id_ct:$rq$$.data._id,id_app:$rq$$.id_app}).sort({trang_thai:-1}).lean();await $requests$$.asyncJoinModel2($rq$$.id_app,participant,{where:{user_approved:"email"},fields:{user_approved_name:"name"}});await $requests$$.asyncJoinModel2($rq$$.id_app,participant,{where:{user_denied:"email"},fields:{user_denied_name:"name"}});let $template$$=($rq$$.template_content||(($option$$.option||{}).system||{}).templateEmail||(($option$$.option||{}).system||{}).templateRpt).split("<em>").join("").split("</em>").join(""); $template$$=` <div> <h3>Y\u00eau c\u1ea7u d\u01b0\u1edbi \u0111\u00e2y \u0111\u00e3 \u0111\u01b0\u1ee3c duy\u1ec7t b\u1edfi ${$rq$$.user_approved_name||$rq$$.user_approved} </h3> <div>Ng\u01b0\u1eddi y\u00eau c\u1ea7u: {{user_request.name}}</div> <div>Ng\u00e0y y\u00eau c\u1ea7u: {{datasource.date_created|date:'dd/MM/yyyy HH:mm:ss'}}</div> <div>N\u1ed9i dung:</div> </div> <hr/> ${$template$$} ${footerEmailTemplate($requests$$.filter($r$$=>$r$$.user_approved||$r$$.user_denied))}`;let $ngEnviorment$$=new ngcompile([]);$_data$jscomp$2_app_info$$={user_request:{name:$rq$$.user_request_name,email:$rq$$.user_request},user_approve:{name:$rq$$.user_approved_name,email:$rq$$.user_approved,date:$rq$$.date_approved},datasource:$rq$$.data,company:$_data$jscomp$2_app_info$$,app_info:$_data$jscomp$2_app_info$$,doc_so:utils.numberToWord};try{let $email_content$$=$ngEnviorment$$.$compile($template$$)($_data$jscomp$2_app_info$$), $emails$$=[$rq$$.user_request],$rqs$$=$requests$$.map($a$$=>$a$$.user_request),$aps$$=$requests$$.filter($r$$=>$r$$._id.toString()!==$rq$$._id.toString()).map($a$$=>$a$$.user_approved);$emails$$=[...(new Set($emails$$.concat($rqs$$).concat($aps$$)))].filter($r$$=>$r$$&&$r$$!==$rq$$.user_approved);$emails$$.length>0&&App.emitEvent($rq$$.id_app,"request_approved",{_id:$rq$$.id_ct,id_app:$rq$$.id_app,title:$title$$,body:$title$$,email_content:$email_content$$,code:$rq$$.ma_ct,allow_unsubcribe:!1,attachments:$rq$$.data._id? await File.find({id_link:$rq$$.data._id}).lean():void 0},null,$emails$$,!0,!0)}catch($e$$){Logger.error("Error send email",$e$$.message)}}).catch($e$$=>{Logger.error($e$$)}):Logger.info("don't find email template or report template to send notify email to request user")}).catch($e$$=>{Logger.error($e$$)})}}; model.approve=async function($encrypted$$,$callback$$,$token$$,$options$$={reason:""}){try{const $result_approve$$=await executeInTransaction(async()=>{Logger.info("[approve] \u0111ang duy\u1ec7t y\u00eau c\u1ea7u....");let $user_approved$$;if(underscore.isObject($encrypted$$)){var $_c$$=$encrypted$$.id_request;$user_approved$$=$encrypted$$.email;var $approve_user_approved_name$$=$encrypted$$.name;var $key_data_trang_thai$$=$encrypted$$.trang_thai}else $key_data_trang_thai$$=utils.decrypt($encrypted$$), $_c$$=$key_data_trang_thai$$.id_request,$user_approved$$=$key_data_trang_thai$$.email,$approve_user_approved_name$$=$key_data_trang_thai$$.name,$key_data_trang_thai$$=$key_data_trang_thai$$.trang_thai;if(!$_c$$||!$user_approved$$)throw"request id and approve user are required";if(!global.mongoose.Types.ObjectId.isValid($_c$$))throw"request id '"+$_c$$+"' is not valid";$_c$$={_id:$_c$$};$key_data_trang_thai$$!==void 0&&$key_data_trang_thai$$!==null&&($_c$$.trang_thai=$key_data_trang_thai$$);const $rq$$= await model.findOne($_c$$);if(!$rq$$)throw"Y\u00eau c\u1ea7u kh\u00f4ng t\u1ed3n t\u1ea1i";if($rq$$.date_approved)throw"Y\u00eau c\u1ea7u n\u00e0y \u0111\u00e3 \u0111\u01b0\u1ee3c duy\u1ec7t b\u1edfi "+$rq$$.user_approved_name;if($rq$$.date_denied)throw"Y\u00eau c\u1ea7u n\u00e0y \u0111\u00e3 b\u1ecb t\u1eeb ch\u1ed1i duy\u1ec7t b\u1edfi "+$rq$$.user_denied_name;$rq$$.date_approved=new Date;$rq$$.user_approved=$user_approved$$;$rq$$.user_approved_name=$approve_user_approved_name$$;$rq$$.approved_reason= $options$$.reason||"";if(!$rq$$.user_approves||!$rq$$.user_approves.find($u$$=>$u$$.email.indexOf($user_approved$$)>=0))throw"User n\u00e0y kh\u00f4ng c\u00f3 quy\u1ec1n duy\u1ec7t y\u00eau c\u1ea7u n\u00e0y";$approve_user_approved_name$$=await $rq$$.save();if($rq$$.update_after_approve&&$rq$$.update_after_approve.data){Logger.info("[approve] t\u00ecm controller c\u1ee7a ch\u1ee9ng t\u1eeb \u0111\u00e3 duy\u1ec7t",$rq$$.update_after_approve.module||$rq$$.update_after_approve.model);let $module_name$$= $rq$$.update_after_approve.module||$rq$$.update_after_approve.model,$ctrl$$;$module_name$$&&($ctrl$$=global.controllers[$module_name$$.toUpperCase()]);let $_m$$;$rq$$.update_after_approve.model?$_m$$=global.getModel($rq$$.update_after_approve.model):$ctrl$$&&($_m$$=$ctrl$$.getProperty("model"));if(!$_m$$){Logger.error("[approve] kh\u00f4ng t\u00ecm th\u1ea5y model c\u1ee7a ch\u1ee9ng t\u1eeb",$rq$$.update_after_approve.model||$module_name$$.toUpperCase());return}Logger.info("[approve] c\u1eadp nh\u1eadt data cho ch\u1ee9ng t\u1eeb sau khi duy\u1ec7t...", $module_name$$);await $_m$$.updateOne({_id:$rq$$.id_ct},$rq$$.update_after_approve.data);if($ctrl$$){const $obj$$=await $_m$$.findOne({_id:$rq$$.id_ct});if($obj$$){onAfterCommit(()=>{$ctrl$$.emit("saved",$obj$$);$ctrl$$.emit("updated",$obj$$)});const $u$$=await global.getModel("user").findOne({email:$user_approved$$});$u$$.current_id_app=$obj$$.id_app;const $app_info$$=await App.findOne({_id:$obj$$.id_app});Logger.info("[approve] post s\u1ed5 s\u00e1ch sau khi duy\u1ec7t ch\u1ee9ng t\u1eeb...",$ctrl$$.name, $module_name$$,$obj$$.so_ct,$obj$$.trang_thai);await new Promise(($resolve$$,$reject$$)=>{$ctrl$$.postData($obj$$,$e$$=>{if($e$$)return Logger.error("[approve] [postData]",$e$$,$module_name$$,$obj$$.so_ct,$obj$$.trang_thai),$reject$$($e$$);$resolve$$()},{kiem_tra_han_muc_cong_no:!1})});$ctrl$$.handleAfterAproved&&await $ctrl$$.handleAfterAproved($obj$$,$rq$$);await new Promise(($resolve$$,$reject$$)=>{$ctrl$$.onView($u$$,[$obj$$.toObject()],async($channel_e$$,$rs$$)=>{$channel_e$$&&Logger.error("[approve][approved][onView]", $channel_e$$);$channel_e$$=`${$module_name$$.toLowerCase()}-${"request_approved"}`;$obj$$.id_app&&($channel_e$$=`${$channel_e$$}-${$obj$$.id_app}`);let $message$$=JSON.stringify($rs$$[0]);try{await global.clientRedis.publish($channel_e$$,$message$$)}catch($e$$){Logger.error("[approve] [sendNotifyApproved] fire event","request_approved",$e$$)}Logger.info("[approve][approved][requestApprove] t\u1ea1o y\u00eau c\u1ea7u duy\u1ec7t ti\u1ebfp theo...");$ctrl$$.requestApprove($app_info$$,$u$$,$rs$$[0],$e$$=> {if($e$$)return $reject$$($e$$);$resolve$$()},null,$rq$$)})})}else Logger.error("[approve] kh\u00f4ng tim th\u1ea5y l\u1ed7i data \u0111\u00e3 duy\u1ec7t theo id_ct",$rq$$.id_ct)}else Logger.error("[approve] kh\u00f4ng t\u00ecm th\u1ea5y controller c\u1ee7a ch\u1ee9ng t\u1eeb",$module_name$$)}else Logger.info("[approve] kh\u00f4ng c\u00f3 d\u1eef li\u1ec7u c\u1ea7n c\u1eadp nh\u1eadt sau khi \u0111\u00e3 duy\u1ec7t");onAfterCommit(()=>{model.sendNotifyApproved($rq$$,$token$$)});return $approve_user_approved_name$$}); $callback$$(null,$result_approve$$)}catch($e$$){Logger.error("[approve][approved]",$e$$),$callback$$($e$$)}}; model.deny=async function($encrypted$$,$callback$$,$token$$,$options$$={reason:""}){try{const $result_reject$$=await executeInTransaction(async()=>{let $user_denied$$;if(underscore.isObject($encrypted$$)){var $_c$jscomp$2__id$$=$encrypted$$.id_request;$user_denied$$=$encrypted$$.email;var $approve$$=$encrypted$$.name;var $key_data$jscomp$1_trang_thai$$=$encrypted$$.trang_thai}else $key_data$jscomp$1_trang_thai$$=utils.decrypt($encrypted$$),$_c$jscomp$2__id$$=$key_data$jscomp$1_trang_thai$$.id_request, $user_denied$$=$key_data$jscomp$1_trang_thai$$.email,$approve$$=$key_data$jscomp$1_trang_thai$$.name,$key_data$jscomp$1_trang_thai$$=$key_data$jscomp$1_trang_thai$$.trang_thai;if(!$_c$jscomp$2__id$$||!$user_denied$$)throw"request id and deny user are required";if(!global.mongoose.Types.ObjectId.isValid($_c$jscomp$2__id$$))throw"request id '"+$_c$jscomp$2__id$$+"' is not valid";$_c$jscomp$2__id$$={_id:$_c$jscomp$2__id$$};$key_data$jscomp$1_trang_thai$$!==void 0&&$key_data$jscomp$1_trang_thai$$!==null&& ($_c$jscomp$2__id$$.trang_thai=$key_data$jscomp$1_trang_thai$$);const $rq$$=await model.findOne($_c$jscomp$2__id$$);if(!$rq$$)throw"Y\u00eau c\u1ea7u kh\u00f4ng t\u1ed3n t\u1ea1i";if($rq$$.date_approved)throw"Y\u00eau c\u1ea7u n\u00e0y \u0111\u00e3 \u0111\u01b0\u1ee3c duy\u1ec7t b\u1edfi "+$rq$$.user_approved_name;if($rq$$.date_denied)throw"Y\u00eau c\u1ea7u n\u00e0y \u0111\u00e3 b\u1ecb t\u1eeb ch\u1ed1i duy\u1ec7t b\u1edfi "+$rq$$.user_denied_name;$rq$$.date_denied=new Date;$rq$$.user_denied=$user_denied$$; $rq$$.user_denied_name=$approve$$;$rq$$.denied_reason=$options$$.reason||"";if(!$rq$$.user_approves||!$rq$$.user_approves.find($u$$=>$u$$.email.indexOf($user_denied$$)>=0))throw"User n\u00e0y kh\u00f4ng c\u00f3 quy\u1ec1n duy\u1ec7t y\u00eau c\u1ea7u n\u00e0y";$approve$$=await $rq$$.save();onAfterCommit(async()=>{let $title$$=`[B\u1ecb t\u1eeb ch\u1ed1i] ${$rq$$.title}`;var $option$jscomp$2_template$$=await Options.findOne({id_app:$rq$$.id_app,id_func:{$regex:"^"+$rq$$.ma_ct+"$",$options:"i"}}).lean(); if($rq$$.template_content||$option$jscomp$2_template$$&&$option$jscomp$2_template$$.option&&$option$jscomp$2_template$$.option.system&&($option$jscomp$2_template$$.option.system.templateEmail||$option$jscomp$2_template$$.option.system.templateRpt)){var $_data$jscomp$3_app_info$$=await App.findOne({_id:$rq$$.id_app}).lean();if(!$_data$jscomp$3_app_info$$)return Logger.info("khong the tim thay thong tin cong ty nay");let $requests$$=await model.find({id_ct:$rq$$.data._id,id_app:$rq$$.id_app}).sort({trang_thai:-1}).lean(); await $requests$$.asyncJoinModel2($rq$$.id_app,participant,{where:{user_approved:"email"},fields:{user_approved_name:"name"}});await $requests$$.asyncJoinModel2($rq$$.id_app,participant,{where:{user_denied:"email"},fields:{user_denied_name:"name"}});$option$jscomp$2_template$$=($rq$$.template_content||(($option$jscomp$2_template$$.option||{}).system||{}).templateEmail||(($option$jscomp$2_template$$.option||{}).system||{}).templateRpt).split("<em>").join("").split("</em>").join("");$option$jscomp$2_template$$= ` <div> <h3>Y\u00eau c\u1ea7u d\u01b0\u1edbi \u0111\u00e2y \u0111\u00e3 b\u1ecb t\u1eeb ch\u1ed1i duy\u1ec7t b\u1edfi ${$rq$$.user_denied_name} </h3> <div style='color:red'>L\u00fd do t\u1eeb ch\u1ed1i: ${$rq$$.denied_reason} </div> <div>Ng\u01b0\u1eddi y\u00eau c\u1ea7u: {{user_request.name}}</div> <div>Ng\u00e0y y\u00eau c\u1ea7u: {{datasource.date_created|date:'dd/MM/yyyy HH:mm:ss'}}</div> <div>N\u1ed9i dung:</div> </div> <hr/> ${$option$jscomp$2_template$$} ${footerEmailTemplate($requests$$.filter($r$$=>$r$$.user_approved||$r$$.user_denied))}`;let $ngEnviorment$$=new ngcompile([]);$_data$jscomp$3_app_info$$={user_request:{name:$rq$$.user_request_name,email:$rq$$.user_request},user_deny:{name:$rq$$.user_denied_name,email:$rq$$.user_denied},datasource:$rq$$.data,company:$_data$jscomp$3_app_info$$,app_info:$_data$jscomp$3_app_info$$,doc_so:utils.numberToWord};try{let $email_content$$=$ngEnviorment$$.$compile($option$jscomp$2_template$$)($_data$jscomp$3_app_info$$), $emails$$=[$rq$$.user_request],$rqs$$=$requests$$.map($a$$=>$a$$.user_request),$aps$$=$requests$$.filter($r$$=>$r$$._id.toString()!==$rq$$._id.toString()).map($a$$=>$a$$.user_approved);$emails$$=[...(new Set($emails$$.concat($rqs$$).concat($aps$$)))].filter($r$$=>$r$$);$emails$$.length>0&&App.emitEvent($rq$$.id_app,"request_denied",{_id:$rq$$.id_ct,id_app:$rq$$.id_app,title:$title$$,body:$title$$,email_content:$email_content$$,code:$rq$$.ma_ct,allow_unsubcribe:!1,attachments:$rq$$.data._id?await File.find({id_link:$rq$$.data._id}).lean(): void 0},null,$emails$$,!0,!0)}catch($e$$){Logger.error("Error send email",$e$$.message)}}else Logger.info("don't find email template or report template to send notify email to request user")});if($rq$$.update_after_deny&&$rq$$.update_after_deny.data){let $module_name$$=$rq$$.update_after_deny.module||$rq$$.update_after_deny.model,$ctrl$$;$module_name$$&&($ctrl$$=global.controllers[$module_name$$.toUpperCase()]);let $_m$$;$rq$$.update_after_deny.model?$_m$$=global.getModel($rq$$.update_after_deny.model): $ctrl$$&&($_m$$=$ctrl$$.getProperty("model"));if(!$_m$$)return;await $_m$$.updateOne({_id:$rq$$.id_ct},$rq$$.update_after_deny.data);if($ctrl$$){const $obj$$=await $_m$$.findOne({_id:$rq$$.id_ct});$obj$$&&(onAfterCommit(()=>{$ctrl$$.emit("saved",$obj$$);$ctrl$$.emit("updated",$obj$$)}),await new Promise(($resolve$$,$reject$$)=>{(async()=>{$ctrl$$.postData($obj$$,async $e$jscomp$12_u$$=>{if($e$jscomp$12_u$$)return $reject$$($e$jscomp$12_u$$);if($ctrl$$.handleAfterDenied)try{await $ctrl$$.handleAfterDenied($obj$$, $rq$$)}catch($e$$){return $reject$$($e$$)}$e$jscomp$12_u$$=await global.getModel("user").findOne({email:$user_denied$$});$e$jscomp$12_u$$.current_id_app=$obj$$.id_app;$ctrl$$.onView($e$jscomp$12_u$$,[$obj$$.toObject()],async($channel$jscomp$1_e$$,$message$jscomp$10_rs$$)=>{$channel$jscomp$1_e$$&&Logger.error("[approve][denied][onView]",$channel$jscomp$1_e$$);$channel$jscomp$1_e$$=`${$module_name$$.toLowerCase()}-${"request_denied"}`;$obj$$.id_app&&($channel$jscomp$1_e$$=`${$channel$jscomp$1_e$$}-${$obj$$.id_app}`); $message$jscomp$10_rs$$=JSON.stringify($message$jscomp$10_rs$$[0]);try{await global.clientRedis.publish($channel$jscomp$1_e$$,$message$jscomp$10_rs$$)}catch($e$$){Logger.error("[approve] [sendNotifyDenied] fire event","request_denied",$e$$)}$resolve$$()})},{kiem_tra_han_muc_cong_no:!1})})()}))}}$callback$$(null,$approve$$)});$callback$$(null,$result_reject$$)}catch($e$$){Logger.error("[approve][denied]",$e$$),$callback$$($e$$)}};module.exports=model;