-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
143 lines (132 loc) · 4.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
import axios from 'axios';
import { prependListener } from 'process';
import * as types from './types'
class DSNET {
/** A token obtained in OAUTH2 login flow */
private accessToken: string;
/** Optional API URL to exectute all queries against */
public baseURL: string;
/**
* @param {string} accessToken A token obtained in OAUTH2 login flow
* @param {string} baseUrl Optional API URL to exectute all queries against */
constructor(accessToken: string, baseUrl = 'https://panel.dsnet.agh.edu.pl/api/beta') {
this.accessToken = accessToken;
this.baseURL = baseUrl;
}
/** Get dormitory types array, no idea if this actually ever changes */
async getDormitoryTypes(): Promise<types.DormitoryType[]> {
const res = (await axios.get(`${this.baseURL}/dormitory/types`, {headers: {'Authorization':`Bearer ${this.accessToken}`}})).data;
return res;
}
/**
* Get list of all dormitories */
async getDormitoryList(): Promise<types.Dormitory[]> {
const res = (await axios.get(`${this.baseURL}/dormitory/dormitories`, {headers: {'Authorization':`Bearer ${this.accessToken}`}})).data;
return res.map(el => {
return {
id: el.id,
typeId: el.type_id,
name: el.name,
shortname: el.shortname
}
});
}
/**
* Get list of all floors of a dormitory (returned floor)
* @param dormitoryID An unique dormitory ID
* @returns Returned floor IDs are unique across entire campus*/
async getDormitoryFloors(dormitoryID: number): Promise<types.Floor[]> {
const res = (await axios.get(`${this.baseURL}/dormitory/levels/${dormitoryID}`, {headers: {'Authorization':`Bearer ${this.accessToken}`}})).data;
return res.map(el => {
return {
id: el.id,
floor: el.floor,
name: el.name
}
})
}
/** Get list of all rooms at given floor */
async getRoomsAtFloor(floorID: number): Promise<types.Room[]> {
const res = (await axios.get(`${this.baseURL}/dormitory/rooms/${floorID}`, {headers: {'Authorization':`Bearer ${this.accessToken}`}})).data;
return res.map(el => {
return {
id: el.id,
name: el.name,
capacity: el.capacity,
rookie: el.rookie,
marriage: el.marriage,
disability: el.disability
}
})
}
async getNetworkInfo(): Promise<types.NetworkInfo> {
const res = (await axios.get(`${this.baseURL}/network/transfer`, {headers: {'Authorization':`Bearer ${this.accessToken}`}})).data;
return {
tenantId: res.tenant_id,
upload: {
used: res.upload_limited,
limit: res.upload_limit
},
download: {
used: {
limited: res.download_limited,
all: res.download_all
},
limit: res.download_limit
},
currentMultiplier: res.cost/100
};
}
async getReservations(): Promise<types.Reservation[]> {
const reservations = await axios.get(`${this.baseURL}/reservations/current`, {headers: {'Authorization':`Bearer ${this.accessToken}`}});
const mappedRes = reservations.data.map((res) => {
const parsedFrom = new Date(`${res.day}T${res.slot_name.split('-')[0]}`);
const parsedTo = new Date(`${res.day}T${res.slot_name.split('-')[1]}`);
if(parsedTo.getHours() < 6) parsedTo.setDate(parsedTo.getDate() + 1);
return {
reservationId: res.id,
object: {id: res.object_id, name: res.object_name},
parent: {id: res.parent_id, name: res.parent_name},
slot: {id: res.slot_id, name: res.slot_name},
from: parsedFrom,
to: parsedTo
}
})
return mappedRes;
}
async getUserInfo(): Promise<types.UserInfo> {
const basicInfo = (await axios.get(`${this.baseURL}/tenant/user`, {headers: {'Authorization':`Bearer ${this.accessToken}`}})).data;
const personalInfo = (await axios.get(`${this.baseURL}/tenant/personal`, {headers: {'Authorization':`Bearer ${this.accessToken}`}})).data;
// console.log({basic: userInfo.data, personal: personalUserInfo.data})
return {
tenantId: basicInfo.tenant_id,
displayName: basicInfo.name + " " + basicInfo.surname,
name: {
first: basicInfo.name,
last: basicInfo.surname
},
email: personalInfo.email,
phone: personalInfo.phone,
verified: basicInfo.verified,
language: basicInfo.language,
nationality: personalInfo.nationality,
gender: personalInfo.gender,
};
}
async getAccommodationInfo(): Promise<types.Accommodation> {
const res = await axios.get(`${this.baseURL}/tenant/accommodation`, {headers: {'Authorization':`Bearer ${this.accessToken}`}});
return {
tenantId: res.data.tenant_id,
room: {
id: res.data.room_id,
name: res.data.room,
},
dormitory: {
id: res.data.dormitory_id,
name: res.data.dormitory
}
}
}
}
export default DSNET;