'use strict'; /* DELETE /trade/offers/:id: take your open offer back; the escrow returns to the POP. */ const { GameError } = require('../errors'); const ctxOf = require('../military/ctx'); const T = require('../trade'); module.exports = function tradeCancel(ctx0, { userId, offerId }) { const ctx = ctxOf(ctx0); const { db, now } = ctx; const o = T.getOffer(db, offerId); if (!o || T.userOfOperator(db, o.operator_id) !== userId) throw new GameError('not_found', { what: 'offer' }); if (o.status !== 'open') throw new GameError('offer_closed', { status: o.status }); T.closeOffer(ctx, o, 'cancelled', now); return { offerId: o.id, status: 'cancelled', returned: { resource: o.give_res, amount: o.give_amount } }; };