diff --git a/src/controllers/custom/deleteAccountController.ts b/src/controllers/custom/deleteAccountController.ts index 5b52f46f..ff0a8b18 100644 --- a/src/controllers/custom/deleteAccountController.ts +++ b/src/controllers/custom/deleteAccountController.ts @@ -1,5 +1,5 @@ import type { RequestHandler } from "express"; -import { getAccountIdForRequest } from "../../services/loginService.ts"; +import { getAccountForRequest } from "../../services/loginService.ts"; import { Account, Ignore } from "../../models/loginModel.ts"; import { Inbox } from "../../models/inboxModel.ts"; import { Inventory } from "../../models/inventoryModels/inventoryModel.ts"; @@ -12,33 +12,44 @@ import { Leaderboard } from "../../models/leaderboardModel.ts"; import { deleteGuild } from "../../services/guildService.ts"; import { Friendship } from "../../models/friendModel.ts"; import { sendWsBroadcastTo } from "../../services/wsService.ts"; +import { config } from "../../services/configService.ts"; +import { saveConfig } from "../../services/configWriterService.ts"; export const deleteAccountController: RequestHandler = async (req, res) => { - const accountId = await getAccountIdForRequest(req); + const account = await getAccountForRequest(req); + + // If this account is an admin, remove it from administratorNames + if (config.administratorNames) { + const adminIndex = config.administratorNames.indexOf(account.DisplayName); + if (adminIndex != -1) { + config.administratorNames.splice(adminIndex, 1); + await saveConfig(); + } + } // If account is the founding warlord of a guild, delete that guild as well. - const guildMember = await GuildMember.findOne({ accountId, rank: 0, status: 0 }); + const guildMember = await GuildMember.findOne({ accountId: account._id, rank: 0, status: 0 }); if (guildMember) { await deleteGuild(guildMember.guildId); } await Promise.all([ - Account.deleteOne({ _id: accountId }), - Friendship.deleteMany({ owner: accountId }), - Friendship.deleteMany({ friend: accountId }), - GuildMember.deleteMany({ accountId: accountId }), - Ignore.deleteMany({ ignorer: accountId }), - Ignore.deleteMany({ ignoree: accountId }), - Inbox.deleteMany({ ownerId: accountId }), - Inventory.deleteOne({ accountOwnerId: accountId }), - Leaderboard.deleteMany({ ownerId: accountId }), - Loadout.deleteOne({ loadoutOwnerId: accountId }), - PersonalRooms.deleteOne({ personalRoomsOwnerId: accountId }), - Ship.deleteMany({ ShipOwnerId: accountId }), - Stats.deleteOne({ accountOwnerId: accountId }) + Account.deleteOne({ _id: account._id }), + Friendship.deleteMany({ owner: account._id }), + Friendship.deleteMany({ friend: account._id }), + GuildMember.deleteMany({ accountId: account._id }), + Ignore.deleteMany({ ignorer: account._id }), + Ignore.deleteMany({ ignoree: account._id }), + Inbox.deleteMany({ ownerId: account._id }), + Inventory.deleteOne({ accountOwnerId: account._id }), + Leaderboard.deleteMany({ ownerId: account._id }), + Loadout.deleteOne({ loadoutOwnerId: account._id }), + PersonalRooms.deleteOne({ personalRoomsOwnerId: account._id }), + Ship.deleteMany({ ShipOwnerId: account._id }), + Stats.deleteOne({ accountOwnerId: account._id }) ]); - sendWsBroadcastTo(accountId, { logged_out: true }); + sendWsBroadcastTo(account._id.toString(), { logged_out: true }); res.end(); };