Passed
Pull Request — master (#451)
by
unknown
02:11
created

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

Complexity

Total Complexity 7
Complexity/F 2.33

Size

Lines of Code 98
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 78
mnd 4
bc 4
fnc 3
dl 0
loc 98
bpm 1.3333
cpm 2.3333
noi 0
c 0
b 0
f 0
rs 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
A CreateNotificationCommandHandler.execute 0 8 2
A CreateNotificationCommandHandler.getRootNotification 0 5 1
B CreateNotificationCommandHandler.createNotification 0 56 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
    try {
26
      return await this.createNotification(command);
27
    } catch (e) {
28
      // On avale l'exception pour éviter d'interrompre les autres traitements.
29
      // Tant pis si la notification ne part pas, on finira bien par s'en rendre compte.
30
      console.error('Failed to create notification:', e);
31
    }
32
  }
33
  private async createNotification(command: CreateNotificationCommand): Promise<string> {
34
    const { message, type, leaveReaquest } = command;
35
36
    if (type === NotificationType.POST) {
37
      const { id } = await this.mattermostNotifier.createPost(
38
        MATTERMOST_CHANNEL_LEAVES_ID,
39
        message
40
      );
41
      const notification = await this.notificationRepository.save(
42
        new Notification(type, message, id, leaveReaquest)
43
      );
44
45
      return notification.getId();
46
    }
47
48
    if (type === NotificationType.REACTION) {
49
      const rootNotification = await this.getRootNotification(
50
        leaveReaquest.getId()
51
      );
52
53
      await this.mattermostNotifier.createReaction(
54
        rootNotification.getResourceId(),
55
        message
56
      );
57
58
      const notification = await this.notificationRepository.save(
59
        new Notification(
60
          type,
61
          message,
62
          rootNotification.getResourceId(),
63
          leaveReaquest
64
        )
65
      );
66
67
      return notification.getId();
68
    }
69
70
    if (type === NotificationType.COMMENT) {
71
      const rootNotification = await this.getRootNotification(
72
        leaveReaquest.getId()
73
      );
74
75
      const { id } = await this.mattermostNotifier.createComment(
76
        this.configService.get<string>('MATTERMOST_CHANNEL_LEAVES_ID'),
77
        message,
78
        rootNotification.getResourceId()
79
      );
80
81
      const notification = await this.notificationRepository.save(
82
        new Notification(type, message, id, leaveReaquest)
83
      );
84
85
      return notification.getId();
86
    }
87
88
    throw new Error('Type not managed');
89
  }
90
91
  private async getRootNotification(leaveReaquestId: string) {
92
    return await this.notificationRepository.findByLeaveRequestIdAndType(
93
      leaveReaquestId,
94
      NotificationType.POST
95
    );
96
  }
97
}
98