UNPKG

flexbiz-server

Version:

Flexible Server

11 lines (10 loc) 4.61 kB
const model=global.getModel("introducecode"),asskey=global.getModel("asskey"),controller=require("../../controllers/controller"),User=global.getModel("user"),Introducer=global.getModel("introducer"),Trangthai=global.getModel("trangthai"),utils=require("../../libs/utils"); module.exports=function($router$$){(new controller($router$$,model,"introducecode",{sort:{date_created:-1},notNeedRight:!0,unique:["introduce_code"],onView:async($user$$,$items$$,$next$$)=>{if(await model.countDocuments({user_created:$user$$.email,id_app:$user$$.current_id_app})===0)try{const $obj$$={user_created:$user$$.email,id_app:$user$$.current_id_app,so_lan_su_dung:0};for(;;){let $code$$=global.getModel("introducecode").generateCode();if(!await model.findOne({id_app:$user$$.current_id_app,introduce_code:$code$$})){$obj$$.introduce_code= $code$$;break}}let $ws$$=await asskey.findOne({user_created:$user$$.email,id_app:$user$$.current_id_app,wallet_type:8,asset:{$nin:["BTC","ETH"]}}).lean();$ws$$||($ws$$=await global.getModel("asskey").createWallet($user$$.current_id_app,$user$$.email,"V\u00ed nh\u1eadn th\u01b0\u1edfng",8),global.getModel("app").emitEvent($user$$.current_id_app,"blockchain.wallet.changed",{sender:$user$$.email},null,[$user$$.email],!1,!1));$obj$$.wallet_address_receive_reward=$ws$$.address;let $new_code$$=await model.create($obj$$); $items$$.push(utils.convertObjectIdsToStrings($new_code$$.toObject()))}catch($e$$){Logger.error($e$$)}await Promise.all($items$$.map($item$$=>(async()=>{var $so_lan_dau_tu_ws$$=await asskey.findOne({user_created:$item$$.user_created,id_app:$item$$.id_app,wallet_type:8,asset:{$nin:["BTC","ETH"]}}).lean();$so_lan_dau_tu_ws$$||($so_lan_dau_tu_ws$$=await global.getModel("asskey").createWallet($item$$.id_app,$item$$.user_created,"V\u00ed nh\u1eadn th\u01b0\u1edfng",8),global.getModel("app").emitEvent($item$$.id_app, "blockchain.wallet.changed",{sender:$item$$.user_created},null,[$item$$.user_created],!1,!1),await model.updateOne({id_app:$user$$.current_id_app,_id:$item$$._id},{wallet_address_receive_reward:$so_lan_dau_tu_ws$$.address}),$item$$.wallet_address_receive_reward=$so_lan_dau_tu_ws$$.address);$item$$.so_user_da_gioi_thieu=await global.getModel("introducer").countDocuments({id_app:$user$$.current_id_app,introduce_code:$item$$.introduce_code});$so_lan_dau_tu_ws$$=await global.getModel("assinvestment").find({id_app:$user$$.current_id_app, introduce_code:$item$$.introduce_code,trang_thai:{$in:["1","5","9"]}}).lean();$item$$.so_lan_dau_tu=$so_lan_dau_tu_ws$$.length;$item$$.so_user_da_dau_tu=(await $so_lan_dau_tu_ws$$.asyncGroupBy(["user_created"],["so_luong"])).length})()));await $items$$.asyncJoinModel2($user$$.current_id_app,Introducer,{where:"introduce_code",fields:["be_introduced",{be_introduced_date:"date_created"}]});await $items$$.asyncJoinModel2(null,User,{where:{be_introduced:"email"},fields:[{be_introduced_name:"name"}]}); await $items$$.asyncJoinModel2(null,Trangthai,{where:$item$$=>({ma_trang_thai:$item$$.trang_thai,ma_ct:"INTRODUCECODE"}),fields:["ten_trang_thai","color"]});$next$$(null,$items$$)},onCreating:async($expiry_date_user$$,$data$$,$next$$,$code$jscomp$2_options$$={})=>{$code$jscomp$2_options$$=$code$jscomp$2_options$$.action==="import"&&$data$$.user_created?$data$$.user_created:$expiry_date_user$$.email;if(await model.countDocuments({user_created:$code$jscomp$2_options$$,id_app:$expiry_date_user$$.current_id_app})> 0)return $next$$("Kh\u00f4ng th\u1ec3 t\u1ea1o th\u00eam m\u00e3 g\u1edbi thi\u1ec7u m\u1edbi cho user "+$code$jscomp$2_options$$);if(!$data$$.introduce_code)for(;;)if($code$jscomp$2_options$$=global.getModel("introducecode").generateCode(),!await model.findOne({id_app:$expiry_date_user$$.current_id_app,introduce_code:$code$jscomp$2_options$$})){$data$$.introduce_code=$code$jscomp$2_options$$;break}$expiry_date_user$$=new Date;$expiry_date_user$$.setDate($expiry_date_user$$.getDate()+1825);$data$$.expiry_date= $expiry_date_user$$;$next$$(null,$data$$)},onUpdating:async($user$$,$data$$,$obj$$,$next$$)=>{if($user$$.email!==$obj$$.user_created)return $next$$("B\u1ea1n kh\u00f4ng th\u1ec3 s\u1eeda m\u00e3 gi\u1edbi thi\u1ec7u n\u00e0y");$data$$.introduce_code=void 0;$data$$.so_lan_su_dung=void 0;$next$$(null,$data$$,$obj$$)},onDeleting:async($obj$jscomp$24_user$$,$data$$,$next$$)=>{$obj$jscomp$24_user$$=await model.findOne({_id:$data$$._id});if(!$obj$jscomp$24_user$$||$obj$jscomp$24_user$$.so_lan_su_dung)return $next$$("Kh\u00f4ng th\u1ec3 xo\u00e1 m\u00e3 gi\u1edbi thi\u1ec7u n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng"); $next$$(null,$data$$)}})).route()};