Добавлена возможность заменять файл с расписанием.

Добалена возможность давать доступ к end-point'ам только определённым ролям.

Чуть-чуть меньше спагетти в объявлениях модулей.
This commit is contained in:
2024-10-03 01:49:23 +04:00
parent d18a6764c9
commit 32e06350ad
20 changed files with 361 additions and 46 deletions

View File

@@ -290,6 +290,10 @@ export class ScheduleParser {
const llesson = lday.lessons[lessonIdx];
// noinspection SpellCheckingInspection
const rlesson = rday.lessons[lessonIdx];
if (llesson === null && rlesson === null) continue;
if (!llesson || !rlesson) return false;
if (
llesson.name.length > 0 &&
(llesson.name !== rlesson.name ||

View File

@@ -9,11 +9,22 @@ import {
NotAcceptableException,
ServiceUnavailableException,
} from "@nestjs/common";
import { ScheduleReplacerService } from "../../../schedule-replacer/schedule-replacer.service";
import { Error } from "mongoose";
import * as crypto from "crypto";
export class BasicXlsDownloader extends XlsDownloaderBase {
cache: XlsDownloaderResult | null = null;
preparedData: { downloadLink: string; updateDate: string } | null = null;
private cacheHash: string = "0000000000000000000000000000000000000000";
private lastUpdate: number = 0;
private scheduleReplacerService: ScheduleReplacerService | null = null;
setScheduleReplacerService(service: ScheduleReplacerService) {
this.scheduleReplacerService = service;
}
private async getDOM(preparedData: any): Promise<JSDOM | null> {
try {
@@ -103,17 +114,32 @@ export class BasicXlsDownloader extends XlsDownloaderBase {
${response.statusText}`);
}
const replacer = await this.scheduleReplacerService.getByEtag(
response.headers["etag"]!,
);
const fileData: ArrayBuffer = replacer
? replacer.data
: response.data.buffer;
const fileDataHash = crypto
.createHash("sha1")
.update(Buffer.from(fileData).toString("base64"))
.digest("hex");
const result: XlsDownloaderResult = {
fileData: response.data.buffer,
fileData: fileData,
updateDate: this.preparedData.updateDate,
etag: response.headers["etag"],
new:
this.cacheMode === XlsDownloaderCacheMode.NONE
? true
: this.cache?.etag !== response.headers["etag"],
: this.cacheHash !== fileDataHash,
updateRequired: this.isUpdateRequired(),
};
this.cacheHash = fileDataHash;
if (this.cacheMode !== XlsDownloaderCacheMode.NONE) this.cache = result;
return result;

View File

@@ -3,9 +3,15 @@ import { ScheduleService } from "./schedule.service";
import { ScheduleController } from "./schedule.controller";
import { UsersService } from "../users/users.service";
import { PrismaService } from "../prisma/prisma.service";
import { ScheduleReplacerService } from "../schedule-replacer/schedule-replacer.service";
@Module({
providers: [ScheduleService, UsersService, PrismaService],
providers: [
ScheduleService,
ScheduleReplacerService,
UsersService,
PrismaService,
],
controllers: [ScheduleController],
exports: [ScheduleService],
})

View File

@@ -17,6 +17,7 @@ import { Cache, CACHE_MANAGER } from "@nestjs/cache-manager";
import { instanceToPlain } from "class-transformer";
import { cacheGetOrFill } from "../utility/cache.util";
import * as crypto from "crypto";
import { ScheduleReplacerService } from "../schedule-replacer/schedule-replacer.service";
@Injectable()
export class ScheduleService {
@@ -33,7 +34,18 @@ export class ScheduleService {
private lastChangedDays: Array<Array<number>> = [];
private scheduleUpdatedAt: Date = new Date(0);
constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache) {}
constructor(
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
private readonly scheduleReplacerService: ScheduleReplacerService,
) {
const xlsDownloader = this.scheduleParser.getXlsDownloader();
if (xlsDownloader instanceof BasicXlsDownloader) {
xlsDownloader.setScheduleReplacerService(
this.scheduleReplacerService,
);
}
}
getCacheStatus(): CacheStatusDto {
return {
@@ -45,7 +57,7 @@ export class ScheduleService {
};
}
private async getSourceSchedule(): Promise<ScheduleParseResult> {
async getSourceSchedule(): Promise<ScheduleParseResult> {
return cacheGetOrFill(this.cacheManager, "sourceSchedule", async () => {
const schedule = await this.scheduleParser.getSchedule();
schedule.groups = ScheduleService.toObject(
@@ -146,10 +158,13 @@ export class ScheduleService {
await this.scheduleParser
.getXlsDownloader()
.setPreparedData(siteMainPageDto.mainPage);
await this.cacheManager.reset();
await this.getSourceSchedule();
await this.refreshCache();
return this.getCacheStatus();
}
async refreshCache() {
await this.cacheManager.reset();
await this.getSourceSchedule();
}
}