'use strict'; /* POST /alliances/:id/transfer. The leader hands leadership to another member and becomes an officer. */ const { GameError } = require('../errors'); const A = require('../alliances'); module.exports = function allianceTransfer(ctx, { userId, allianceId, operatorId }) { const { db } = ctx; const { op, alliance } = A.requireRole(db, userId, allianceId, 'transfer'); if (operatorId === op.id) throw new GameError('validation_failed', { errors: [{ path: 'operatorId', message: 'you are already the leader' }] }); const t = A.membership(db, operatorId); if (!t || t.alliance_id !== alliance.id) throw new GameError('not_found', { what: 'member' }); db.run("UPDATE alliance_members SET role = 'officer' WHERE operator_id = ?", op.id); // before promoting: one leader at a time db.run("UPDATE alliance_members SET role = 'leader' WHERE operator_id = ?", operatorId); A.refreshCap(db, alliance); const target = db.get('SELECT name FROM operators WHERE id = ?', operatorId); A.notifyAlliance(ctx, alliance.id, 'alliance_leader', { allianceId: alliance.id, operatorId, name: target.name, from: op.name }); return { changed: true, leaderId: operatorId }; };