Fix woka endpoints on pusher

This commit is contained in:
Alexis Faizeau 2022-02-24 14:10:23 +01:00
parent c37046e138
commit 586e829e2e
4 changed files with 27 additions and 77 deletions

View File

@ -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);
});*/
}
}

View File

@ -7,9 +7,9 @@ class AdminWokaService implements WokaServiceInterface {
/**
* Returns the list of all available Wokas for the current user.
*/
getWokaList(token: string): Promise<WokaList | undefined> {
getWokaList(roomId: string, token: string): Promise<WokaList | undefined> {
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<WokaDetailsResult | undefined> {
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();

View File

@ -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<WokaList | undefined> {
async getWokaList(roomId: string, token: string): Promise<WokaList | undefined> {
const wokaData: WokaList = await require("../../data/woka.json");
if (!wokaData) {
return undefined;

View File

@ -4,15 +4,5 @@ export interface WokaServiceInterface {
/**
* Returns the list of all available Wokas for the current user.
*/
getWokaList(token: string): Promise<WokaList | 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 (and the user should be redirected to Woka choice page!)
*/
//fetchWokaDetails(textureIds: string[]): Promise<WokaDetailsResult | undefined>;
getWokaList(roomId: string, token: string): Promise<WokaList | undefined>;
}