Add getGuildForRequest

This commit is contained in:
Sainan 2024-06-06 15:03:05 +02:00
parent a28acd6d60
commit 41f27362d2
2 changed files with 21 additions and 16 deletions

View File

@ -1,8 +1,6 @@
import { RequestHandler } from "express";
import { IDojoComponentClient } from "@/src/types/guildTypes";
import { getAccountIdForRequest } from "@/src/services/loginService";
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
import { Guild } from "@/src/models/guildModel";
import { getGuildForRequest } from "@/src/services/guildService";
import { Types } from "mongoose";
interface IStartDojoRecipeRequest {
@ -11,19 +9,8 @@ interface IStartDojoRecipeRequest {
}
export const startDojoRecipeController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const inventory = await Inventory.findOne({ accountOwnerId: accountId });
if (!inventory) {
res.status(400).end();
return;
}
const guildId = req.query.guildId as string;
if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
res.status(400).end();
return;
}
// Verified that a member of the guild is making this request. Assuming they are allowed to start a build.
const guild = (await Guild.findOne({ _id: guildId }))!;
const guild = await getGuildForRequest(req);
// At this point, we know that a member of the guild is making this request. Assuming they are allowed to start a build.
const request = JSON.parse(req.body.toString()) as IStartDojoRecipeRequest;
guild.DojoComponents!.push({
_id: new Types.ObjectId(),

View File

@ -0,0 +1,18 @@
import { Request } from "express";
import { getAccountIdForRequest } from "@/src/services/loginService";
import { getInventory } from "@/src/services/inventoryService";
import { Guild } from "@/src/models/guildModel";
export const getGuildForRequest = async (req: Request) => {
const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId);
const guildId = req.query.guildId as string;
if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
throw new Error("Account is not in the guild that it has sent a request for");
}
const guild = await Guild.findOne({ _id: guildId });
if (!guild) {
throw new Error("Account thinks it is a in guild that doesn't exist");
}
return guild;
};