Files
vic-server-mongo/src/modules/ticket/ticket.handler.js
chaosBreaking d5e5f727da targetAddress
2019-09-30 11:35:52 +08:00

52 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const Ticket = require('./ticket.model');
const User = require('../user/user.model');
const Fund = require('../fund/fund.model');
const { sendUSDT2User, sendVIC2User } = require('../../common/rootWallet');
const name2type = {
withdraw: 0
};
function createTicket (name, data) {
const type = name2type[name];
const status = 'created';
const { userId, handler, action, coinId, option, targetAddress } = data;
return new Ticket({
status, type, userId, handler, action, option, coinId, targetAddress
});
}
async function handleTicket (ticket, handlerId) {
// 所有工单在此处理只要状态未完结status !== 'finished',就可以多次调用改变状态。
const { userId, status } = ticket;
const user = await User.findById(userId).exec();
if (!user) return null;
ticket = await ticket.next(handlerId);
if (status === 'processing') {
await dealTicket(ticket, user);
await ticket.next(handlerId);
return ticket;
}
return null;
}
async function dealTicket (ticket, user) {
if (ticket.type === 0) {
// 提现
return await dealWithdraw(ticket, user);
}
}
async function dealWithdraw (ticket, user) {
// userId, amount: +amount, action, targetAddress
const { userId, amount, targetAddress, coinId } = ticket;
const fund = await Fund.findOne({ userId }).exec();
const { path } = fund.tokenAddress['usdtETH'];
return coinId === 'usdt' ? sendUSDT2User(targetAddress, amount, {path}) : sendVIC2User(targetAddress, amount)
}
module.exports = {
createTicket,
handleTicket
};