@khodorkovskyalexey/rasp-omgtu-sdk
Version:
109 lines • 5.3 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var RaspOmgtuSdkService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RaspOmgtuSdkService = void 0;
const common_1 = require("@nestjs/common");
const rxjs_1 = require("rxjs");
const rasp_omgtu_sdk_types_1 = require("../rasp-omgtu-sdk.types");
let RaspOmgtuSdkService = RaspOmgtuSdkService_1 = class RaspOmgtuSdkService {
constructor(httpService) {
this.httpService = httpService;
this.searchUrl = `${RaspOmgtuSdkService_1.baseApiUrl}/search`;
this.baseScheduleUrlMapper = {
[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.GROUP]: (id) => `${RaspOmgtuSdkService_1.baseApiUrl}/schedule/group/${id}`,
[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.AUDITORIUM]: (id) => `${RaspOmgtuSdkService_1.baseApiUrl}/schedule/auditorium/${id}`,
[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.PERSON]: (id) => `${RaspOmgtuSdkService_1.baseApiUrl}/schedule/person/${id}`,
[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.STUDENT]: (id) => `${RaspOmgtuSdkService_1.baseApiUrl}/schedule/student/${id}`,
[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.LECTURER]: (id) => `${RaspOmgtuSdkService_1.baseApiUrl}/schedule/lecturer/${id}`,
};
}
async search(type, filter) {
const url = `${this.searchUrl}?term=${filter}&type=${type}`;
try {
const groups = await this.fetch(encodeURI(url));
return groups;
}
catch (error) {
throw new common_1.BadRequestException(error.message);
}
}
async schedulesForGroup(groupId, start, finish, lng = 1) {
const baseUrl = this.baseScheduleUrlMapper[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.GROUP](groupId);
const url = `${baseUrl}?start=${start}&finish=${finish}&lng=${lng}`;
try {
const schedules = await this.fetch(url);
return schedules;
}
catch (error) {
throw new common_1.BadRequestException(error.message);
}
}
async schedulesForAuditorium(auditoriumId, start, finish, lng = 1) {
const baseUrl = this.baseScheduleUrlMapper[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.AUDITORIUM](auditoriumId);
const url = `${baseUrl}?start=${start}&finish=${finish}&lng=${lng}`;
try {
const schedules = await this.fetch(url);
return schedules;
}
catch (error) {
throw new common_1.BadRequestException(error.message);
}
}
async schedulesForPerson(personId, start, finish, lng = 1) {
const baseUrl = this.baseScheduleUrlMapper[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.PERSON](personId);
const url = `${baseUrl}?start=${start}&finish=${finish}&lng=${lng}`;
try {
const schedules = await this.fetch(url);
return schedules;
}
catch (error) {
throw new common_1.BadRequestException(error.message);
}
}
async schedulesForLecturer(lecturerId, start, finish, lng = 1) {
const baseUrl = this.baseScheduleUrlMapper[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.LECTURER](lecturerId);
const url = `${baseUrl}?start=${start}&finish=${finish}&lng=${lng}`;
try {
const schedules = await this.fetch(url);
return schedules;
}
catch (error) {
throw new common_1.BadRequestException(error.message);
}
}
async schedulesForStudent(studentId, start, finish, lng = 1) {
const baseUrl = this.baseScheduleUrlMapper[rasp_omgtu_sdk_types_1.RaspOmgtuScheduleFor.STUDENT](studentId);
const url = `${baseUrl}?start=${start}&finish=${finish}&lng=${lng}`;
try {
const schedules = await this.fetch(url);
return schedules;
}
catch (error) {
throw new common_1.BadRequestException(error.message);
}
}
async fetch(url) {
const fetch = this.httpService.get(url);
const response = await (0, rxjs_1.firstValueFrom)(fetch);
if (!response) {
throw new Error(`Fetch to ${url} return null`);
}
return Object.assign({}, response.data);
}
};
RaspOmgtuSdkService.baseApiUrl = 'https://rasp.omgtu.ru/api';
RaspOmgtuSdkService = RaspOmgtuSdkService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(common_1.HttpService))
], RaspOmgtuSdkService);
exports.RaspOmgtuSdkService = RaspOmgtuSdkService;
//# sourceMappingURL=rasp-omgtu-sdk.service.js.map