const util = require('util'); const utils = require('./utils'); const db = require("./db"); const log = require("./log"); const C = require("./C"); class BoxRepairer { constructor(boxOrder, orderMgr) { this.boxOrder = boxOrder; this.orderMgr = orderMgr; } async repairPaidOrder() { await this.internalRepairPaidOrder(); this.orderMgr.removePendingOrder(this.boxOrder.getOrderId()); } async internalRepairPaidOrder() { const logClass = 'repairPaidOrder'; const {err, boxDb} = await this.getBoxDb(logClass); if (err || !boxDb) { utils.throwError(util.format('%s box not found orderDb:%s err:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), err ) ); return; } log.info(util.format('%s begin orderDb:%s boxDb:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), JSON.stringify(boxDb) ) ); if (boxDb['state'] == C.BOX_STATE_PAID) { await this.boxOrder.setDone(logClass); } else { let err = await this.boxOrder.setBoxPaidState(logClass, boxDb); if (!err) { await this.boxOrder.setDone(logClass); } } log.info(util.format('%s end orderDb:%s boxDb:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), JSON.stringify(boxDb) ) ); } async repairExpiredOrder() { await this.internalRepairExpiredOrder(); this.orderMgr.removePendingOrder(this.boxOrder.getOrderId()); } async internalRepairExpiredOrder() { const logClass = 'repairExpiredOrder'; const {err, boxDb} = await this.getBoxDb(logClass); if (err || !boxDb) { log.warning(util.format('%s box not found orderDb:%s err:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), err ) ); return; } log.info(util.format('%s begin orderDb:%s boxDb:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), JSON.stringify(boxDb) )); if (!boxDb['order_valid']) { await this.boxOrder.setDone(logClass); } else { let err = await this.boxOrder.setBoxOrderInvalid(logClass, boxDb); if (!err) { await this.boxOrder.setDone(logClass); } } log.info(util.format('%s end orderDb:%s boxDb:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), JSON.stringify(boxDb) ) ); } async repairFailOrder() { await this.internalRepairFailOrder(); this.orderMgr.removePendingOrder(this.boxOrder.getOrderId()); } async internalRepairFailOrder() { const logClass = 'repairFailOrder'; const {err, boxDb} = await this.boxOrder.getBoxDb(logClass); if (err || !boxDb) { log.warning(util.format('%s box not found orderDb:%s err:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), err )); return; } log.info(util.format('%s begin orderDb:%s boxDb:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), JSON.stringify(boxDb) ) ); if (!boxDb['order_valid']) { await this.boxOrder.setDone(logClass); } else { let err = await this.boxOrder.setBoxOrderInvalid(logClass, boxDb); if (!err) { await this.boxOrder.setDone(logClass); } } log.info(util.format('%s end orderDb:%s boxDb:%s', logClass, JSON.stringify(this.boxOrder.getOrderDb()), JSON.stringify(boxDb) ) ); } } exports.BoxRepairer = BoxRepairer;