chore: delete guild when founding warlord leaves #1371
@ -1,7 +1,7 @@
 | 
				
			|||||||
import { GuildMember } from "@/src/models/guildModel";
 | 
					import { GuildMember } from "@/src/models/guildModel";
 | 
				
			||||||
import { Inbox } from "@/src/models/inboxModel";
 | 
					import { Inbox } from "@/src/models/inboxModel";
 | 
				
			||||||
import { Account } from "@/src/models/loginModel";
 | 
					import { Account } from "@/src/models/loginModel";
 | 
				
			||||||
import { getGuildForRequest, hasGuildPermission } from "@/src/services/guildService";
 | 
					import { deleteGuild, getGuildForRequest, hasGuildPermission } from "@/src/services/guildService";
 | 
				
			||||||
import { getInventory } from "@/src/services/inventoryService";
 | 
					import { getInventory } from "@/src/services/inventoryService";
 | 
				
			||||||
import { getAccountForRequest, getSuffixedName } from "@/src/services/loginService";
 | 
					import { getAccountForRequest, getSuffixedName } from "@/src/services/loginService";
 | 
				
			||||||
import { GuildPermission } from "@/src/types/guildTypes";
 | 
					import { GuildPermission } from "@/src/types/guildTypes";
 | 
				
			||||||
