UNPKG

flexbiz-server

Version:

Flexible Server

13 lines (12 loc) 6.21 kB
'use strict';const model=global.getModel("asswithdraw"),assinvestment=global.getModel("assinvestment"),Trangthai=global.getModel("trangthai"),controller=require("../../controllers/controller"),{isSupperAdmin}=require("../../libs/utils"),moment=require("moment"); module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"asswithdraw",{sort:{date_created:-1},notNeedRight:!0,requireOtp:($data$$,$options$$)=>isSupperAdmin($options$$.user.email.toLowerCase())?!1:!0,onFinding:async($user$$,$condition$$,$next$$)=>{$next$$(null,$condition$$)},onView:async($user$$,$items$$,$next$$)=>{await $items$$.asyncJoinModel2(null,Trangthai,[{where:$a$$=>({ma_trang_thai:$a$$.trang_thai,ma_ct:"WITHDRAW"}),fields:["ten_trang_thai","color"]}]); await $items$$.asyncJoinModel2($user$$.current_id_app,assinvestment,[{where:$a$$=>({token:$a$$.token}),fields:["ten_san_pham","product_token"]}]);$next$$(null,$items$$)},onCreating:async($id_app_user$$,$Withdraw$$,$next$$)=>{var $creator_investment$$=$Withdraw$$.creator;if(!await global.getModel("asskey").findOne({id_app:$id_app_user$$.current_id_app,username:$id_app_user$$.email,address:$creator_investment$$}).lean())return $next$$("\u0110\u1ecba ch\u1ec9 v\u00ed n\u00e0y kh\u00f4ng thu\u1ed9c v\u1ec1 b\u1ea1n"); $id_app_user$$=$id_app_user$$.current_id_app;$creator_investment$$=await global.getModel("assinvestment").findOne({id_app:$id_app_user$$,token:$Withdraw$$.token});if(!$creator_investment$$)return $next$$("Kho\u1ea3n \u0111\u1ea7u t\u01b0 kh\u00f4ng t\u1ed3n t\u1ea1i");$creator_investment$$.da_rut_von=$creator_investment$$.da_rut_von||0;var $con_lai_period$$=$creator_investment$$.so_luong-$creator_investment$$.da_rut_von;if(0>=$con_lai_period$$||$creator_investment$$.da_dao_han)return $next$$("Kho\u1ea3n \u0111\u1ea7u t\u01b0 n\u00e0y \u0111\u00e3 \u0111\u00e1o h\u1ea1n ho\u1eb7c \u0111\u00e3 r\u00fat v\u1ed1n"); if($Withdraw$$.so_luong>$con_lai_period$$)return $next$$("S\u1ed1 l\u01b0\u1ee3ng r\u00fat v\u1ed1n ph\u1ea3i nh\u1ecf h\u01a1n s\u1ed1 l\u01b0\u1ee3ng c\u00f2n l\u1ea1i c\u1ee7a kho\u1ea3n \u0111\u1ea7u t\u01b0");var $laiSuat_product$$=await global.getModel("assproduct").findOne({id_app:$id_app_user$$,id:$creator_investment$$.id_product});$con_lai_period$$=await global.getModel("assperiod").findOne({id_app:$id_app_user$$,id:$creator_investment$$.id_period});if(!$laiSuat_product$$||!$con_lai_period$$)return $next$$("Y\u00eau c\u1ea7u kh\u00f4ng h\u1ee3p l\u1ec7"); $Withdraw$$.product_owner=$laiSuat_product$$.creator;$Withdraw$$.denom=$creator_investment$$.denom;$Withdraw$$.ngay=new Date;$Withdraw$$.tg_xu_ly_rut_von=$creator_investment$$.tg_xu_ly_rut_von||0;$Withdraw$$.ngay_xu_ly=moment($Withdraw$$.ngay).add($Withdraw$$.tg_xu_ly_rut_von,"days").toDate();$Withdraw$$.trang_thai="0";$Withdraw$$.gia=$creator_investment$$.gia||0;$Withdraw$$.von=$Withdraw$$.so_luong*($creator_investment$$.gia||0);$Withdraw$$.lai_thuong=0;if(!$creator_investment$$.ngay_dao_han||0< moment($creator_investment$$.ngay_dao_han).diff(moment($Withdraw$$.ngay),"days"))if($creator_investment$$.cho_phep_rut_von){if(0<$con_lai_period$$.so_ngay_dau_tu_toi_thieu&&moment($Withdraw$$.ngay).diff(moment($creator_investment$$.ngay_phat_hanh),"days")<$con_lai_period$$.so_ngay_dau_tu_toi_thieu)return $next$$("Kho\u1ea3n \u0111\u1ea7u t\u01b0 n\u00e0y ch\u01b0a \u0111\u1ea1t s\u1ed1 ng\u00e0y t\u1ed1i thi\u1ec3u \u0111\u1ec3 r\u00fat tr\u01b0\u1edbc h\u1ea1n");$laiSuat_product$$=$creator_investment$$.lai_suat_truoc_han}else return $next$$("Kho\u1ea3n \u0111\u1ea7u t\u01b0 n\u00e0y kh\u00f4ng \u0111\u01b0\u1ee3c ph\u00e9p r\u00fat v\u1ed1n tr\u01b0\u1edbc h\u1ea1n"); else $laiSuat_product$$=$creator_investment$$.lai_suat_cam_ket;try{await global.getModel("assbank").sendCoinsFromAccountToSystem($id_app_user$$,$Withdraw$$.creator,$Withdraw$$.so_luong,$Withdraw$$.token)}catch($e$$){return $next$$($e$$)}$Withdraw$$.lai_suat_truoc_han=$laiSuat_product$$;$Withdraw$$.so_ngay_tinh_lai=$con_lai_period$$.tru_lai_da_tra&&$creator_investment$$.ngay_tinh_lai_ky_truoc?moment($Withdraw$$.ngay).diff(moment($creator_investment$$.ngay_tinh_lai_ky_truoc),"days"):moment($Withdraw$$.ngay).diff(moment($creator_investment$$.ngay_phat_hanh), "days");$Withdraw$$.lai_truoc_han=Math.roundBy($Withdraw$$.von*$Withdraw$$.lai_suat_truoc_han*$Withdraw$$.so_ngay_tinh_lai/36500,0);$creator_investment$$.da_rut_von+=$Withdraw$$.so_luong;$creator_investment$$.da_rut_von>=$creator_investment$$.so_luong&&($creator_investment$$.trang_thai="9");await $creator_investment$$.save();if(!$Withdraw$$.tg_xu_ly_rut_von){try{await global.getModel("assbank").sendCoins($id_app_user$$,$Withdraw$$.product_owner,$Withdraw$$.creator,$Withdraw$$.von,$Withdraw$$.denom, {...$Withdraw$$.toObject(),event:"WITHDRAW",memo:"R\u00fat v\u1ed1n"})}catch($e$$){return $next$$("Hi\u1ec7n t\u1ea1i kh\u00f4ng th\u1ec3 r\u00fat v\u1ed1n. B\u1ea1n hay th\u1eed l\u1ea1i sau")}try{$Withdraw$$.lai_truoc_han&&await global.getModel("assbank").sendCoins($id_app_user$$,$Withdraw$$.product_owner,$Withdraw$$.creator,$Withdraw$$.lai_truoc_han,$Withdraw$$.denom,{...$Withdraw$$.toObject(),event:"INTEREST",memo:"L\u00e3i t\u1eeb \u0111\u1ea7u t\u01b0"})}catch($e$$){return await global.getModel("assbank").sendCoins($id_app_user$$, $Withdraw$$.creator,$Withdraw$$.product_owner,$Withdraw$$.von,$Withdraw$$.denom,{...$Withdraw$$.toObject(),event:"WITHDRAW",memo:"R\u00fat v\u1ed1n kh\u00f4ng th\u00e0nh c\u00f4ng. X\u1eed l\u00fd l\u1ea1i v\u1ed1n \u0111\u00e3 tr\u1ea3 \u0111\u1ec3 th\u1ef1c hi\u1ec7n sau"}),$next$$("Hi\u1ec7n t\u1ea1i kh\u00f4ng th\u1ec3 r\u00fat v\u1ed1n. B\u1ea1n hay th\u1eed l\u1ea1i sau")}$Withdraw$$.da_rut_von=!0;$Withdraw$$.trang_thai="5"}$next$$(null,$Withdraw$$)},onCreated:async($user$$,$obj$$,$next$$)=> {$obj$$.id=$obj$$._id.toString();await model.updateOne({_id:$obj$$._id},{id:$obj$$.id});$next$$(null,$obj$$)}});$contr_router$$.deleting=function($user$$,$obj$$,$fn$$){if(!isSupperAdmin($user$$.email.toLowerCase()))return $fn$$("B\u1ea1n kh\u00f4ng th\u1ec3 x\u00f3a");$fn$$(null,$obj$$)};$contr_router$$.updating=function($user$$,$data$$,$obj$$,$fn$$){return $fn$$("Kh\u00f4ng th\u1ec3 c\u1eadp nh\u1eadt")};$contr_router$$.route()};