| 
									
										
										
										
											2023-06-04 03:06:22 +02:00
										 |  |  | /* eslint-disable @typescript-eslint/no-misused-promises */ | 
					
						
							|  |  |  | import { toInventoryResponse } from "@/src/helpers/inventoryHelpers"; | 
					
						
							| 
									
										
										
										
											2023-12-14 17:34:15 +01:00
										 |  |  | import { Inventory } from "@/src/models/inventoryModels/inventoryModel"; | 
					
						
							| 
									
										
										
										
											2023-05-23 20:53:26 -04:00
										 |  |  | import { Request, RequestHandler, Response } from "express"; | 
					
						
							| 
									
										
										
										
											2024-05-15 21:55:59 +02:00
										 |  |  | import { config } from "@/src/services/configService"; | 
					
						
							| 
									
										
										
										
											2024-02-12 14:31:28 +01:00
										 |  |  | import allMissions from "@/static/fixed_responses/allMissions.json"; | 
					
						
							|  |  |  | import allQuestKeys from "@/static/fixed_responses/allQuestKeys.json"; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:10:05 +02:00
										 |  |  | import allShipDecorations from "@/static/fixed_responses/allShipDecorations.json"; | 
					
						
							|  |  |  | import allFlavourItems from "@/static/fixed_responses/allFlavourItems.json"; | 
					
						
							| 
									
										
										
										
											2023-12-14 17:34:15 +01:00
										 |  |  | import { ILoadoutDatabase } from "@/src/types/saveLoadoutTypes"; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:10:05 +02:00
										 |  |  | import { IShipInventory, IFlavourItem } from "@/src/types/inventoryTypes/inventoryTypes"; | 
					
						
							| 
									
										
										
										
											2023-05-19 15:22:48 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-04 03:06:22 +02:00
										 |  |  | const inventoryController: RequestHandler = async (request: Request, response: Response) => { | 
					
						
							| 
									
										
										
										
											2023-05-23 20:42:06 -04:00
										 |  |  |     const accountId = request.query.accountId; | 
					
						
							| 
									
										
										
										
											2023-06-04 03:06:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!accountId) { | 
					
						
							|  |  |  |         response.status(400).json({ error: "accountId was not provided" }); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 13:58:43 +01:00
										 |  |  |     const inventory = await Inventory.findOne({ accountOwnerId: accountId }) | 
					
						
							|  |  |  |         .populate<{ | 
					
						
							|  |  |  |             LoadOutPresets: ILoadoutDatabase; | 
					
						
							|  |  |  |         }>("LoadOutPresets") | 
					
						
							|  |  |  |         .populate<{ Ships: IShipInventory }>("Ships", "-ShipInteriorColors"); | 
					
						
							| 
									
										
										
										
											2023-06-04 03:06:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!inventory) { | 
					
						
							|  |  |  |         response.status(400).json({ error: "inventory was undefined" }); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 14:49:45 +01:00
										 |  |  |     //TODO: make a function that converts from database representation to client
 | 
					
						
							| 
									
										
										
										
											2023-06-04 03:06:22 +02:00
										 |  |  |     const inventoryJSON = inventory.toJSON(); | 
					
						
							| 
									
										
										
										
											2024-02-18 13:58:43 +01:00
										 |  |  |     console.log(inventoryJSON.Ships); | 
					
						
							| 
									
										
										
										
											2023-06-04 03:06:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 13:20:07 +02:00
										 |  |  |     const inventoryResponse = toInventoryResponse(inventoryJSON); | 
					
						
							| 
									
										
										
										
											2023-06-04 03:06:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 23:37:51 +02:00
										 |  |  |     if (config.infiniteResources) { | 
					
						
							|  |  |  |         inventoryResponse.RegularCredits = 999999999; | 
					
						
							|  |  |  |         inventoryResponse.TradesRemaining = 999999999; | 
					
						
							|  |  |  |         inventoryResponse.PremiumCreditsFree = 999999999; | 
					
						
							|  |  |  |         inventoryResponse.PremiumCredits = 999999999; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 23:50:38 +02:00
										 |  |  |     if (config.unlockAllMissions) { | 
					
						
							|  |  |  |         inventoryResponse.Missions = allMissions; | 
					
						
							|  |  |  |         inventoryResponse.NodeIntrosCompleted.push("TeshinHardModeUnlocked"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-12 14:31:28 +01:00
										 |  |  |     if (config.unlockAllQuests) inventoryResponse.QuestKeys = allQuestKeys; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:10:05 +02:00
										 |  |  |     if (config.unlockAllShipDecorations) inventoryResponse.ShipDecorations = allShipDecorations; | 
					
						
							|  |  |  |     if (config.unlockAllFlavourItems) inventoryResponse.FlavourItems = allFlavourItems satisfies IFlavourItem[]; | 
					
						
							| 
									
										
										
										
											2024-02-18 13:58:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 13:20:07 +02:00
										 |  |  |     response.json(inventoryResponse); | 
					
						
							| 
									
										
										
										
											2023-05-19 15:22:48 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 00:20:49 -03:00
										 |  |  | export { inventoryController }; |