Passed
Pull Request — master (#436)
by
unknown
02:07
created

src/Application/Notification/Command/CreateNotificationCommandHandler.ts   A

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 79
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 65
mnd 3
bc 3
fnc 1
dl 0
loc 79
rs 10
bpm 3
cpm 4
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B CreateNotificationCommandHandler.execute 0 54 4
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import {
4
  Notification,
5
  NotificationType
6
} from 'src/Domain/Notification/Notification.entity';
7
import { CreateNotificationCommand } from './CreateNotificationCommand';
8
import { INotificationRepository } from 'src/Domain/Notification/Repository/INotificationRepository';
9
import { IMattermostNotifier } from 'src/Application/IMattermostNotifier';
10
import { ConfigService } from '@nestjs/config';
11
12
const { MATTERMOST_CHANNEL_LEAVES_ID } = process.env;
13
14
@CommandHandler(CreateNotificationCommand)
15
export class CreateNotificationCommandHandler {
16
  constructor(
17
    @Inject('INotificationRepository')
18
    private readonly notificationRepository: INotificationRepository,
19
    @Inject('IMattermostNotifier')
20
    private readonly mattermostNotifier: IMattermostNotifier,
21
    private readonly configService: ConfigService
22
  ) {}
23
24
  public async execute(command: CreateNotificationCommand): Promise<string> {
25
    const { message, type, leaveReaquest } = command;
26
27
    if (type === NotificationType.POST) {
28
      const { id } = await this.mattermostNotifier.createPost(
29
        MATTERMOST_CHANNEL_LEAVES_ID,
30
        message
31
      );
32
      const notification = await this.notificationRepository.save(
33
        new Notification(type, message, id, leaveReaquest)
34
      );
35
36
      return notification.getId();
37
    }
38
39
    if (type === NotificationType.REACTION) {
40
      const post = await this.notificationRepository.findByLeaveRequestIdAndType(
41
        leaveReaquest.getId(),
42
        NotificationType.POST
43
      );
44
45
      const { id } = await this.mattermostNotifier.createReaction(
46
        post.getResourceId(),
47
        message
48
      );
49
50
      const notification = await this.notificationRepository.save(
51
        new Notification(type, message, post.getResourceId(), leaveReaquest)
52
      );
53
54
      return notification.getId();
55
    }
56
57
    if (type === NotificationType.COMMENT) {
58
      const post = await this.notificationRepository.findByLeaveRequestIdAndType(
59
        leaveReaquest.getId(),
60
        NotificationType.POST
61
      );
62
63
      const { id } = await this.mattermostNotifier.createComment(
64
        this.configService.get<string>('MATTERMOST_CHANNEL_LEAVES_ID'),
65
        message,
66
        post.getResourceId()
67
      );
68
69
      const notification = await this.notificationRepository.save(
70
        new Notification(type, message, id, leaveReaquest)
71
      );
72
73
      return notification.getId();
74
    }
75
76
    throw new Error('Type not managed');
77
  }
78
}
79