@ -18,50 +18,54 @@ export const removeFromGuildController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const guildMember = (await GuildMember.findOne({ accountId: payload.userId, guildId: guild._id }))!;
 | 
					    const guildMember = (await GuildMember.findOne({ accountId: payload.userId, guildId: guild._id }))!;
 | 
				
			||||||
    if (guildMember.status == 0) {
 | 
					    if (guildMember.rank == 0) {
 | 
				
			||||||
        const inventory = await getInventory(payload.userId);
 | 
					        await deleteGuild(guild._id);
 | 
				
			||||||
        inventory.GuildId = undefined;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Remove clan key or blueprint from kicked member
 | 
					 | 
				
			||||||
        const itemIndex = inventory.LevelKeys.findIndex(x => x.ItemType == "/Lotus/Types/Keys/DojoKey");
 | 
					 | 
				
			||||||
        if (itemIndex != -1) {
 | 
					 | 
				
			||||||
            inventory.LevelKeys.splice(itemIndex, 1);
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            const recipeIndex = inventory.Recipes.findIndex(x => x.ItemType == "/Lotus/Types/Keys/DojoKeyBlueprint");
 | 
					 | 
				
			||||||
            if (recipeIndex != -1) {
 | 
					 | 
				
			||||||
                inventory.Recipes.splice(recipeIndex, 1);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        await inventory.save();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // TODO: Handle clan leader kicking themselves (guild should be deleted in this case, I think)
 | 
					 | 
				
			||||||
    } else if (guildMember.status == 2) {
 | 
					 | 
				
			||||||
        // Delete the inbox message for the invite
 | 
					 | 
				
			||||||
        await Inbox.deleteOne({
 | 
					 | 
				
			||||||
            ownerId: guildMember.accountId,
 | 
					 | 
				
			||||||
            contextInfo: guild._id.toString(),
 | 
					 | 
				
			||||||
            acceptAction: "GUILD_INVITE"
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    await GuildMember.deleteOne({ _id: guildMember._id });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    guild.RosterActivity ??= [];
 | 
					 | 
				
			||||||
    if (isKick) {
 | 
					 | 
				
			||||||
        const kickee = (await Account.findById(payload.userId))!;
 | 
					 | 
				
			||||||
        guild.RosterActivity.push({
 | 
					 | 
				
			||||||
            dateTime: new Date(),
 | 
					 | 
				
			||||||
            entryType: 12,
 | 
					 | 
				
			||||||
            details: getSuffixedName(kickee) + "," + getSuffixedName(account)
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        guild.RosterActivity.push({
 | 
					        if (guildMember.status == 0) {
 | 
				
			||||||
            dateTime: new Date(),
 | 
					            const inventory = await getInventory(payload.userId);
 | 
				
			||||||
            entryType: 7,
 | 
					            inventory.GuildId = undefined;
 | 
				
			||||||
            details: getSuffixedName(account)
 | 
					
 | 
				
			||||||
        });
 | 
					            // Remove clan key or blueprint from kicked member
 | 
				
			||||||
 | 
					            const itemIndex = inventory.LevelKeys.findIndex(x => x.ItemType == "/Lotus/Types/Keys/DojoKey");
 | 
				
			||||||
 | 
					            if (itemIndex != -1) {
 | 
				
			||||||
 | 
					                inventory.LevelKeys.splice(itemIndex, 1);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                const recipeIndex = inventory.Recipes.findIndex(
 | 
				
			||||||
 | 
					                    x => x.ItemType == "/Lotus/Types/Keys/DojoKeyBlueprint"
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
 | 
					                if (recipeIndex != -1) {
 | 
				
			||||||
 | 
					                    inventory.Recipes.splice(recipeIndex, 1);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            await inventory.save();
 | 
				
			||||||
 | 
					        } else if (guildMember.status == 2) {
 | 
				
			||||||
 | 
					            // Delete the inbox message for the invite
 | 
				
			||||||
 | 
					            await Inbox.deleteOne({
 | 
				
			||||||
 | 
					                ownerId: guildMember.accountId,
 | 
				
			||||||
 | 
					                contextInfo: guild._id.toString(),
 | 
				
			||||||
 | 
					                acceptAction: "GUILD_INVITE"
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        await GuildMember.deleteOne({ _id: guildMember._id });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        guild.RosterActivity ??= [];
 | 
				
			||||||
 | 
					        if (isKick) {
 | 
				
			||||||
 | 
					            const kickee = (await Account.findById(payload.userId))!;
 | 
				
			||||||
 | 
					            guild.RosterActivity.push({
 | 
				
			||||||
 | 
					                dateTime: new Date(),
 | 
				
			||||||
 | 
					                entryType: 12,
 | 
				
			||||||
 | 
					                details: getSuffixedName(kickee) + "," + getSuffixedName(account)
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            guild.RosterActivity.push({
 | 
				
			||||||
 | 
					                dateTime: new Date(),
 | 
				
			||||||
 | 
					                entryType: 7,
 | 
				
			||||||
 | 
					                details: getSuffixedName(account)
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        await guild.save();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    await guild.save();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    res.json({
 | 
					    res.json({
 | 
				
			||||||
        _id: payload.userId,
 | 
					        _id: payload.userId,
 | 
				
			||||||
 | 
				
			|||||||
@ -9,10 +9,17 @@ import { Ship } from "@/src/models/shipModel";
 | 
				
			|||||||
import { Stats } from "@/src/models/statsModel";
 | 
					import { Stats } from "@/src/models/statsModel";
 | 
				
			||||||
import { GuildMember } from "@/src/models/guildModel";
 | 
					import { GuildMember } from "@/src/models/guildModel";
 | 
				
			||||||
import { Leaderboard } from "@/src/models/leaderboardModel";
 | 
					import { Leaderboard } from "@/src/models/leaderboardModel";
 | 
				
			||||||
 | 
					import { deleteGuild } from "@/src/services/guildService";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const deleteAccountController: RequestHandler = async (req, res) => {
 | 
					export const deleteAccountController: RequestHandler = async (req, res) => {
 | 
				
			||||||
    const accountId = await getAccountIdForRequest(req);
 | 
					    const accountId = await getAccountIdForRequest(req);
 | 
				
			||||||
    // TODO: Handle the account being the creator of a guild
 | 
					
 | 
				
			||||||
 | 
					    // If account is the founding warlord of a guild, delete that guild as well.
 | 
				
			||||||
 | 
					    const guildMember = await GuildMember.findOne({ accountId, rank: 0, status: 0 });
 | 
				
			||||||
 | 
					    if (guildMember) {
 | 
				
			||||||
 | 
					        await deleteGuild(guildMember.guildId);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await Promise.all([
 | 
					    await Promise.all([
 | 
				
			||||||
        Account.deleteOne({ _id: accountId }),
 | 
					        Account.deleteOne({ _id: accountId }),
 | 
				
			||||||
        GuildMember.deleteMany({ accountId: accountId }),
 | 
					        GuildMember.deleteMany({ accountId: accountId }),
 | 
				
			||||||
 | 
				
			|||||||
@ -22,6 +22,7 @@ import { logger } from "../utils/logger";
 | 
				
			|||||||
import { config } from "./configService";
 | 
					import { config } from "./configService";
 | 
				
			||||||
import { Account } from "../models/loginModel";
 | 
					import { Account } from "../models/loginModel";
 | 
				
			||||||
import { getRandomInt } from "./rngService";
 | 
					import { getRandomInt } from "./rngService";
 | 
				
			||||||
 | 
					import { Inbox } from "../models/inboxModel";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
 | 
					export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
 | 
				
			||||||
    const accountId = await getAccountIdForRequest(req);
 | 
					    const accountId = await getAccountIdForRequest(req);
 | 
				
			||||||
@ -360,3 +361,14 @@ export const removePigmentsFromGuildMembers = async (guildId: string | Types.Obj
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const deleteGuild = async (guildId: Types.ObjectId): Promise<void> => {
 | 
				
			||||||
 | 
					    await Guild.deleteOne({ _id: guildId });
 | 
				
			||||||
 | 
					    await GuildMember.deleteMany({ guildId });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // If guild sent any invites, delete those inbox messages as well.
 | 
				
			||||||
 | 
					    await Inbox.deleteMany({
 | 
				
			||||||
 | 
					        contextInfo: guildId.toString(),
 | 
				
			||||||
 | 
					        acceptAction: "GUILD_INVITE"
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user