flexbiz-server
Version:
Flexible Server
16 lines (15 loc) • 6.3 kB
JavaScript
'use strict';const assinvestmentSchema=new Schema({id_app:String,so_hd:{required:!0,type:String,set:function($so_hd$$){this._previous_so_hd=this.so_hd;return $so_hd$$},trim:!0},ngay_hd:{type:Date,default:Date.now},creator:{type:String,required:!0,trim:!0},id:{type:String,trim:!0},id_product:{type:String,trim:!0},ten_san_pham:{type:String},id_period:{type:String,required:!0,trim:!0},ten_ky_han:{type:String},product_token:{type:String,lowercase:!0,trim:!0},token:{type:String,required:!0,lowercase:!0,
trim:!0},co_phan:Boolean,lai_da_tra:Number,ngay_tinh_lai_ky_truoc:{type:Date},ngay_tinh_lai_ky_tiep_theo:{type:Date},gia:{type:Number},so_luong:{type:Number,required:!0},tong_gia_tri:{type:Number},lai_du_tinh:Number,tong_gia_tri_dao_han:Number,lai_suat_cam_ket:{type:Number},ky_tinh_lai:{type:Number},lan_tinh_lai:{type:Number,default:0},ngay_phat_hanh:{type:Date},ky_han:{type:Number},ngay_dao_han:{type:Date},cho_phep_rut_von:{type:Boolean},lai_suat_truoc_han:{type:Number},tg_xu_ly_rut_von:{type:Number},
cho_phep_chuyen_nhuong:{type:Boolean},denom:{type:String,trim:!0},da_dao_han:{type:Boolean},trang_thai:{type:String,default:"1"},ty_le_hoa_hong:Number,denom_hoa_hong:String,address_hoa_hong:String,ky_tra_hoa_hong:Number,introduce_code:String,da_rut_von:Number,thong_tin_hop_dong:Schema.Types.Mixed,signature:String,exfields:Schema.Types.Mixed,status:{type:Boolean,default:!0},date_created:{type:Date,default:Date.now},date_updated:{type:Date,default:Date.now},user_created:{type:String,default:""},user_updated:{type:String,
default:""}}),model=mongoose.models.assinvestment||mongoose.model("assinvestment",assinvestmentSchema);
(global.configs||{}).createIndexes&&(assinvestmentSchema.index({id_app:1}),assinvestmentSchema.index({creator:1}),assinvestmentSchema.index({id:1}),assinvestmentSchema.index({id_product:1}),assinvestmentSchema.index({ten_san_pham:1}),assinvestmentSchema.index({product_token:1}),assinvestmentSchema.index({co_phan:1}),assinvestmentSchema.index({ngay_tinh_lai_ky_truoc:1}),assinvestmentSchema.index({ngay_tinh_lai_ky_tiep_theo:1}),assinvestmentSchema.index({id_period:1}),assinvestmentSchema.index({ten_ky_han:1}),
assinvestmentSchema.index({cho_phep_rut_von:1}),assinvestmentSchema.index({cho_phep_chuyen_nhuong:1}),assinvestmentSchema.index({da_dao_han:1}),assinvestmentSchema.index({denom:1}),assinvestmentSchema.index({token:1}),assinvestmentSchema.index({id_app:1,token:1},{unique:!0}),assinvestmentSchema.index({status:1}),assinvestmentSchema.index({user_created:1}),assinvestmentSchema.index({creator:"text",product_token:"text",token:"text",ten_san_pham:"text",ten_ky_han:"text",denom:"text"},{name:"assinvestment_index2"}),
model.on("index",async function($err$$){$err$$?console.error("AssInvestmnet index error",$err$$):console.info("AssInvestmnet indexing complete")}));
assinvestmentSchema.pre("save",function($next$$){if(!this.so_hd)return $next$$();const $self$$=this;this.isNew||this.so_hd!=this._previous_so_hd?mongoose.models.customer.countDocuments({id_app:this.id_app,so_hd:this.so_hd},($e$$,$count$$)=>{0<$count$$?$next$$(Error(`S\u1ed1 h\u1ee3p \u0111\u1ed3ng '${$self$$.so_hd}' \u0111\u00e3 t\u1ed3n t\u1ea1i`)):$next$$()}):$next$$()});
const thongke=async $p$$=>{let $withdraws$$=await global.getModel("asswithdraw").find({id_app:$p$$.id_app,token:$p$$.token,trang_thai:"5"},{so_luong:1,trang_thai:1}).lean();$p$$.da_rut_von=$withdraws$$.map($ivt$$=>$ivt$$.so_luong).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$p$$.id_period&&$p$$.so_luong&&$p$$.id_app&&(await [$p$$].asyncJoinModel2($p$$.id_app,"assperiod",{where:{id_period:"id"},fields:["ky_han","gia","lai_suat_cam_ket","denom"]}),$p$$.tong_gia_tri=($p$$.gia||0)*($p$$.so_luong||0),$p$$.lai_du_tinh=
Math.roundBy($p$$.lai_suat_cam_ket/365/100*$p$$.tong_gia_tri*$p$$.ky_han,0),$p$$.tong_gia_tri_dao_han=$p$$.tong_gia_tri+$p$$.lai_du_tinh)};assinvestmentSchema.post("find",async function($result$$){for(let $i$$=0;$i$$<$result$$.length;$i$$++){const $p$$=$result$$[$i$$];$p$$.token&&$p$$.id_app&&await thongke($p$$)}});assinvestmentSchema.post("findOne",async function($p$$){$p$$&&$p$$.token&&$p$$.id_app&&await thongke($p$$)});
model.getPercentOfSteps=$steps$$=>{let $owners$$=[];$steps$$.filter($s$$=>"4"==$s$$.trang_thai||"5"==$s$$.trang_thai).sort(($a$$,$b$$)=>$a$$.stt-$b$$.stt).forEach($s$$=>{let $owner$$={code:$s$$.code,name:$s$$.name,sl_slot:$s$$.sl_slot,gt_slot:$s$$.gt_slot,dillution:$s$$.dillution,raise:$s$$.raise,percent:$s$$.dillution||100,investment:$s$$.raise||$s$$.valuation};$owners$$.forEach($o$$=>{$o$$.percent*=1-$s$$.dillution/100});$owners$$.push($owner$$);$owners$$.forEach($o$$=>{$o$$.valuation=Math.roundBy($s$$.valuation*
$o$$.percent/100,0);$o$$.sl_cp=Math.roundBy($s$$.total_share*$o$$.percent/100,0);$o$$.sl_cp_slot=$o$$.sl_slot?$o$$.sl_cp/$o$$.sl_slot:0;$o$$.gt_cp_slot=$o$$.sl_slot?$o$$.valuation/$o$$.sl_slot:0;$o$$.pi=$o$$.investment?$o$$.valuation/$o$$.investment:0});$s$$.owners={...$owners$$}});return $owners$$};
model.getInvestmentMarketValue=async($id_app_steps$$,$frm_step_token$$)=>{console.log("get investment market value",$frm_step_token$$,$id_app_steps$$);const $invest$$=await model.findOne({id_app:$id_app_steps$$,token:$frm_step_token$$}).lean();if($invest$$){var $period$$=await global.getModel("assperiod").findOne({id_app:$id_app_steps$$,id:$invest$$.id_period}).lean();if($period$$)if($period$$.goi_dau_tu&&$period$$.frm_step_code)if($invest$$.goi_dau_tu=!0,$frm_step_token$$=await global.getModel("financialroadmap").findOne({id_app:$id_app_steps$$,
code:$invest$$.id_product.toString()}).lean()){$id_app_steps$$=model.getPercentOfSteps($frm_step_token$$.steps||[]);if($frm_step_token$$=$id_app_steps$$.find($s$$=>$s$$.code===$period$$.frm_step_code))return $frm_step_token$$.t_sl_cp_ht=$frm_step_token$$.sl_cp_slot*$invest$$.so_luong,$frm_step_token$$.t_gt_cp_ht=$frm_step_token$$.gt_cp_slot*$invest$$.so_luong,$frm_step_token$$;console.log("not find step",$period$$.frm_step_code,$id_app_steps$$)}else console.log("not find frm",$invest$$.id_product.toString(),
$id_app_steps$$);else console.log("period",$invest$$.id_period,"does not a frm",$period$$.frm_step_code);else console.log("not find period",$invest$$.id_period,$id_app_steps$$)}else console.log("not find investment",$frm_step_token$$,$id_app_steps$$)};module.exports=model;