'use strict'; /* DELETE /alliances/:id/diplomacy/:otherId/pending. The alliance that started a delayed change (cancelling a pact, * declaring war out of a pact) may take it back before it takes effect. Nothing pending: no-op. */ const { GameError } = require('../errors'); const A = require('../alliances'); const Dip = require('../diplomacy'); module.exports = function diploRevoke(ctx, { userId, allianceId, otherId }) { const { db, now } = ctx; const { alliance } = A.requireRole(db, userId, allianceId, 'diplomacy'); const other = A.getAlliance(db, otherId); if (!other) throw new GameError('not_found', { what: 'alliance' }); const rel = A.getRelation(db, alliance.id, other.id); if (!rel || !rel.pending_to) return { changed: false }; if (rel.pending_by !== alliance.id) throw new GameError('forbidden', { need: 'pending change started by your alliance' }); db.run("UPDATE events SET status = 'cancelled', done_at = ? WHERE id = ? AND status = 'pending'", now, rel.pending_event); db.run('UPDATE alliance_relations SET pending_to = NULL, pending_at = NULL, pending_by = NULL, pending_event = NULL WHERE a_id = ? AND b_id = ?', rel.a_id, rel.b_id); for (const [me, o2] of [[alliance, other], [other, alliance]]) { A.notifyAlliance(ctx, me.id, 'diplomacy_cancel_revoked', { allianceId: me.id, other: Dip.brief(o2), state: rel.state }); } return { changed: true, state: rel.state }; };