fix: 完善工单

This commit is contained in:
chaosBreaking
2019-09-08 21:47:21 +08:00
parent df8a430d63
commit a88ec3c759
4 changed files with 52 additions and 23 deletions

View File

@@ -1,18 +1,31 @@
'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
};
async function handleTicket (ticket, handler) {
function createTicket (name, data) {
const type = name2type[name];
const status = 'created';
const { userId, handler, action, coinId, option } = data;
return new Ticket({
status, type, userId, handler, action, option, coinId
});
}
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(handler);
ticket = await ticket.next(handlerId);
if (status === 'processing') {
await dealTicket(ticket, user);
await ticket.next(handler);
await ticket.next(handlerId);
return ticket;
}
return null;
@@ -20,22 +33,18 @@ async function handleTicket (ticket, handler) {
async function dealTicket (ticket, user) {
if (ticket.type === 0) {
// 提现
dealWithdraw(ticket, user);
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)
}
function createTicket (name, data) {
const type = name2type[name];
const status = 'created';
const { userId, handler, action, option } = data;
return new Ticket({
status, type, userId, handler, action, option
});
}
module.exports = {
createTicket,