Skip to content

Commit

Permalink
chore(api-service): Instrument get subscriber preferences (#7451)
Browse files Browse the repository at this point in the history
  • Loading branch information
SokratisVidros authored Jan 7, 2025
1 parent 73b8466 commit fd8e082
Showing 1 changed file with 79 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
NotificationTemplateRepository,
SubscriberRepository,
PreferencesRepository,
PreferencesEntity,
NotificationTemplateEntity,
} from '@novu/dal';
import {
Expand All @@ -13,9 +12,8 @@ import {
StepTypeEnum,
} from '@novu/shared';

import { AnalyticsService } from '../../services/analytics.service';
import { GetSubscriberPreferenceCommand } from './get-subscriber-preference.command';
import { InstrumentUsecase } from '../../instrumentation';
import { Instrument, InstrumentUsecase } from '../../instrumentation';
import { MergePreferences } from '../merge-preferences/merge-preferences.usecase';
import { GetPreferences, PreferenceSet } from '../get-preferences';
import {
Expand All @@ -30,7 +28,6 @@ export class GetSubscriberPreference {
constructor(
private subscriberRepository: SubscriberRepository,
private notificationTemplateRepository: NotificationTemplateRepository,
private analyticsService: AnalyticsService,
private preferencesRepository: PreferencesRepository,
) {}

Expand All @@ -56,14 +53,6 @@ export class GetSubscriberPreference {
},
);

this.analyticsService.mixpanelTrack(
'Fetch User Preferences - [Notification Center]',
'',
{
_organization: command.organizationId,
templatesSize: workflowList.length,
},
);
const workflowIds = workflowList.map((wf) => wf._id);

const [
Expand All @@ -72,26 +61,22 @@ export class GetSubscriberPreference {
subscriberWorkflowPreferences,
subscriberGlobalPreference,
] = await Promise.all([
this.preferencesRepository.find({
_templateId: { $in: workflowIds },
_environmentId: command.environmentId,
type: PreferencesTypeEnum.WORKFLOW_RESOURCE,
}) as Promise<PreferenceSet['workflowResourcePreference'][] | null>,
this.preferencesRepository.find({
_templateId: { $in: workflowIds },
_environmentId: command.environmentId,
type: PreferencesTypeEnum.USER_WORKFLOW,
}) as Promise<PreferenceSet['workflowUserPreference'][] | null>,
this.preferencesRepository.find({
_templateId: { $in: workflowIds },
_subscriberId: subscriber._id,
_environmentId: command.environmentId,
type: PreferencesTypeEnum.SUBSCRIBER_WORKFLOW,
this.findWorkflowPreferences({
environmentId: command.environmentId,
workflowIds,
}),
this.preferencesRepository.findOne({
_subscriberId: subscriber._id,
_environmentId: command.environmentId,
type: PreferencesTypeEnum.SUBSCRIBER_GLOBAL,
this.findUserWorkflowPreferences({
environmentId: command.environmentId,
workflowIds,
}),
this.findSubscriberWorkflowPreferences({
environmentId: command.environmentId,
subscriberId: subscriber._id,
workflowIds,
}),
this.findSubscriberGlobalPreferences({
environmentId: command.environmentId,
subscriberId: subscriber._id,
}),
]);

Expand Down Expand Up @@ -223,4 +208,67 @@ export class GetSubscriberPreference {

return channels as unknown as ChannelTypeEnum[];
}

@Instrument()
private async findWorkflowPreferences({
environmentId,
workflowIds,
}: {
environmentId: string;
workflowIds: string[];
}) {
return this.preferencesRepository.find({
_templateId: { $in: workflowIds },
_environmentId: environmentId,
type: PreferencesTypeEnum.WORKFLOW_RESOURCE,
});
}

@Instrument()
private async findUserWorkflowPreferences({
environmentId,
workflowIds,
}: {
environmentId: string;
workflowIds: string[];
}) {
return this.preferencesRepository.find({
_templateId: { $in: workflowIds },
_environmentId: environmentId,
type: PreferencesTypeEnum.USER_WORKFLOW,
});
}

@Instrument()
private async findSubscriberWorkflowPreferences({
environmentId,
subscriberId,
workflowIds,
}: {
environmentId: string;
subscriberId: string;
workflowIds: string[];
}) {
return this.preferencesRepository.find({
_templateId: { $in: workflowIds },
_subscriberId: subscriberId,
_environmentId: environmentId,
type: PreferencesTypeEnum.SUBSCRIBER_WORKFLOW,
});
}

@Instrument()
private async findSubscriberGlobalPreferences({
environmentId,
subscriberId,
}: {
environmentId: string;
subscriberId: string;
}) {
return this.preferencesRepository.findOne({
_subscriberId: subscriberId,
_environmentId: environmentId,
type: PreferencesTypeEnum.SUBSCRIBER_GLOBAL,
});
}
}

0 comments on commit fd8e082

Please sign in to comment.