2025-03-10 16:40:40 -07:00
|
|
|
import { Guild, GuildMember } from "@/src/models/guildModel";
|
2025-03-31 04:26:55 -07:00
|
|
|
import { deleteGuild, getGuildClient, removeDojoKeyItems } from "@/src/services/guildService";
|
|
|
|
import { addRecipes, combineInventoryChanges, getInventory } from "@/src/services/inventoryService";
|
2025-03-13 10:46:08 -07:00
|
|
|
import { getAccountForRequest, getSuffixedName } from "@/src/services/loginService";
|
2025-03-31 04:26:55 -07:00
|
|
|
import { IInventoryChanges } from "@/src/types/purchaseTypes";
|
2025-03-10 16:40:40 -07:00
|
|
|
import { RequestHandler } from "express";
|
|
|
|
import { Types } from "mongoose";
|
|
|
|
|
|
|
|
export const confirmGuildInvitationController: RequestHandler = async (req, res) => {
|
2025-03-13 10:46:08 -07:00
|
|
|
const account = await getAccountForRequest(req);
|
2025-03-31 04:26:55 -07:00
|
|
|
const invitedGuildMember = await GuildMember.findOne({
|
2025-03-13 10:46:08 -07:00
|
|
|
accountId: account._id,
|
2025-03-10 16:40:40 -07:00
|
|
|
guildId: req.query.clanId as string
|
|
|
|
});
|
2025-03-31 04:26:55 -07:00
|
|
|
if (invitedGuildMember) {
|
|
|
|
let inventoryChanges: IInventoryChanges = {};
|
2025-03-13 10:46:08 -07:00
|
|
|
|
2025-03-31 04:26:55 -07:00
|
|
|
// If this account is already in a guild, we need to do cleanup first.
|
|
|
|
const guildMember = await GuildMember.findOneAndDelete({ accountId: account._id, status: 0 });
|
|
|
|
if (guildMember) {
|
|
|
|
const inventory = await getInventory(account._id.toString(), "LevelKeys Recipes");
|
|
|
|
inventoryChanges = removeDojoKeyItems(inventory);
|
|
|
|
await inventory.save();
|
|
|
|
|
|
|
|
if (guildMember.rank == 0) {
|
|
|
|
await deleteGuild(guildMember.guildId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're sure this account is not in a guild right now, we can just proceed with the normal updates.
|
|
|
|
invitedGuildMember.status = 0;
|
|
|
|
await invitedGuildMember.save();
|
|
|
|
|
|
|
|
const inventory = await getInventory(account._id.toString(), "GuildId LevelKeys Recipes");
|
|
|
|
inventory.GuildId = new Types.ObjectId(req.query.clanId as string);
|
|
|
|
const recipeChanges = [
|
|
|
|
{
|
|
|
|
ItemType: "/Lotus/Types/Keys/DojoKeyBlueprint",
|
|
|
|
ItemCount: 1
|
|
|
|
}
|
|
|
|
];
|
|
|
|
addRecipes(inventory, recipeChanges);
|
|
|
|
combineInventoryChanges(inventoryChanges, { Recipes: recipeChanges });
|
|
|
|
await inventory.save();
|
2025-03-13 10:46:08 -07:00
|
|
|
|
2025-03-24 11:32:08 -07:00
|
|
|
const guild = (await Guild.findById(req.query.clanId as string))!;
|
2025-03-13 10:46:08 -07:00
|
|
|
|
|
|
|
guild.RosterActivity ??= [];
|
|
|
|
guild.RosterActivity.push({
|
|
|
|
dateTime: new Date(),
|
|
|
|
entryType: 6,
|
|
|
|
details: getSuffixedName(account)
|
|
|
|
});
|
|
|
|
await guild.save();
|
|
|
|
|
2025-03-10 16:40:40 -07:00
|
|
|
res.json({
|
2025-03-13 10:46:08 -07:00
|
|
|
...(await getGuildClient(guild, account._id.toString())),
|
2025-03-31 04:26:55 -07:00
|
|
|
InventoryChanges: inventoryChanges
|
2025-03-10 16:40:40 -07:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
};
|