From 351a7540983d40f89aa1d173eb36ecddd512588c Mon Sep 17 00:00:00 2001 From: Pavel Gnedov Date: Mon, 3 Oct 2022 21:34:28 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=83=D0=B2=D0=B5=D0=B4=D0=BE=D0=BC=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=BE=D0=B1=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=D1=85=20=D1=81=D1=82=D0=B0=D1=82=D1=83?= =?UTF-8?q?=D1=81=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.module.ts | 15 ++++++++++--- .../adapters/status-change.adapter.service.ts | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/notifications/adapters/status-change.adapter.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 2931078..bc20acf 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -23,6 +23,7 @@ import { UserMetaInfoService } from './user-meta-info/user-meta-info.service'; import { UserMetaInfo } from './couchdb-datasources/user-meta-info'; import { PersonalNotificationAdapterService } from './notifications/adapters/personal-notification.adapter/personal-notification.adapter.service'; import { PublicUrlAdapterService } from './notifications/adapters/public-url.adapter.service'; +import { StatusChangeAdapterService } from './notifications/adapters/status-change.adapter.service'; @Module({ imports: [ @@ -49,6 +50,7 @@ import { PublicUrlAdapterService } from './notifications/adapters/public-url.ada UserMetaInfo, PersonalNotificationAdapterService, PublicUrlAdapterService, + StatusChangeAdapterService, ], }) export class AppModule implements OnModuleInit { @@ -65,6 +67,7 @@ export class AppModule implements OnModuleInit { private changesCacheWriterService: ChangesCacheWriterService, private telegramBotService: TelegramBotService, private personalNotificationAdapterService: PersonalNotificationAdapterService, + private statusChangeAdapterService: StatusChangeAdapterService, ) {} onModuleInit() { @@ -89,13 +92,19 @@ export class AppModule implements OnModuleInit { this.personalNotificationAdapterService.send(resp); }); this.statusChangeNotificationsService.$changes.subscribe((change) => { + const messages = change.messages + .map((m) => m.change_message) + .filter((m) => !!m); + const notifications = change.messages + .map((m) => m.notification_message) + .filter((m) => !!m); this.logger.log( `Get status changes messages for ` + `issue_id = ${change.issue_id}, ` + - `messages = ${JSON.stringify( - change.messages.map((m) => m.change_message).filter((m) => !!m), - )}`, + `messages = ${JSON.stringify(messages)}, ` + + `notifications = ${JSON.stringify(notifications)}`, ); + this.statusChangeAdapterService.send(change); }); this.redmineIssuesCacheWriterService.subject diff --git a/src/notifications/adapters/status-change.adapter.service.ts b/src/notifications/adapters/status-change.adapter.service.ts new file mode 100644 index 0000000..06ccba6 --- /dev/null +++ b/src/notifications/adapters/status-change.adapter.service.ts @@ -0,0 +1,22 @@ +import { Injectable } from '@nestjs/common'; +import { Change } from 'src/models/change.model'; +import { TelegramBotService } from 'src/telegram-bot/telegram-bot.service'; + +@Injectable() +export class StatusChangeAdapterService { + constructor(private telegramBotService: TelegramBotService) {} + + async send(change: Change): Promise { + const promises = change.messages.map((m) => { + if (!m || !m.recipient || !m.recipient.id || !m.notification_message) { + return true; + } + return this.telegramBotService.sendMessageByRedmineId( + m.recipient.id, + m.notification_message, + { parse_mode: 'HTML' }, + ); + }); + return await Promise.all(promises); + } +}