2025-04-01 02:28:24 -07:00
|
|
|
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
2025-03-10 16:40:40 -07:00
|
|
|
import { Guild, GuildMember } from "@/src/models/guildModel";
|
2025-04-01 02:28:24 -07:00
|
|
|
import { Account } from "@/src/models/loginModel";
|
2025-04-27 12:38:55 -07:00
|
|
|
import {
|
|
|
|
deleteGuild,
|
|
|
|
getGuildClient,
|
|
|
|
giveClanKey,
|
|
|
|
hasGuildPermission,
|
|
|
|
removeDojoKeyItems
|
|
|
|
} from "@/src/services/guildService";
|
|
|
|
import { getInventory } from "@/src/services/inventoryService";
|
2025-04-01 02:28:24 -07:00
|
|
|
import { getAccountForRequest, getAccountIdForRequest, getSuffixedName } from "@/src/services/loginService";
|
|
|
|
import { GuildPermission } from "@/src/types/guildTypes";
|
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";
|
|
|
|
|
2025-04-05 06:51:37 -07:00
|
|
|
// GET request: A player accepting an invite they got in their inbox.
|
|
|
|
export const confirmGuildInvitationGetController: 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-04-01 02:28:24 -07:00
|
|
|
if (invitedGuildMember && invitedGuildMember.status == 2) {
|
2025-03-31 04:26:55 -07:00
|
|
|
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();
|
|
|
|
|
2025-04-01 02:28:24 -07:00
|
|
|
// Remove pending applications for this account
|
|
|
|
await GuildMember.deleteMany({ accountId: account._id, status: 1 });
|
|
|
|
|
|
|
|
// Update inventory of new member
|
2025-03-31 04:26:55 -07:00
|
|
|
const inventory = await getInventory(account._id.toString(), "GuildId LevelKeys Recipes");
|
|
|
|
inventory.GuildId = new Types.ObjectId(req.query.clanId as string);
|
2025-04-27 12:38:55 -07:00
|
|
|
giveClanKey(inventory, inventoryChanges);
|
2025-03-31 04:26:55 -07:00
|
|
|
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
|
|
|
|
2025-04-01 02:28:24 -07:00
|
|
|
// Add join to clan log
|
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();
|
|
|
|
}
|
|
|
|
};
|
2025-04-05 06:51:37 -07:00
|
|
|
|
|
|
|
// POST request: Clan representative accepting invite(s).
|
|
|
|
export const confirmGuildInvitationPostController: RequestHandler = async (req, res) => {
|
|
|
|
const accountId = await getAccountIdForRequest(req);
|
|
|
|
const guild = (await Guild.findById(req.query.clanId as string, "Ranks RosterActivity"))!;
|
|
|
|
if (!(await hasGuildPermission(guild, accountId, GuildPermission.Recruiter))) {
|
|
|
|
res.status(400).json("Invalid permission");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const payload = getJSONfromString<{ userId: string }>(String(req.body));
|
|
|
|
const filter: { accountId?: string; status: number } = { status: 1 };
|
|
|
|
if (payload.userId != "all") {
|
|
|
|
filter.accountId = payload.userId;
|
|
|
|
}
|
|
|
|
const guildMembers = await GuildMember.find(filter);
|
|
|
|
const newMembers: string[] = [];
|
|
|
|
for (const guildMember of guildMembers) {
|
|
|
|
guildMember.status = 0;
|
|
|
|
guildMember.RequestMsg = undefined;
|
|
|
|
guildMember.RequestExpiry = undefined;
|
|
|
|
await guildMember.save();
|
|
|
|
|
|
|
|
// Remove other pending applications for this account
|
|
|
|
await GuildMember.deleteMany({ accountId: guildMember.accountId, status: 1 });
|
|
|
|
|
|
|
|
// Update inventory of new member
|
2025-04-27 12:38:55 -07:00
|
|
|
const inventory = await getInventory(guildMember.accountId.toString(), "GuildId LevelKeys Recipes");
|
2025-04-05 06:51:37 -07:00
|
|
|
inventory.GuildId = new Types.ObjectId(req.query.clanId as string);
|
2025-04-27 12:38:55 -07:00
|
|
|
giveClanKey(inventory);
|
2025-04-05 06:51:37 -07:00
|
|
|
await inventory.save();
|
|
|
|
|
|
|
|
// Add join to clan log
|
|
|
|
const account = (await Account.findOne({ _id: guildMember.accountId }))!;
|
|
|
|
guild.RosterActivity ??= [];
|
|
|
|
guild.RosterActivity.push({
|
|
|
|
dateTime: new Date(),
|
|
|
|
entryType: 6,
|
|
|
|
details: getSuffixedName(account)
|
|
|
|
});
|
|
|
|
|
|
|
|
newMembers.push(account._id.toString());
|
|
|
|
}
|
|
|
|
await guild.save();
|
|
|
|
res.json({
|
|
|
|
NewMembers: newMembers
|
|
|
|
});
|
|
|
|
};
|