import { EventEmitterModule } from '@app/event-emitter'; import { RedmineIssuesCacheWriterService } from '@app/event-emitter/issue-cache-writer/redmine-issues-cache-writer.service'; import { EnhancerService } from '@app/event-emitter/issue-enhancers/enhancer.service'; import { TimestampEnhancer } from '@app/event-emitter/issue-enhancers/timestamps-enhancer'; import { MainController } from '@app/event-emitter/main/main.controller'; import { CacheModule, Logger, Module, OnModuleInit } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { switchMap, tap } from 'rxjs'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import configuration from './configs/app'; import { RedminePublicUrlConverter } from './converters/redmine-public-url.converter'; import { Changes } from './couchdb-datasources/changes'; import { CurrentUserEnhancer } from './issue-enhancers/current-user-enhancer'; import { CustomFieldsEnhancer } from './issue-enhancers/custom-fields-enhancer'; import { PersonalNotificationsService } from './notifications/personal-notifications.service'; import { StatusChangeNotificationsService } from './notifications/status-change-notifications.service'; import { ChangesCacheWriterService } from './changes-cache-writer/changes-cache-writer.service'; import { Issues } from '@app/event-emitter/couchdb-datasources/issues'; import { Users } from '@app/event-emitter/couchdb-datasources/users'; import { TelegramBotService } from './telegram-bot/telegram-bot.service'; 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 { StatusChangeAdapterService } from './notifications/adapters/status-change.adapter.service'; import { CurrentIssuesEccmReportService } from './reports/current-issues-eccm.report.service'; import { CurrentIssuesEccmBotHandlerService } from './telegram-bot/handlers/current-issues-eccm.bot-handler.service'; import { CurrentIssuesEccmReportController } from './reports/current-issues-eccm.report.controller'; import { DailyEccmReportController } from './reports/daily-eccm.report.controller'; import { DailyEccmReportService } from './reports/daily-eccm.report.service'; import { ChangesService } from './changes/changes.service'; import { DailyEccmReportsDatasource } from './couchdb-datasources/daily-eccm-reports.datasource'; import { ScheduleModule } from '@nestjs/schedule'; import { DailyEccmReportTask } from './reports/daily-eccm.report.task'; @Module({ imports: [ EventEmitterModule.register({ config: configuration().redmineIssueEventEmitterConfig, }), ConfigModule.forRoot({ load: [configuration] }), CacheModule.register({ isGlobal: true, }), ScheduleModule.forRoot(), ], controllers: [ AppController, MainController, CurrentIssuesEccmReportController, DailyEccmReportController, ], providers: [ AppService, CustomFieldsEnhancer, CurrentUserEnhancer, PersonalNotificationsService, StatusChangeNotificationsService, Changes, RedminePublicUrlConverter, ChangesCacheWriterService, TelegramBotService, UserMetaInfoService, UserMetaInfo, PersonalNotificationAdapterService, StatusChangeAdapterService, CurrentIssuesEccmReportService, CurrentIssuesEccmBotHandlerService, DailyEccmReportService, ChangesService, DailyEccmReportsDatasource, DailyEccmReportTask, ], }) export class AppModule implements OnModuleInit { private logger = new Logger(AppModule.name); constructor( private personalNotificationsService: PersonalNotificationsService, private redmineIssuesCacheWriterService: RedmineIssuesCacheWriterService, private enhancerService: EnhancerService, private timestampEnhancer: TimestampEnhancer, private customFieldsEnhancer: CustomFieldsEnhancer, private currentUserEnhancer: CurrentUserEnhancer, private statusChangeNotificationsService: StatusChangeNotificationsService, private changesCacheWriterService: ChangesCacheWriterService, private telegramBotService: TelegramBotService, private personalNotificationAdapterService: PersonalNotificationAdapterService, private statusChangeAdapterService: StatusChangeAdapterService, ) {} onModuleInit() { Issues.getDatasource(); Users.getDatasource(); Changes.getDatasource(); UserMetaInfo.getDatasource(); DailyEccmReportsDatasource.getDatasource(); this.enhancerService.addEnhancer([ this.timestampEnhancer, this.customFieldsEnhancer, this.currentUserEnhancer, ]); this.personalNotificationsService.$messages.subscribe((resp) => { this.logger.log( `Get personal message ` + JSON.stringify(resp.personalParsedMessage.message) + ` for recipients ` + JSON.stringify(resp.personalParsedMessage.recipients), ); this.personalNotificationAdapterService.send(resp); }); this.statusChangeNotificationsService.$batchChanges.subscribe((changes) => { this.statusChangeAdapterService.batchSend(changes); }); this.redmineIssuesCacheWriterService.subject .pipe( tap((saveResult) => { this.logger.debug( `Save result process started, issue_id = ${saveResult.current.id}`, ); }), ) .pipe( switchMap(async (saveResult) => { this.logger.debug(`personalNotificationsService.analize started`); await this.personalNotificationsService.analize(saveResult); this.logger.debug('personalNotificationsService.analize successed'); return saveResult; }), switchMap(async (saveResult) => { // eslint-disable-next-line prettier/prettier this.logger.debug(`statusChangeNotificationsService.getChanges started`); const changes = await this.statusChangeNotificationsService.getChanges(saveResult); // eslint-disable-next-line prettier/prettier this.logger.debug(`statusChangeNotificationsService.getChanges successed`); return { changes, saveResult }; }), switchMap(async (args) => { this.logger.debug(`Save changes in couchdb started`); const promises = args.changes.map((c) => this.changesCacheWriterService.saveChange(c), ); await Promise.all(promises); this.logger.debug('Save changes in couchdb successed'); return args; }), switchMap(async (args) => { this.logger.debug(``); return args; }), ) .subscribe(async (args) => { this.logger.debug( `Save result process success finished, issue_id = ${args.saveResult.current.id}`, ); }); } }