feat: implement startDojoRecipeController to place new rooms in Dojo #273
@ -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";
 | 
				
			||||||
| 
							
								
									
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
					|||||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
import { Guild } from "@/src/models/guildModel";
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
import { Types } from "mongoose";
 | 
					import { Types } from "mongoose";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface IStartDojoRecipeRequest {
 | 
					interface IStartDojoRecipeRequest {
 | 
				
			||||||
@ -11,19 +9,8 @@ interface IStartDojoRecipeRequest {
 | 
				
			|||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const startDojoRecipeController: RequestHandler = async (req, res) => {
 | 
					export const startDojoRecipeController: RequestHandler = async (req, res) => {
 | 
				
			||||||
    const accountId = await getAccountIdForRequest(req);
 | 
					    const guild = await getGuildForRequest(req);
 | 
				
			||||||
| 
							
								
									
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
					|||||||
    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.
 | 
				
			||||||
| 
							
								
									
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
					|||||||
    if (!inventory) {
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
        res.status(400).end();
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
        return;
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
    }
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
    const guildId = req.query.guildId as string;
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
    if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
        res.status(400).end();
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
        return;
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
    }
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
    // Verified that a member of the guild is making this request. Assuming they are allowed to start a build.
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
    const guild = (await Guild.findOne({ _id: guildId }))!;
 | 
					 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
    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(),
 | 
				
			||||||
 | 
				
			|||||||
| 
						
							
	
	
	
	
	
	
	
	 could use getInventory() could use getInventory() 
			
			
		could use getInventory() could use getInventory() 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		perhaps this could be a getGuild() function, seems like we would need that often perhaps this could be a getGuild() function, seems like we would need that often 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		i dont like the non-null assertion operator here. i dont like the non-null assertion operator here. 
I would prefer coding it such that it is checked before null before, so this is not needed 
			
			
		 | 
				|||||||
							
								
								
									
										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;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
could use getInventory()
could use getInventory()
perhaps this could be a getGuild() function, seems like we would need that often
perhaps this could be a getGuild() function, seems like we would need that often
i dont like the non-null assertion operator here.
I would prefer coding it such that it is checked before null before, so this is not needed
i dont like the non-null assertion operator here.
I would prefer coding it such that it is checked before null before, so this is not needed