Skip to content

Commit

Permalink
perf: improve mongodb queries
Browse files Browse the repository at this point in the history
  • Loading branch information
scopsy committed May 6, 2024
1 parent 8d402fd commit 81ac5ec
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
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

0 comments on commit 81ac5ec

Please sign in to comment.