UNPKG

flexbiz-server

Version:

Flexible Server

14 lines (13 loc) 6.8 kB
'use strict';const model=global.getModel("asssell"),controller=require("../../controllers/controller"),AssProduct=global.getModel("assproduct"),Trangthai=global.getModel("trangthai"),async=require("async"),updateSell=model.updateSell,moment=require("moment"),{isSupperAdmin}=require("../../libs/utils"); module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"asssell",{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$$)=>{async.mapSeries($items$$.filter($item$$=>($item$$.id||0===$item$$.id)&&"0"==$item$$.trang_thai),($item$$,$cb$$)=>{setImmediate(async()=>{try{if(await updateSell($item$$), $item$$.goi_dau_tu)try{$item$$.market_value=await global.getModel("assinvestment").getInvestmentMarketValue($item$$.id_app,$item$$.token)}catch($e$$){console.error($e$$)}}catch($e$$){console.error("view asssell",$e$$)}$cb$$(null,$item$$)})},async()=>{$items$$=$items$$.filter($i$$=>!$i$$.not_found);await $items$$.asyncJoinModel2(null,Trangthai,{where:$item$$=>({ma_ct:"ASSSELL",ma_trang_thai:$item$$.trang_thai}),fields:["ten_trang_thai","color"]});await $items$$.asyncJoinModel2($user$$.current_id_app, AssProduct,{where:{id_product:"id"},fields:["ten_san_pham","hinh_anh","logo","ma_loai"]});$next$$(null,$items$$)})},onCreating:async($NeedToBuy_user$$,$Sell$$,$next$$)=>{var $app_creator_invest$$=$Sell$$.creator;if(!await global.getModel("asskey").findOne({id_app:$NeedToBuy_user$$.current_id_app,username:$NeedToBuy_user$$.email,address:$app_creator_invest$$}).lean())return $next$$("\u0110\u1ecba ch\u1ec9 v\u00ed n\u00e0y kh\u00f4ng thu\u1ed9c v\u1ec1 b\u1ea1n");$app_creator_invest$$=await global.getModel("app").findOne({_id:$NeedToBuy_user$$.current_id_app}).lean(); $Sell$$.fee=$app_creator_invest$$.exchange_fee;$Sell$$.exchange_address=$app_creator_invest$$.wallet_address;$app_creator_invest$$=await global.getModel("assinvestment").findOne({id_app:$NeedToBuy_user$$.current_id_app,token:$Sell$$.token});if(!$app_creator_invest$$)return $next$$("Investment package is not exists");if($app_creator_invest$$.da_dao_han)return $next$$("investment package expired");if(void 0!=$Sell$$.id_need_to_buy&&null!=$Sell$$.id_need_to_buy){$NeedToBuy_user$$=await global.getModel("assneedtobuy").findOne({id_app:$NeedToBuy_user$$.current_id_app, id:$Sell$$.id_need_to_buy});if(!$NeedToBuy_user$$)return $next$$("The need to buy does not exist");if(0!=$NeedToBuy_user$$.trang_thai)return $next$$("The need to buy has been canceled or expired");if($app_creator_invest$$.id_period!=$NeedToBuy_user$$.id_period)return $next$$("Incorrect investment package");if(moment().diff(moment($app_creator_invest$$.ngay_dao_han),"days")>$NeedToBuy_user$$.thoi_gian_con_lai)return $next$$("The remaining maturity is too long");$Sell$$.denom=$NeedToBuy_user$$.denom; $Sell$$.gia_ban=$NeedToBuy_user$$.gia_mua;$Sell$$.so_luong=$Sell$$.so_luong?Math.min($Sell$$.so_luong,$NeedToBuy_user$$.so_luong_con_lai):$NeedToBuy_user$$.so_luong_con_lai;$Sell$$.da_ban=$Sell$$.so_luong;$Sell$$.con_lai=0;$Sell$$.trang_thai=8;$Sell$$.ngay_het_han=new Date;try{await global.getModel("assbank").sendCoins($app_creator_invest$$.id_app,$Sell$$.creator,$NeedToBuy_user$$.creator,$Sell$$.so_luong,$Sell$$.token,{...$Sell$$.toObject(),event:"SELL",memo:`Chuy\u1ec3n kho\u1ea3n \u0111\u1ea7u t\u01b0 ${$Sell$$.so_luong}${$Sell$$.token} cho ng\u01b0\u1eddi mua`}); const $totalMoney$$=$Sell$$.gia_ban*$Sell$$.so_luong;await global.getModel("assbank").sendCoinsFromSystemToAccount($app_creator_invest$$.id_app,$Sell$$.creator,$totalMoney$$,$Sell$$.denom,{...$Sell$$.toObject(),event:"SELL",memo:`Nh\u1eadn ${$totalMoney$$}${$Sell$$.denom} t\u1eeb ng\u01b0\u1eddi b\u00e1n`});let $fee$$=Math.roundBy(($Sell$$.fee||0)*$totalMoney$$/100,0);$fee$$&&!$Sell$$.exchange_address&&await global.getModel("assbank").sendCoins($app_creator_invest$$.id_app,$Sell$$.creator,$Sell$$.exchange_address, $fee$$,$Sell$$.denom,{...$Sell$$.toObject(),event:"SELL",memo:"Ph\u00ed giao d\u1ecbch"});$NeedToBuy_user$$.so_luong_da_mua=($NeedToBuy_user$$.so_luong_da_mua||0)+$Sell$$.so_luong;$NeedToBuy_user$$.so_luong_con_lai=$NeedToBuy_user$$.so_luong-$NeedToBuy_user$$.so_luong_da_mua;0==$NeedToBuy_user$$.so_luong_con_lai&&($NeedToBuy_user$$.trang_thai="8");await $NeedToBuy_user$$.save()}catch($e$$){return $next$$($e$$)}}else{if((new Date($Sell$$.ngay_het_han)).getTime()<=(new Date).getTime())return $next$$("Ng\u00e0y h\u1ebft h\u1ea1n ph\u1ea3i l\u1edbn h\u01a1n ng\u00e0y hi\u1ec7n t\u1ea1i"); try{await global.getModel("assbank").sendCoinsFromAccountToSystem($app_creator_invest$$.id_app,$Sell$$.creator,$Sell$$.so_luong,$Sell$$.token,{...$Sell$$.toObject(),event:"SELL",memo:`Chuy\u1ec3n ${$Sell$$.so_luong}${$Sell$$.token} l\u00ean s\u00e0n`})}catch($e$$){return $next$$($e$$)}$Sell$$.da_ban=0;$Sell$$.con_lai=$Sell$$.so_luong;$Sell$$.trang_thai=0}$next$$(null,$Sell$$)},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=async function($user$$,$obj$$,$next$$){if(isSupperAdmin($user$$.email.toLowerCase()))return $next$$(null,$obj$$);if($user$$.email!==$obj$$.user_created)return $next$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n hu\u1ef7 l\u1ec7nh n\u00e0y");if(0==$obj$$.trang_thai&&0<$obj$$.con_lai){try{await global.getModel("assbank").sendCoinsFromSystemToAccount($obj$$.id_app,$obj$$.creator,$obj$$.con_lai,$obj$$.token,{...$obj$$,event:"SELL-CANCEL",memo:`Hu\u1ef7 l\u1ec7nh b\u00e1n. Nh\u1eadn ${$obj$$.so_luong_con_lai}${$obj$$.token} c\u00f2n l\u1ea1i`})}catch($e$$){return $next$$($e$$)}await model.updateOne({id_app:$obj$$.id_app, _id:$obj$$._id},{trang_thai:9});return $next$$("L\u1ec7nh b\u00e1n n\u00e0y \u0111\u00e3 \u0111\u01b0\u1ee3c hu\u1ef7 th\u00e0nh c\u00f4ng")}$next$$("Kh\u00f4ng th\u1ec3 hu\u1ef7 l\u1ec7nh n\u00e0y")};$contr_router$$.updating=async function($user$$,$data$$,$obj$$,$next$$){if($user$$.email!==$obj$$.user_created&&!isSupperAdmin($user$$.email.toLowerCase()))return $next$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n c\u1eadp nh\u1eadt");if(0==$obj$$.trang_thai&&0<$obj$$.con_lai&&9==$data$$.trang_thai){try{await global.getModel("assbank").sendCoinsFromSystemToAccount($obj$$.id_app, $obj$$.creator,$obj$$.con_lai,$obj$$.token,{...$obj$$.toObject(),event:"SELL-CANCEL",memo:`Hu\u1ef7 l\u1ec7nh b\u00e1n. Nh\u1eadn ${$obj$$.so_luong_con_lai}${$obj$$.token} c\u00f2n l\u1ea1i`})}catch($e$$){return $next$$($e$$)}$data$$={trang_thai:9};return $next$$(null,$data$$,$obj$$)}$next$$("Kh\u00f4ng th\u1ec3 c\u1eadp nh\u1eadt l\u1ec7nh n\u00e0y")};$contr_router$$.route()};