From 8a29f06207243f8557fc10ebea7e87b544a19aab Mon Sep 17 00:00:00 2001 From: Sainan Date: Sun, 23 Mar 2025 09:06:28 -0700 Subject: [PATCH] chore: use inventory projection for updateTheme (#1302) Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1302 --- src/controllers/api/updateThemeController.ts | 28 +++++++++----------- src/services/inventoryService.ts | 15 +---------- src/types/requestTypes.ts | 6 ----- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/controllers/api/updateThemeController.ts b/src/controllers/api/updateThemeController.ts index ccb4ab575..ce31d27d2 100644 --- a/src/controllers/api/updateThemeController.ts +++ b/src/controllers/api/updateThemeController.ts @@ -1,25 +1,23 @@ import { getAccountIdForRequest } from "@/src/services/loginService"; import { getJSONfromString } from "@/src/helpers/stringHelpers"; -import { updateTheme } from "@/src/services/inventoryService"; -import { IThemeUpdateRequest } from "@/src/types/requestTypes"; import { RequestHandler } from "express"; +import { getInventory } from "@/src/services/inventoryService"; -const updateThemeController: RequestHandler = async (request, response) => { +export const updateThemeController: RequestHandler = async (request, response) => { const accountId = await getAccountIdForRequest(request); - const body = String(request.body); + const data = getJSONfromString(String(request.body)); - try { - const json = getJSONfromString(body); - if (typeof json !== "object") { - throw new Error("Invalid data format"); - } - - await updateTheme(json, accountId); - } catch (err) { - console.error("Error parsing JSON data:", err); - } + const inventory = await getInventory(accountId, "ThemeStyle ThemeBackground ThemeSounds"); + if (data.Style) inventory.ThemeStyle = data.Style; + if (data.Background) inventory.ThemeBackground = data.Background; + if (data.Sounds) inventory.ThemeSounds = data.Sounds; + await inventory.save(); response.json({}); }; -export { updateThemeController }; +interface IThemeUpdateRequest { + Style?: string; + Background?: string; + Sounds?: string; +} diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index ef99f53a0..ffa184846 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -29,11 +29,7 @@ import { ICrewShipWeaponClient } from "@/src/types/inventoryTypes/inventoryTypes"; import { IGenericUpdate, IUpdateNodeIntrosResponse } from "../types/genericUpdate"; -import { - IMissionInventoryUpdateRequest, - IThemeUpdateRequest, - IUpdateChallengeProgressRequest -} from "../types/requestTypes"; +import { IMissionInventoryUpdateRequest, IUpdateChallengeProgressRequest } from "../types/requestTypes"; import { logger } from "@/src/utils/logger"; import { convertInboxMessage, fromStoreItem, getExalted, getKeyChainItems } from "@/src/services/itemDataService"; import { @@ -891,15 +887,6 @@ export const updateGeneric = async (data: IGenericUpdate, accountId: string): Pr }; }; -export const updateTheme = async (data: IThemeUpdateRequest, accountId: string): Promise => { - const inventory = await getInventory(accountId); - if (data.Style) inventory.ThemeStyle = data.Style; - if (data.Background) inventory.ThemeBackground = data.Background; - if (data.Sounds) inventory.ThemeSounds = data.Sounds; - - await inventory.save(); -}; - export const addEquipment = ( inventory: TInventoryDatabaseDocument, category: TEquipmentKey, diff --git a/src/types/requestTypes.ts b/src/types/requestTypes.ts index f9df3ff88..78e43c5cb 100644 --- a/src/types/requestTypes.ts +++ b/src/types/requestTypes.ts @@ -19,12 +19,6 @@ import { ICollectibleEntry } from "./inventoryTypes/inventoryTypes"; -export interface IThemeUpdateRequest { - Style?: string; - Background?: string; - Sounds?: string; -} - export interface IAffiliationChange { Tag: string; Standing: number;