'use strict'; /* DELETE /diplomacy/proposals/:proposalId. The proposing alliance withdraws it. Needs `diplomacy`. */ const { GameError } = require('../errors'); const A = require('../alliances'); module.exports = function diploWithdraw(ctx, { userId, proposalId }) { const { db, now } = ctx; const p = db.get('SELECT * FROM alliance_proposals WHERE id = ?', proposalId); if (!p) throw new GameError('not_found', { what: 'proposal' }); A.requireRole(db, userId, p.from_id, 'diplomacy'); if (p.status === 'withdrawn') return { changed: false }; if (p.status !== 'pending') throw new GameError('conflict', { what: 'proposal is ' + p.status }); db.run("UPDATE alliance_proposals SET status = 'withdrawn', answered_at = ? WHERE id = ?", now, p.id); return { changed: true }; };