fix: fund模型修改和创建api

This commit is contained in:
chaosBreaking
2019-09-07 12:01:57 +08:00
parent 8a493f3b10
commit 54674dd060
6 changed files with 276 additions and 104 deletions

View File

@@ -2,6 +2,8 @@
const mongoose = require('mongoose');
const ASSETTYPES = ['gin', 'vic', 'usdtBTC', 'usdtETH', 'usdtTRX'];
const { createAction } = require('../action/action.handler');
const { createNewFund } = require('./fund.handler');
const FundSchema = new mongoose.Schema({
userId: {
@@ -15,15 +17,30 @@ const FundSchema = new mongoose.Schema({
'usdtTRX': ''
}
},
// fund 为充值矿晶的一系列数值,用于计算收益
fund: [{
// fund 为充值矿晶的一系列数值,用于计算收益
// fund = 矿晶单位KG
fundList: [{}],
// fundSum, 用户充值的总usdt数
fundSum: {
type: Number
}],
},
// asset表示用户的财产包括VIC/团队奖励的USDT
asset: {
type: {},
default: {
'vic': 0,
'usdt': 0
}
},
// 团队总充值资产
groupFundSum: {
type: Number
},
asset: {
type: {}
lastMine: {
type: String
},
lastDeposit: {
type: String
},
option: {
type: {}
@@ -37,7 +54,45 @@ const FundSchema = new mongoose.Schema({
const FundModel = mongoose.model('Fund', FundSchema);
/**
[incFund] : 向用户添加矿晶在用户充值USDT时调用
* 当一个用户充值,
• 4% 给上面第一个 金 或 银 或 铜 会员
• 3%
• 如果直接上家是金,就全给他;
• 如果直接上家不是金,就平分 3% 给上线所有银会员
• 2% 给上线所有金会员平分
*/
FundModel.prototype.incFund = async function (amount, option = {}) {
if (!amount || !Number.isFinite(amount) || amount <= 0) return null;
// 1.注入资金
const newFund = createNewFund(amount);
const res = await FundModel.findByIdAndUpdate(this.id, {
$push: {
fundList: newFund
},
$inc: {
fundSum: amount
},
$set: {
lastDeposit: new Date().toISOString()
}
// eslint-disable-next-line no-unused-vars
}, { ...option, returnOriginal: false }).exec().catch(err => null);
// 2.记录action
const action = createAction(0, {
userId: this.userId,
amount,
tokenType: 'vic', // 目前设定只能挖出矿晶vic
remain: this.asset
});
await action.save().catch(err => {
throw new Error('充值行为记录失败', err.errmsg);
});
// 3.奖励团队
return res;
};
FundModel.prototype.increase = async function (assetName, amount, option = {}) {
if (!assetName || !amount || amount <= 0 || !Number.isFinite(amount) || !ASSETTYPES.includes(assetName)) return null;
const asset = 'asset.' + assetName;
@@ -45,6 +100,7 @@ FundModel.prototype.increase = async function (assetName, amount, option = {}) {
$inc: {
[asset]: amount
}
// eslint-disable-next-line no-unused-vars
}, { ...option, returnOriginal: false }).exec().catch(err => null);
if (!res || !res.asset) throw new Error('用户资产修改[increase]失败!');
if (res.asset[assetName] < 0) throw new Error('Insufficient funds');
@@ -59,6 +115,7 @@ FundModel.prototype.decrease = async function (assetName, amount, option) {
$inc: {
[asset]: amount
}
// eslint-disable-next-line no-unused-vars
}, { ...option, returnOriginal: false }).exec().catch(err => null);
if (!res || !res.asset) throw new Error('用户资产修改[decrease]失败!');
if (res.asset[assetName] < 0) throw new Error('Insufficient funds');