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

@@ -4,9 +4,11 @@ const Ticket = require('./ticket.model');
const handleTicket = require('./ticket.handler');
module.exports = {
// 处理工单请求的api
async reqHandleTicket (req, res) {
const { handler } = res.local.handler;
const { handlerId } = res.local.handler;
const { id } = req.body;
// 存在性检查
const ticket = await Ticket.findById(id).exec().catch(err => {
mylog.error(err);
return null;
@@ -15,10 +17,12 @@ module.exports = {
code: 1,
msg: 'unknow ticket'
});
handleTicket(ticket, handler).then(data => {
// 调用处理函数
handleTicket(ticket, handlerId).then(data => {
return res.json({
code: 0,
msg: 'success'
msg: 'success',
data: JSON.stringify(data)
});
}).catch(err => {
return res.json({
@@ -28,8 +32,8 @@ module.exports = {
});
},
async getTicketList (req, res) {
const { pageSize, pageIndex } = req.query;
return Ticket.find().skip((pageIndex - 1) * pageSize).limit(pageSize).then(data => {
const { pageSize = 0, pageIndex = 1} = req.query;
return Ticket.find().skip((+pageIndex - 1) * +pageSize).limit(+pageSize).then(data => {
return res.json({
code: 0,
data

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,

View File

@@ -11,6 +11,9 @@ const TicketSchema = new mongoose.Schema({
// 订单类型
type: String,
},
coinId: {
type: String
},
amount: {
type: Number
},