chain-client/src/controllers/workflow.controller.ts
2023-04-26 16:23:18 +08:00

135 lines
4.5 KiB
TypeScript

import BaseController, { ROLE_ANON } from 'common/base.controller'
import { ZError } from 'common/ZError'
import { role, router } from 'decorators/router'
import { getSignature, decrypt } from '@wecom/crypto'
import { TaskQueue } from 'queue/task.queue'
import { RequestTask, TaskTypeMap } from 'models/RequestTask'
import { BlockChain } from 'chain/BlockChain'
import { ChainTask } from 'models/ChainTask'
import { isObjectId } from 'utils/string.util'
class WorkFlowController extends BaseController {
@role(ROLE_ANON)
@router('get /workflow/notify')
async wxNotifyCheck(req, res) {
const token = process.env.WX_TOKEN
const aesKey = process.env.WX_AES_KEY
let { msg_signature, timestamp, nonce, echostr } = req.params
const signature = getSignature(token, timestamp, nonce, echostr)
if (msg_signature !== signature) {
throw new ZError(10, 'sign check failed')
}
const { message, id } = decrypt(aesKey, echostr)
res.send(message)
}
@role(ROLE_ANON)
@router('post /workflow/notify')
async flowNotify(req, res) {
let { msg_signature, timestamp, nonce, xml } = req.params
const token = process.env.WX_TOKEN
const aesKey = process.env.WX_AES_KEY
const signature = getSignature(token, timestamp, nonce, xml.Encrypt)
if (msg_signature !== signature) {
throw new ZError(10, 'sign check failed')
}
// const { message, id } = decrypt(aesKey, xml.Encrypt)
// let parser = new XMLParser()
// let jsonData = parser.parse(message)
// let spStatus = jsonData.xml?.ApprovalInfo?.SpStatus
// if (spStatus === TaskStatus.PASS) {
// let spNo = jsonData.xml?.ApprovalInfo?.SpNo
// if (spNo) {
// new TaskQueue().addTaskToQueue(spNo)
// }
// }
res.send('success')
}
@role(ROLE_ANON)
@router('get /workflow/task/:id')
async info(req, res) {
const { id } = req.params
if (!id) {
throw new ZError(10, '参数错误')
}
if (!isObjectId(id)) {
throw new ZError(11, '参数错误')
}
const chainTask = await ChainTask.findById(id)
if (!chainTask) {
throw new ZError(12, '任务未找到')
}
const taskObj = {
id: chainTask.id,
name: chainTask.name,
desc: chainTask.desc,
starterName: chainTask.starterName,
}
let requestTasks = await RequestTask.find({ chainTaskId: id })
if (requestTasks.length === 0) {
throw new ZError(13, '链请求任务未找到')
}
// 内容脱敏
let tasks = requestTasks.map(o => {
return { scheduleId: o.scheduleId, reqDatas: o.reqDatas }
})
let address = process.env.CHAIN_WALLET_ADDRESS
let types = Object.fromEntries(TaskTypeMap)
return {
chainTask: taskObj,
requestTasks: tasks,
address,
types,
}
}
@role(ROLE_ANON)
@router('get /workflow/update_required')
async updateRequired(req, res) {
let result = await new BlockChain().walletReactor.updateRequired(1)
return result
}
@role(ROLE_ANON)
@router('post /workflow/update_required')
async execUpdateRequired(req, res) {
let data = {
scheduleId: '0xa5c35368cd44dbe805a4595d6813ed3afefa1bf667209dc8d63f99cdec117f58',
targets: ['0xc195196351566d2c4e13563C4492fB0BdB7894Fb'],
values: ['0'],
datas: ['0xba51a6df0000000000000000000000000000000000000000000000000000000000000001'],
predecessor: '0x0000000000000000000000000000000000000000000000000000000000000000',
salt: '0x39383830353131363736333036',
}
let result = await new BlockChain().walletReactor.executeSchedule(data)
return result
}
@role(ROLE_ANON)
@router('get /workflow/test')
async test(req, res) {
// let file_path = '/Users/zhl/Documents/workspace/tools/excel2json/test.xlsx'
// let fileId = 'WWME_g-oYEAAAzSUkPNpznkoGbgD2f1bDCA.xlsx'
// await new WechatWorkService().fetchFile(fileId)
// console.log('11')
let spNo = '202304260007'
new TaskQueue().addTaskToQueue(spNo)
// let task = await ChainTask.findById('642fe42611845ce0e1def316')
// for (let tid of task.tasks) {
// let subTask = await RequestTask.findById(tid)
// let { scheduleId } = new BlockChain().walletReactor.genOperation(subTask)
// subTask.scheduleId = scheduleId
// await subTask.save()
// new ChainQueue().addTaskToQueue(subTask)
// }
// let { id } = req.params
// let record = await RequestTask.findById(id)
// if (record) {
// let res = await new BlockChain().walletReactor.executeSchedule(record)
// return res
// }
return {}
}
}