fix: 完善工单
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user