-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathget-subscriber-global-preference.usecase.ts
132 lines (115 loc) · 4.05 KB
/
get-subscriber-global-preference.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Injectable, NotFoundException } from '@nestjs/common';
import { SubscriberEntity, SubscriberRepository } from '@novu/dal';
import { IPreferenceChannels, ChannelTypeEnum } from '@novu/shared';
import { GetSubscriberGlobalPreferenceCommand } from './get-subscriber-global-preference.command';
import { buildSubscriberKey, CachedEntity } from '../../services/cache';
import { GetPreferences } from '../get-preferences';
import { GetSubscriberPreference } from '../get-subscriber-preference/get-subscriber-preference.usecase';
import { filteredPreference } from '../get-subscriber-template-preference/get-subscriber-template-preference.usecase';
import { Instrument, InstrumentUsecase } from '../../instrumentation';
@Injectable()
export class GetSubscriberGlobalPreference {
constructor(
private subscriberRepository: SubscriberRepository,
private getPreferences: GetPreferences,
private getSubscriberPreference: GetSubscriberPreference,
) {}
@InstrumentUsecase()
async execute(command: GetSubscriberGlobalPreferenceCommand) {
const subscriber = await this.getSubscriber(command);
const activeChannels = await this.getActiveChannels(command);
const subscriberGlobalPreference = await this.getSubscriberGlobalPreference(
command,
subscriber._id,
);
const channelsWithDefaults = this.buildDefaultPreferences(
subscriberGlobalPreference.channels,
);
let channels: IPreferenceChannels;
if (command.includeInactiveChannels === true) {
channels = channelsWithDefaults;
} else {
channels = filteredPreference(channelsWithDefaults, activeChannels);
}
return {
preference: {
enabled: subscriberGlobalPreference.enabled,
channels,
},
};
}
@Instrument()
private async getSubscriberGlobalPreference(
command: GetSubscriberGlobalPreferenceCommand,
subscriberId: string,
): Promise<{
channels: IPreferenceChannels;
enabled: boolean;
}> {
const subscriberGlobalChannels =
await this.getPreferences.getPreferenceChannels({
environmentId: command.environmentId,
organizationId: command.organizationId,
subscriberId,
});
return {
channels: subscriberGlobalChannels,
enabled: true,
};
}
@Instrument()
private async getActiveChannels(
command: GetSubscriberGlobalPreferenceCommand,
): Promise<ChannelTypeEnum[]> {
const subscriberWorkflowPreferences =
await this.getSubscriberPreference.execute(
GetSubscriberGlobalPreferenceCommand.create({
environmentId: command.environmentId,
subscriberId: command.subscriberId,
organizationId: command.organizationId,
includeInactiveChannels: command.includeInactiveChannels,
}),
);
const activeChannels = new Set<ChannelTypeEnum>();
subscriberWorkflowPreferences.forEach((subscriberWorkflowPreference) => {
Object.keys(subscriberWorkflowPreference.preference.channels).forEach(
(channel) => {
activeChannels.add(channel as ChannelTypeEnum);
},
);
});
return Array.from(activeChannels);
}
@CachedEntity({
builder: (command: { subscriberId: string; _environmentId: string }) =>
buildSubscriberKey({
_environmentId: command._environmentId,
subscriberId: command.subscriberId,
}),
})
private async getSubscriber(
command: GetSubscriberGlobalPreferenceCommand,
): Promise<SubscriberEntity | null> {
const subscriber = await this.subscriberRepository.findBySubscriberId(
command.environmentId,
command.subscriberId,
);
if (!subscriber) {
throw new NotFoundException(
`Subscriber ${command.subscriberId} not found`,
);
}
return subscriber;
}
// adds default state for missing channels
private buildDefaultPreferences(preference: IPreferenceChannels) {
const defaultPreference: IPreferenceChannels = {
email: true,
sms: true,
in_app: true,
chat: true,
push: true,
};
return { ...defaultPreference, ...preference };
}
}