/
create-novu-integrations.usecase.ts
96 lines (87 loc) · 3.46 KB
/
create-novu-integrations.usecase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Injectable } from '@nestjs/common';
import { IntegrationRepository } from '@novu/dal';
import { areNovuEmailCredentialsSet, areNovuSmsCredentialsSet } from '@novu/application-generic';
import { CreateNovuIntegrationsCommand } from './create-novu-integrations.command';
import { CreateIntegration } from '../create-integration/create-integration.usecase';
import { CreateIntegrationCommand } from '../create-integration/create-integration.command';
import { ChannelTypeEnum, EmailProviderIdEnum, SmsProviderIdEnum } from '@novu/shared';
import { SetIntegrationAsPrimary } from '../set-integration-as-primary/set-integration-as-primary.usecase';
import { SetIntegrationAsPrimaryCommand } from '../set-integration-as-primary/set-integration-as-primary.command';
@Injectable()
export class CreateNovuIntegrations {
constructor(
private createIntegration: CreateIntegration,
private integrationRepository: IntegrationRepository,
private setIntegrationAsPrimary: SetIntegrationAsPrimary
) {}
private async createEmailIntegration(command: CreateNovuIntegrationsCommand) {
if (!areNovuEmailCredentialsSet()) {
return;
}
const emailIntegrationCount = await this.integrationRepository.count({
providerId: EmailProviderIdEnum.Novu,
channel: ChannelTypeEnum.EMAIL,
_organizationId: command.organizationId,
_environmentId: command.environmentId,
});
if (emailIntegrationCount === 0) {
const novuEmailIntegration = await this.createIntegration.execute(
CreateIntegrationCommand.create({
providerId: EmailProviderIdEnum.Novu,
channel: ChannelTypeEnum.EMAIL,
active: true,
name: 'Novu Email',
check: false,
userId: command.userId,
environmentId: command.environmentId,
organizationId: command.organizationId,
})
);
await this.setIntegrationAsPrimary.execute(
SetIntegrationAsPrimaryCommand.create({
organizationId: command.organizationId,
environmentId: command.environmentId,
integrationId: novuEmailIntegration._id,
userId: command.userId,
})
);
}
}
private async createSmsIntegration(command: CreateNovuIntegrationsCommand) {
if (!areNovuSmsCredentialsSet()) {
return;
}
const smsIntegrationCount = await this.integrationRepository.count({
providerId: SmsProviderIdEnum.Novu,
channel: ChannelTypeEnum.SMS,
_organizationId: command.organizationId,
_environmentId: command.environmentId,
});
if (smsIntegrationCount === 0) {
const novuSmsIntegration = await this.createIntegration.execute(
CreateIntegrationCommand.create({
providerId: SmsProviderIdEnum.Novu,
channel: ChannelTypeEnum.SMS,
name: 'Novu SMS',
active: true,
check: false,
userId: command.userId,
environmentId: command.environmentId,
organizationId: command.organizationId,
})
);
await this.setIntegrationAsPrimary.execute(
SetIntegrationAsPrimaryCommand.create({
organizationId: command.organizationId,
environmentId: command.environmentId,
integrationId: novuSmsIntegration._id,
userId: command.userId,
})
);
}
}
async execute(command: CreateNovuIntegrationsCommand): Promise<void> {
await this.createEmailIntegration(command);
await this.createSmsIntegration(command);
}
}