workadventure/back/src/Services/AdminApi.ts

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-10-06 15:37:00 +02:00
import {ADMIN_API_TOKEN, ADMIN_API_URL} from "../Enum/EnvironmentVariable";
import Axios from "axios";
import {RoomIdentifier} from "../Model/RoomIdentifier";
2020-10-06 15:37:00 +02:00
export interface AdminApiData {
organizationSlug: string
worldSlug: string
roomSlug: string
mapUrlStart: string
userUuid: string
}
class AdminApi {
2020-10-13 15:12:24 +02:00
async fetchMapDetails(organizationSlug: string, worldSlug: string, roomSlug: string|undefined): Promise<AdminApiData> {
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
const params: { organizationSlug: string, worldSlug: string, roomSlug?: string } = {
2020-10-13 15:12:24 +02:00
organizationSlug,
worldSlug
};
if (roomSlug) {
params.roomSlug = roomSlug;
2020-10-13 15:12:24 +02:00
}
const res = await Axios.get(ADMIN_API_URL+'/api/map',
{
headers: {"Authorization" : `${ADMIN_API_TOKEN}`},
params
}
)
return res.data;
}
2020-10-06 15:37:00 +02:00
async fetchMemberDataByToken(organizationMemberToken: string): Promise<AdminApiData> {
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
const res = await Axios.get(ADMIN_API_URL+'/api/login-url/'+organizationMemberToken,
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} }
)
return res.data;
}
async memberIsGrantedAccessToRoom(memberId: string, roomIdentifier: RoomIdentifier): Promise<boolean> {
2020-10-06 15:37:00 +02:00
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
2020-10-09 16:18:25 +02:00
try {
//todo: send more specialized data instead of the whole id
2020-10-09 16:18:25 +02:00
const res = await Axios.get(ADMIN_API_URL+'/api/member/is-granted-access',
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`}, params: {memberId, roomIdentifier: roomIdentifier.id} }
2020-10-09 16:18:25 +02:00
)
return !!res.data;
} catch (e) {
console.log(e.message)
return false;
}
2020-10-06 15:37:00 +02:00
}
}
2020-10-13 15:12:24 +02:00
export const adminApi = new AdminApi();