Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve mongodb pref fetching #5508

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libs/dal/src/repositories/base-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ export class BaseRepository<T_DBModel, T_MappedEntity, T_Enforcement> {
async find(
query: FilterQuery<T_DBModel> & T_Enforcement,
select: ProjectionType<T_MappedEntity> = '',
options: { limit?: number; sort?: any; skip?: number } = {}
options: { limit?: number; sort?: any; skip?: number; readPreference?: 'secondaryPreferred' | 'primary' } = {}
): Promise<T_MappedEntity[]> {
const data = await this.MongooseModel.find(query, select, {
sort: options.sort || null,
})
.skip(options.skip as number)
.limit(options.limit as number)
.lean()
.read(options?.readPreference || 'primary')
.exec();

return this.mapEntities(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import {
NotificationTemplateRepository,
SubscriberRepository,
SubscriberPreferenceRepository,
} from '@novu/dal';
import { ISubscriberPreferenceResponse } from '@novu/shared';

Expand All @@ -18,7 +19,8 @@ export class GetSubscriberPreference {
private subscriberRepository: SubscriberRepository,
private notificationTemplateRepository: NotificationTemplateRepository,
private getSubscriberTemplatePreferenceUsecase: GetSubscriberTemplatePreference,
private analyticsService: AnalyticsService
private analyticsService: AnalyticsService,
private subscriberPreferenceRepository: SubscriberPreferenceRepository
) {}

async execute(
Expand All @@ -36,6 +38,18 @@ export class GetSubscriberPreference {
true
);

const subscriberPreference = await this.subscriberPreferenceRepository.find(
{
_environmentId: command.environmentId,
_subscriberId: subscriber._id,
_templateId: {
$in: templateList.map((template) => template._id),
},
},
'enabled channels _templateId',
{ readPreference: 'secondaryPreferred' }
);

this.analyticsService.mixpanelTrack(
'Fetch User Preferences - [Notification Center]',
'',
Expand All @@ -46,17 +60,22 @@ export class GetSubscriberPreference {
);

return await Promise.all(
templateList.map(async (template) =>
this.getSubscriberTemplatePreferenceUsecase.execute(
templateList.map(async (template) => {
const preference = subscriberPreference.find(
(i) => i._templateId === template._id
);

return this.getSubscriberTemplatePreferenceUsecase.execute(
GetSubscriberTemplatePreferenceCommand.create({
organizationId: command.organizationId,
subscriberId: command.subscriberId,
environmentId: command.environmentId,
template,
subscriber,
preference: preference || null,
})
)
)
);
})
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { NotificationTemplateEntity, SubscriberEntity } from '@novu/dal';
import {
NotificationTemplateEntity,
SubscriberEntity,
SubscriberPreferenceEntity,
} from '@novu/dal';
import { IsDefined, IsNotEmpty, IsOptional } from 'class-validator';

import { EnvironmentWithSubscriber } from '../../commands';
Expand All @@ -14,4 +18,10 @@ export class GetSubscriberTemplatePreferenceCommand extends EnvironmentWithSubsc

@IsOptional()
tenant?: ITenantDefine;

@IsOptional()
preference?: Pick<
SubscriberPreferenceEntity,
'channels' | '_templateId' | 'enabled'
>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ export class GetSubscriberTemplatePreference {

const initialActiveChannels = await this.getActiveChannels(command);
const subscriberPreference =
await this.subscriberPreferenceRepository.findOne(
{
_environmentId: command.environmentId,
_subscriberId: subscriber._id,
_templateId: command.template._id,
},
'enabled channels',
{ readPreference: 'secondaryPreferred' }
);
command.preference === undefined
? await this.subscriberPreferenceRepository.findOne(
{
_environmentId: command.environmentId,
_subscriberId: subscriber._id,
_templateId: command.template._id,
},
'enabled channels',
{ readPreference: 'secondaryPreferred' }
)
: command.preference;
const workflowOverride = await this.getWorkflowOverride(command);

const templateChannelPreference = command.template.preferenceSettings;
Expand Down Expand Up @@ -98,10 +100,13 @@ export class GetSubscriberTemplatePreference {
return null;
}

const tenant = await this.tenantRepository.findOne({
_environmentId: command.environmentId,
identifier: command.tenant.identifier,
});
const tenant = await this.tenantRepository.findOne(
{
_environmentId: command.environmentId,
identifier: command.tenant.identifier,
},
'_id'
);

if (!tenant) {
return null;
Expand Down
Loading