2025-03-10 16:40:40 -07:00
|
|
|
import { GuildMember } from "@/src/models/guildModel";
|
2025-03-29 09:48:45 -07:00
|
|
|
import { Inbox } from "@/src/models/inboxModel";
|
2025-03-13 10:46:08 -07:00
|
|
|
import { Account } from "@/src/models/loginModel";
|
2025-03-14 07:09:28 -07:00
|
|
|
import { getGuildForRequest, hasGuildPermission } from "@/src/services/guildService";
|
2025-03-10 16:40:40 -07:00
|
|
|
import { getInventory } from "@/src/services/inventoryService";
|
2025-03-13 10:46:08 -07:00
|
|
|
import { getAccountForRequest, getSuffixedName } from "@/src/services/loginService";
|
2025-03-14 07:09:28 -07:00
|
|
|
import { GuildPermission } from "@/src/types/guildTypes";
|
2025-03-10 16:40:40 -07:00
|
|
|
import { RequestHandler } from "express";
|
|
|
|
|
|
|
|
export const removeFromGuildController: RequestHandler = async (req, res) => {
|
2025-03-13 10:46:08 -07:00
|
|
|
const account = await getAccountForRequest(req);
|
2025-03-10 16:40:40 -07:00
|
|
|
const guild = await getGuildForRequest(req);
|
|
|
|
const payload = JSON.parse(String(req.body)) as IRemoveFromGuildRequest;
|
2025-03-14 07:09:28 -07:00
|
|
|
const isKick = !account._id.equals(payload.userId);
|
|
|
|
if (isKick && !(await hasGuildPermission(guild, account._id, GuildPermission.Regulator))) {
|
|
|
|
res.status(400).json("Invalid permission");
|
|
|
|
return;
|
|
|
|
}
|
2025-03-10 16:40:40 -07:00
|
|
|
|
|
|
|
const guildMember = (await GuildMember.findOne({ accountId: payload.userId, guildId: guild._id }))!;
|
|
|
|
if (guildMember.status == 0) {
|
|
|
|
const inventory = await getInventory(payload.userId);
|
|
|
|
inventory.GuildId = undefined;
|
|
|
|
|
|
|
|
// Remove clan key or blueprint from kicked member
|
2025-03-14 11:21:56 +01:00
|
|
|
const itemIndex = inventory.LevelKeys.findIndex(x => x.ItemType == "/Lotus/Types/Keys/DojoKey");
|
2025-03-10 16:40:40 -07:00
|
|
|
if (itemIndex != -1) {
|
2025-03-14 11:21:56 +01:00
|
|
|
inventory.LevelKeys.splice(itemIndex, 1);
|
2025-03-10 16:40:40 -07:00
|
|
|
} else {
|
|
|
|
const recipeIndex = inventory.Recipes.findIndex(x => x.ItemType == "/Lotus/Types/Keys/DojoKeyBlueprint");
|
|
|
|
if (recipeIndex != -1) {
|
|
|
|
inventory.Recipes.splice(itemIndex, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await inventory.save();
|
|
|
|
|
|
|
|
// TODO: Handle clan leader kicking themselves (guild should be deleted in this case, I think)
|
|
|
|
} else if (guildMember.status == 2) {
|
2025-03-29 09:48:45 -07:00
|
|
|
// Delete the inbox message for the invite
|
|
|
|
await Inbox.deleteOne({
|
|
|
|
ownerId: guildMember.accountId,
|
|
|
|
contextInfo: guild._id.toString(),
|
|
|
|
acceptAction: "GUILD_INVITE"
|
|
|
|
});
|
2025-03-10 16:40:40 -07:00
|
|
|
}
|
|
|
|
await GuildMember.deleteOne({ _id: guildMember._id });
|
|
|
|
|
2025-03-13 10:46:08 -07:00
|
|
|
guild.RosterActivity ??= [];
|
2025-03-14 07:09:28 -07:00
|
|
|
if (isKick) {
|
2025-03-24 11:32:08 -07:00
|
|
|
const kickee = (await Account.findById(payload.userId))!;
|
2025-03-13 10:46:08 -07:00
|
|
|
guild.RosterActivity.push({
|
|
|
|
dateTime: new Date(),
|
2025-03-14 07:09:28 -07:00
|
|
|
entryType: 12,
|
|
|
|
details: getSuffixedName(kickee) + "," + getSuffixedName(account)
|
2025-03-13 10:46:08 -07:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
guild.RosterActivity.push({
|
|
|
|
dateTime: new Date(),
|
2025-03-14 07:09:28 -07:00
|
|
|
entryType: 7,
|
|
|
|
details: getSuffixedName(account)
|
2025-03-13 10:46:08 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
await guild.save();
|
|
|
|
|
2025-03-10 16:40:40 -07:00
|
|
|
res.json({
|
|
|
|
_id: payload.userId,
|
|
|
|
ItemToRemove: "/Lotus/Types/Keys/DojoKey",
|
|
|
|
RecipeToRemove: "/Lotus/Types/Keys/DojoKeyBlueprint"
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IRemoveFromGuildRequest {
|
|
|
|
userId: string;
|
|
|
|
kicker?: string;
|
|
|
|
}
|