Add getGuildForRequest
This commit is contained in:
		
							parent
							
								
									a28acd6d60
								
							
						
					
					
						commit
						41f27362d2
					
				@ -1,8 +1,6 @@
 | 
				
			|||||||
import { RequestHandler } from "express";
 | 
					import { RequestHandler } from "express";
 | 
				
			||||||
import { IDojoComponentClient } from "@/src/types/guildTypes";
 | 
					import { IDojoComponentClient } from "@/src/types/guildTypes";
 | 
				
			||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
					import { getGuildForRequest } from "@/src/services/guildService";
 | 
				
			||||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
					 | 
				
			||||||
import { Guild } from "@/src/models/guildModel";
 | 
					 | 
				
			||||||
import { Types } from "mongoose";
 | 
					import { Types } from "mongoose";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface IStartDojoRecipeRequest {
 | 
					interface IStartDojoRecipeRequest {
 | 
				
			||||||
@ -11,19 +9,8 @@ interface IStartDojoRecipeRequest {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const startDojoRecipeController: RequestHandler = async (req, res) => {
 | 
					export const startDojoRecipeController: RequestHandler = async (req, res) => {
 | 
				
			||||||
    const accountId = await getAccountIdForRequest(req);
 | 
					    const guild = await getGuildForRequest(req);
 | 
				
			||||||
    const inventory = await Inventory.findOne({ accountOwnerId: accountId });
 | 
					    // At this point, we know that a member of the guild is making this request. Assuming they are allowed to start a build.
 | 
				
			||||||
    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 request = JSON.parse(req.body.toString()) as IStartDojoRecipeRequest;
 | 
					    const request = JSON.parse(req.body.toString()) as IStartDojoRecipeRequest;
 | 
				
			||||||
    guild.DojoComponents!.push({
 | 
					    guild.DojoComponents!.push({
 | 
				
			||||||
        _id: new Types.ObjectId(),
 | 
					        _id: new Types.ObjectId(),
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										18
									
								
								src/services/guildService.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/services/guildService.ts
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user