diff --git a/back/src/Controller/MapController.ts b/back/src/Controller/MapController.ts index 50e47a9d..15b35a58 100644 --- a/back/src/Controller/MapController.ts +++ b/back/src/Controller/MapController.ts @@ -2,6 +2,8 @@ import {OK} from "http-status-codes"; import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js"; import {BaseController} from "./BaseController"; +import {parse} from "query-string"; +import {adminApi} from "../Services/AdminApi"; //todo: delete this export class MapController extends BaseController{ @@ -9,26 +11,42 @@ export class MapController extends BaseController{ constructor(private App : TemplatedApp) { super(); this.App = App; - this.getStartMap(); + this.getMapUrl(); } // Returns a map mapping map name to file name of the map - getStartMap() { - this.App.options("/start-map", (res: HttpResponse, req: HttpRequest) => { + getMapUrl() { + this.App.options("/map", (res: HttpResponse, req: HttpRequest) => { this.addCorsHeaders(res); res.end(); }); - this.App.get("/start-map", (res: HttpResponse, req: HttpRequest) => { + this.App.get("/map", (res: HttpResponse, req: HttpRequest) => { this.addCorsHeaders(res); - const url = req.getHeader('host').replace('api.', 'maps.') + URL_ROOM_STARTED; - res.writeStatus("200 OK").end(JSON.stringify({ - mapUrlStart: url, - startInstance: "global" - })); + const query = parse(req.getQuery()); + + if (typeof query.organizationSlug !== 'string') { + console.error('Expected organizationSlug parameter'); + res.writeStatus("400 Bad request").end("Expected organizationSlug parameter"); + } + if (typeof query.worldSlug !== 'string') { + console.error('Expected worldSlug parameter'); + res.writeStatus("400 Bad request").end("Expected worldSlug parameter"); + } + if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) { + console.error('Expected only one roomSlug parameter'); + res.writeStatus("400 Bad request").end("Expected only one roomSlug parameter"); + } + + (async () => { + const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined); + + res.writeStatus("200 OK").end(JSON.stringify(mapDetails)); + })(); + }); } } diff --git a/back/src/Services/AdminApi.ts b/back/src/Services/AdminApi.ts index 2d03ee9d..2b3e15ae 100644 --- a/back/src/Services/AdminApi.ts +++ b/back/src/Services/AdminApi.ts @@ -11,7 +11,30 @@ export interface AdminApiData { } class AdminApi { - + + async fetchMapDetails(organizationSlug: string, worldSlug: string, roomSlug: string|undefined): Promise { + if (!ADMIN_API_URL) { + return Promise.reject('No admin backoffice set!'); + } + + const params: { organizationSlug: string, worldSlug: string, mapSlug?: string } = { + organizationSlug, + worldSlug + }; + + if (roomSlug) { + params.mapSlug = roomSlug; + } + + const res = await Axios.get(ADMIN_API_URL+'/api/map', + { + headers: {"Authorization" : `${ADMIN_API_TOKEN}`}, + params + } + ) + return res.data; + } + async fetchMemberDataByToken(organizationMemberToken: string): Promise { if (!ADMIN_API_URL) { return Promise.reject('No admin backoffice set!'); @@ -40,4 +63,4 @@ class AdminApi { } } -export const adminApi = new AdminApi(); \ No newline at end of file +export const adminApi = new AdminApi();