SpaceNinjaServer/src/controllers/api/createGuildController.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import { RequestHandler } from "express";
2024-05-28 13:45:06 +02:00
import { getAccountIdForRequest } from "@/src/services/loginService";
import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { Guild, GuildMember } from "@/src/models/guildModel";
2025-03-30 22:47:58 +02:00
import { createUniqueClanName, getGuildClient } from "@/src/services/guildService";
import { addRecipes, getInventory } from "@/src/services/inventoryService";
2025-01-03 09:06:50 +01:00
export const createGuildController: RequestHandler = async (req, res) => {
2024-05-28 13:45:06 +02:00
const accountId = await getAccountIdForRequest(req);
const payload = getJSONfromString<ICreateGuildRequest>(String(req.body));
// 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: accountId,
guildId: guild._id,
status: 0,
rank: 0
});
2025-03-30 22:47:58 +02:00
const inventory = await getInventory(accountId, "GuildId Recipes");
inventory.GuildId = guild._id;
addRecipes(inventory, [
{
ItemType: "/Lotus/Types/Keys/DojoKeyBlueprint",
ItemCount: 1
}
]);
await inventory.save();
res.json({
...(await getGuildClient(guild, accountId)),
InventoryChanges: {
Recipes: [
{
ItemType: "/Lotus/Types/Keys/DojoKeyBlueprint",
ItemCount: 1
}
]
}
});
};
2025-01-03 09:06:50 +01:00
interface ICreateGuildRequest {
guildName: string;
}