-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathget-subscriber-preferences.usecase.ts
70 lines (61 loc) · 2.33 KB
/
get-subscriber-preferences.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
import { Injectable } from '@nestjs/common';
import {
GetSubscriberGlobalPreference,
GetSubscriberGlobalPreferenceCommand,
GetSubscriberPreference,
GetSubscriberPreferenceCommand,
} from '@novu/application-generic';
import { IGetSubscriberPreferencesResponseDto, ISubscriberPreferenceResponse } from '@novu/shared';
import { GetSubscriberPreferencesCommand } from './get-subscriber-preferences.command';
@Injectable()
export class GetSubscriberPreferences {
constructor(
private getSubscriberGlobalPreference: GetSubscriberGlobalPreference,
private getSubscriberPreference: GetSubscriberPreference
) {}
async execute(command: GetSubscriberPreferencesCommand): Promise<IGetSubscriberPreferencesResponseDto> {
const globalPreference = await this.fetchGlobalPreference(command);
const workflowPreferences = await this.fetchWorkflowPreferences(command);
return {
global: globalPreference,
workflows: workflowPreferences,
};
}
private async fetchGlobalPreference(command: GetSubscriberPreferencesCommand) {
const { preference } = await this.getSubscriberGlobalPreference.execute(
GetSubscriberGlobalPreferenceCommand.create({
organizationId: command.organizationId,
environmentId: command.environmentId,
subscriberId: command.subscriberId,
includeInactiveChannels: false,
})
);
return {
enabled: preference.enabled,
channels: preference.channels,
};
}
private async fetchWorkflowPreferences(command: GetSubscriberPreferencesCommand) {
const subscriberWorkflowPreferences = await this.getSubscriberPreference.execute(
GetSubscriberPreferenceCommand.create({
environmentId: command.environmentId,
subscriberId: command.subscriberId,
organizationId: command.organizationId,
includeInactiveChannels: false,
})
);
return subscriberWorkflowPreferences.map(this.mapToWorkflowPreference);
}
private mapToWorkflowPreference(subscriberWorkflowPreference: ISubscriberPreferenceResponse) {
const { preference, template } = subscriberWorkflowPreference;
return {
enabled: preference.enabled,
channels: preference.channels,
overrides: preference.overrides,
workflow: {
identifier: template.triggers[0].identifier,
name: template.name,
},
};
}
}