Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 39 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { Inject } from '@nestjs/common'; |
||
2 | import { CommandHandler } from '@nestjs/cqrs'; |
||
3 | import { Notification, NotificationType } from 'src/Domain/Notification/Notification.entity'; |
||
4 | import { CreateNotificationCommand } from './CreateNotificationCommand'; |
||
5 | import { INotificationRepository } from 'src/Domain/Notification/Repository/INotificationRepository'; |
||
6 | import { IMattermostNotifier } from 'src/Application/IMattermostNotifier'; |
||
7 | |||
8 | const { MATTERMOST_CHANNEL_LEAVES_ID } = process.env; |
||
9 | |||
10 | @CommandHandler(CreateNotificationCommand) |
||
11 | export class CreateNotificationCommandHandler { |
||
12 | constructor( |
||
13 | @Inject('INotificationRepository') |
||
14 | private readonly notificationRepository: INotificationRepository, |
||
15 | @Inject('IMattermostNotifier') |
||
16 | private readonly mattermostNotifier: IMattermostNotifier, |
||
17 | ) {} |
||
18 | |||
19 | public async execute(command: CreateNotificationCommand): Promise<string> { |
||
20 | const { message, type, leaveReaquest } = command; |
||
21 | |||
22 | if (type === NotificationType.POST) { |
||
23 | const { id } = await this.mattermostNotifier.createPost(MATTERMOST_CHANNEL_LEAVES_ID, message); |
||
24 | const notification = await this.notificationRepository.save( |
||
25 | new Notification( |
||
26 | type, |
||
27 | message, |
||
28 | id, |
||
29 | leaveReaquest |
||
30 | ) |
||
31 | ); |
||
32 | |||
33 | return notification.getId(); |
||
34 | } |
||
35 | |||
36 | throw new Error('Type not managed'); |
||
37 | } |
||
38 | } |
||
39 |