import type { RequestHandler } from "express";
import { getAccountForRequest, getBuildLabel, type TAccountDocument } from "../../services/loginService.ts";
import { getJSONfromString } from "../../helpers/stringHelpers.ts";
import { Guild, GuildMember } from "../../models/guildModel.ts";
import { createUniqueClanName, getGuildClient, giveClanKey } from "../../services/guildService.ts";
import { getInventory } from "../../services/inventoryService.ts";
import type { IInventoryChanges } from "../../types/purchaseTypes.ts";
import { sendWsBroadcastTo } from "../../services/wsService.ts";
import type { IGuildClient } from "../../types/guildTypes.ts";
export const createGuildGetController: RequestHandler = async (req, res) => {
const account = await getAccountForRequest(req);
const buildLabel = getBuildLabel(req, account);
const guildName = decodeURIComponent(req.query.guildName as string);
const response = await processCreateGuildRequest(account, buildLabel, { guildName });
res.json(response);
};
export const createGuildPostController: RequestHandler = async (req, res) => {
const account = await getAccountForRequest(req);
const buildLabel = getBuildLabel(req, account);
const payload = getJSONfromString<ICreateGuildRequest>(String(req.body));
const response = await processCreateGuildRequest(account, buildLabel, payload);
res.json(response);
};
const processCreateGuildRequest = async (
account: TAccountDocument,
buildLabel: string,
payload: ICreateGuildRequest
): Promise<ICreateGuildResponse> => {
const inventory = await getInventory(account._id, "GuildId LevelKeys Recipes");
if (inventory.GuildId) {
const guild = await Guild.findById(inventory.GuildId);
if (guild) {
return {
...(await getGuildClient(guild, account, buildLabel))
};
}
}
// Remove pending applications for this account
await GuildMember.deleteMany({ accountId: account._id, status: 1 });
// Create guild on database
const guild = new Guild({
Name: await createUniqueClanName(payload.guildName)
});
await guild.save();
// Create guild member on database
await GuildMember.insertOne({
accountId: account._id,
guildId: guild._id,
status: 0,
rank: 0
});
inventory.GuildId = guild._id;
const inventoryChanges: IInventoryChanges = {};
giveClanKey(inventory, inventoryChanges);
await inventory.save();
sendWsBroadcastTo(account._id.toString(), { update_inventory: true });
return {
...(await getGuildClient(guild, account, buildLabel)),
InventoryChanges: inventoryChanges
};
};
interface ICreateGuildRequest {
guildName: string;
}
interface ICreateGuildResponse extends IGuildClient {
InventoryChanges?: IInventoryChanges;
}
import type { RequestHandler } from "express";
import { getAccountForRequest, getBuildLabel, type TAccountDocument } from "../../services/loginService.ts";
import { getJSONfromString } from "../../helpers/stringHelpers.ts";
import { Guild, GuildMember } from "../../models/guildModel.ts";
import { createUniqueClanName, getGuildClient, giveClanKey } from "../../services/guildService.ts";
import { getInventory } from "../../services/inventoryService.ts";
import type { IInventoryChanges } from "../../types/purchaseTypes.ts";
import { sendWsBroadcastTo } from "../../services/wsService.ts";
import type { IGuildClient } from "../../types/guildTypes.ts";
export const createGuildGetController: RequestHandler = async (req, res) => {
const account = await getAccountForRequest(req);
const buildLabel = getBuildLabel(req, account);
const guildName = decodeURIComponent(req.query.guildName as string);
const response = await processCreateGuildRequest(account, buildLabel, { guildName });
res.json(response);
};
export const createGuildPostController: RequestHandler = async (req, res) => {
const account = await getAccountForRequest(req);
const buildLabel = getBuildLabel(req, account);
const payload = getJSONfromString<ICreateGuildRequest>(String(req.body));
const response = await processCreateGuildRequest(account, buildLabel, payload);
res.json(response);
};
const processCreateGuildRequest = async (
account: TAccountDocument,
buildLabel: string,
payload: ICreateGuildRequest
): Promise<ICreateGuildResponse> => {
const inventory = await getInventory(account._id, "GuildId LevelKeys Recipes");
if (inventory.GuildId) {
const guild = await Guild.findById(inventory.GuildId);
if (guild) {
return {
...(await getGuildClient(guild, account, buildLabel))
};
}
}
// Remove pending applications for this account
await GuildMember.deleteMany({ accountId: account._id, status: 1 });
// Create guild on database
const guild = new Guild({
Name: await createUniqueClanName(payload.guildName)
});
await guild.save();
// Create guild member on database
await GuildMember.insertOne({
accountId: account._id,
guildId: guild._id,
status: 0,
rank: 0
});
inventory.GuildId = guild._id;
const inventoryChanges: IInventoryChanges = {};
giveClanKey(inventory, inventoryChanges);
await inventory.save();
sendWsBroadcastTo(account._id.toString(), { update_inventory: true });
return {
...(await getGuildClient(guild, account, buildLabel)),
InventoryChanges: inventoryChanges
};
};
interface ICreateGuildRequest {
guildName: string;
}
interface ICreateGuildResponse extends IGuildClient {
InventoryChanges?: IInventoryChanges;
}