2024-05-04 15:42:25 +02:00
|
|
|
import { RequestHandler } from "express";
|
|
|
|
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
|
|
|
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
|
|
|
|
import { Guild } from "@/src/models/guildModel";
|
|
|
|
import { IGuild, ICreateGuildRequest } from "@/src/types/guildTypes";
|
|
|
|
|
|
|
|
const createGuildController: RequestHandler = async (req, res) => {
|
2024-05-06 15:19:42 +02:00
|
|
|
const payload: ICreateGuildRequest = getJSONfromString(req.body.toString());
|
2024-05-04 15:42:25 +02:00
|
|
|
|
|
|
|
// Create guild on database
|
2024-05-06 15:19:42 +02:00
|
|
|
const guild = new Guild({
|
2024-05-04 15:42:25 +02:00
|
|
|
Name: payload.guildName
|
|
|
|
} satisfies IGuild);
|
|
|
|
await guild.save();
|
|
|
|
|
|
|
|
// Update inventory
|
2024-05-06 15:19:42 +02:00
|
|
|
const inventory = await Inventory.findOne({ accountOwnerId: req.query.accountId });
|
2024-05-04 15:42:25 +02:00
|
|
|
if (inventory) {
|
|
|
|
// Set GuildId
|
|
|
|
inventory.GuildId = guild._id;
|
|
|
|
|
|
|
|
// Give clan key
|
|
|
|
inventory.LevelKeys ??= [];
|
|
|
|
inventory.LevelKeys.push({
|
|
|
|
ItemType: "/Lotus/Types/Keys/DojoKey",
|
|
|
|
ItemCount: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
await inventory.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(guild);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { createGuildController };
|