diff --git a/configs/main-config.jsonc.dist b/configs/main-config.jsonc.dist index e558201..a42a743 100644 --- a/configs/main-config.jsonc.dist +++ b/configs/main-config.jsonc.dist @@ -6,5 +6,6 @@ } }, "telegramBotToken": "", - "personalMessageTemplate": "" + "personalMessageTemplate": "", + "periodValidityNotification": 43200 // 12h } \ No newline at end of file diff --git a/src/models/app-config.model.ts b/src/models/app-config.model.ts index be968a9..2f04e56 100644 --- a/src/models/app-config.model.ts +++ b/src/models/app-config.model.ts @@ -16,4 +16,5 @@ export type AppConfig = { }; }; telegramBotToken: string; + periodValidityNotification: number; }; diff --git a/src/notifications/adapters/status-change.adapter.service.ts b/src/notifications/adapters/status-change.adapter.service.ts index dcdba57..68ba715 100644 --- a/src/notifications/adapters/status-change.adapter.service.ts +++ b/src/notifications/adapters/status-change.adapter.service.ts @@ -1,4 +1,5 @@ import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import TelegramBot from 'node-telegram-bot-api'; import { Change } from 'src/models/change.model'; import { TelegramBotService } from 'src/telegram-bot/telegram-bot.service'; @@ -17,8 +18,23 @@ namespace StatusChangeAdapter { @Injectable() export class StatusChangeAdapterService { - constructor(private telegramBotService: TelegramBotService) {} + private periodValidityNotification: number; + constructor( + private telegramBotService: TelegramBotService, + private configService: ConfigService, + ) { + this.periodValidityNotification = this.configService.get( + 'periodValidityNotification', + ); + } + + // TODO: Удалить устаревший метод send + + /** + * @deprecated + * @see StatusChangeAdapterService.batchSend + */ async send(change: Change): Promise { const promises = change.messages.map((m) => { if (!m || !m.recipient || !m.recipient.id || !m.notification_message) { @@ -51,8 +67,15 @@ export class StatusChangeAdapterService { private getMessages(changes: Change[]): StatusChangeAdapter.MsgFromBatch[] { const res: StatusChangeAdapter.MsgFromBatch[] = []; const store: Record = {}; + const nowTimestamp = new Date().getTime(); for (let i = 0; i < changes.length; i++) { const change = changes[i]; + if ( + nowTimestamp - change.created_on_timestamp > + this.periodValidityNotification + ) { + continue; + } for (let j = 0; j < change.messages.length; j++) { const message = change.messages[j]; if (!message.change_message) continue;