From 586e829e2e433738b7f8f1b95da276349bdb71d6 Mon Sep 17 00:00:00 2001 From: Alexis Faizeau Date: Thu, 24 Feb 2022 14:10:23 +0100 Subject: [PATCH] Fix woka endpoints on pusher --- pusher/src/Controller/WokaListController.ts | 43 ++++++++++--------- pusher/src/Services/AdminWokaService.ts | 47 +-------------------- pusher/src/Services/LocalWokaService.ts | 2 +- pusher/src/Services/WokaServiceInterface.ts | 12 +----- 4 files changed, 27 insertions(+), 77 deletions(-) diff --git a/pusher/src/Controller/WokaListController.ts b/pusher/src/Controller/WokaListController.ts index 0ce10966..e4e563f5 100644 --- a/pusher/src/Controller/WokaListController.ts +++ b/pusher/src/Controller/WokaListController.ts @@ -1,13 +1,33 @@ -import { hasToken } from "../Middleware/HasToken"; import { BaseHttpController } from "./BaseHttpController"; import { wokaService } from "../Services/WokaService"; +import * as tg from "generic-type-guard"; export class WokaListController extends BaseHttpController { routes() { // eslint-disable-next-line @typescript-eslint/no-misused-promises - this.app.get("/woka-list", {}, async (req, res) => { + this.app.get("/woka/list/:roomId", {}, async (req, res) => { const token = req.header("Authorization"); - const wokaList = await wokaService.getWokaList(token); + + if (!token) { + res.status(401).send("Undefined authorization header"); + return; + } + + const isParameters = new tg.IsInterface() + .withProperties({ + roomId: tg.isString, + }) + .withOptionalProperties({ + messages: tg.isArray(tg.isUnknown), + }) + .get(); + + if (!isParameters(req.path_parameters)) { + return res.status(400).send("Unknown parameters"); + } + + const roomId = req.path_parameters.roomId; + const wokaList = await wokaService.getWokaList(roomId, token); if (!wokaList) { return res.status(500).send("Error on getting woka list"); @@ -15,22 +35,5 @@ export class WokaListController extends BaseHttpController { return res.status(200).json(wokaList); }); - - // eslint-disable-next-line @typescript-eslint/no-misused-promises - /*this.app.post("/woka-details", async (req, res) => { - const body = await req.json(); - if (!body || !body.textureIds) { - return res.status(400); - } - - const textureIds = body.textureIds; - const wokaDetails = await wokaService.fetchWokaDetails(textureIds); - - if (!wokaDetails) { - return res.json({ details: [] }); - } - - return res.json(wokaDetails); - });*/ } } diff --git a/pusher/src/Services/AdminWokaService.ts b/pusher/src/Services/AdminWokaService.ts index a14458bc..acc10151 100644 --- a/pusher/src/Services/AdminWokaService.ts +++ b/pusher/src/Services/AdminWokaService.ts @@ -7,9 +7,9 @@ class AdminWokaService implements WokaServiceInterface { /** * Returns the list of all available Wokas for the current user. */ - getWokaList(token: string): Promise { + getWokaList(roomId: string, token: string): Promise { return axios - .get(`${ADMIN_API_URL}/api/woka-list/${token}`, { + .get(`${ADMIN_API_URL}/api/woka/list/${roomId}/${token}`, { headers: { Authorization: `${ADMIN_API_TOKEN}` }, }) .then((res) => { @@ -23,49 +23,6 @@ class AdminWokaService implements WokaServiceInterface { return undefined; }); } - - /** - * Returns the URL of all the images for the given texture ids. - * - * Key: texture id - * Value: URL - * - * If one of the textures cannot be found, undefined is returned - */ - /*fetchWokaDetails(textureIds: string[]): Promise { - return axios - .post( - `${ADMIN_API_URL}/api/woka-details`, - { - textureIds, - }, - { - headers: { Authorization: `${ADMIN_API_TOKEN}` }, - } - ) - .then((res) => { - if (isWokaDetailsResult(res.data)) { - throw new Error("Bad response format provided by woka detail endpoint"); - } - - const result: WokaDetailsResult = res.data; - if (result.length !== textureIds.length) { - return undefined; - } - - for (const detail of result) { - if (!detail.texture) { - return undefined; - } - } - - return res.data; - }) - .catch((err) => { - console.error(`Cannot get woka details from admin API with ids: ${textureIds}`, err); - return undefined; - }); - }*/ } export const adminWokaService = new AdminWokaService(); diff --git a/pusher/src/Services/LocalWokaService.ts b/pusher/src/Services/LocalWokaService.ts index 0f96fc3c..931ff4b3 100644 --- a/pusher/src/Services/LocalWokaService.ts +++ b/pusher/src/Services/LocalWokaService.ts @@ -5,7 +5,7 @@ class LocalWokaService implements WokaServiceInterface { /** * Returns the list of all available Wokas & Woka Parts for the current user. */ - async getWokaList(token: string): Promise { + async getWokaList(roomId: string, token: string): Promise { const wokaData: WokaList = await require("../../data/woka.json"); if (!wokaData) { return undefined; diff --git a/pusher/src/Services/WokaServiceInterface.ts b/pusher/src/Services/WokaServiceInterface.ts index 7165a209..47d3b8bd 100644 --- a/pusher/src/Services/WokaServiceInterface.ts +++ b/pusher/src/Services/WokaServiceInterface.ts @@ -4,15 +4,5 @@ export interface WokaServiceInterface { /** * Returns the list of all available Wokas for the current user. */ - getWokaList(token: string): Promise; - - /** - * Returns the URL of all the images for the given texture ids. - * - * Key: texture id - * Value: URL - * - * If one of the textures cannot be found, undefined is returned (and the user should be redirected to Woka choice page!) - */ - //fetchWokaDetails(textureIds: string[]): Promise; + getWokaList(roomId: string, token: string): Promise; }