From a00858b0370e48d5178b2c7d43e5b5d2fef4f3d0 Mon Sep 17 00:00:00 2001 From: zkldi Date: Tue, 28 Sep 2021 22:51:34 +0100 Subject: [PATCH] Add a utility endpoint for a chart's parent folders. --- .../_game/_playtype/charts/_chartID/router.ts | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/server/src/server/router/api/v1/games/_game/_playtype/charts/_chartID/router.ts b/server/src/server/router/api/v1/games/_game/_playtype/charts/_chartID/router.ts index 274467e7a..4d27e95ff 100644 --- a/server/src/server/router/api/v1/games/_game/_playtype/charts/_chartID/router.ts +++ b/server/src/server/router/api/v1/games/_game/_playtype/charts/_chartID/router.ts @@ -1,5 +1,6 @@ import { Router } from "express"; import db from "external/mongo/db"; +import { FilterQuery } from "mongodb"; import { SYMBOL_TachiData } from "lib/constants/tachi"; import CreateLogCtx from "lib/logger/logger"; import { SearchUsersRegExp } from "lib/search/search"; @@ -7,7 +8,7 @@ import { IsString } from "utils/misc"; import { ParseStrPositiveNonZeroInt } from "utils/string-checks"; import { GetUsersWithIDs } from "utils/user"; import { ValidateAndGetChart } from "./middleware"; -import { FormatChart } from "tachi-common"; +import { FormatChart, FolderDocument } from "tachi-common"; const logger = CreateLogCtx(__filename); @@ -49,6 +50,44 @@ router.get("/", async (req, res) => { }); }); +/** + * Returns any folders that contain this chart. + * + * @param inactive - Also include inactive folders. + * + * @name GET /api/v1/games/:game/:playtype/charts/:chartID/folders + */ +router.get("/folders", async (req, res) => { + const chart = req[SYMBOL_TachiData]!.chartDoc!; + + const folderIDs = await db["folder-chart-lookup"].find( + { + chartID: chart.chartID, + }, + { + projection: { + folderID: 1, + }, + } + ); + + const query: FilterQuery = { + folderID: { $in: folderIDs.map((e) => e.folderID) }, + }; + + if (!req.query.inactive) { + query.inactive = false; + } + + const folders = await db.folders.find(query); + + return res.status(200).json({ + success: true, + description: `Found ${folders.length} folders that contain this chart.`, + body: folders, + }); +}); + /** * Returns the total amount of unique players that have played this chart. *