Skip to content

Commit

Permalink
Revert "perf: improve mongodb queries (#5508)"
Browse files Browse the repository at this point in the history
This reverts commit f60fc3e.
  • Loading branch information
scopsy committed May 6, 2024
1 parent f60fc3e commit 5cafc05
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 55 deletions.
3 changes: 1 addition & 2 deletions libs/dal/src/repositories/base-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ 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; readPreference?: 'secondaryPreferred' | 'primary' } = {}
options: { limit?: number; sort?: any; skip?: number } = {}
): 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,7 +2,6 @@ import { Injectable } from '@nestjs/common';
import {
NotificationTemplateRepository,
SubscriberRepository,
SubscriberPreferenceRepository,
} from '@novu/dal';
import { ISubscriberPreferenceResponse } from '@novu/shared';

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

async execute(
Expand All @@ -38,18 +36,6 @@ 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 @@ -60,22 +46,17 @@ export class GetSubscriberPreference {
);

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

return this.getSubscriberTemplatePreferenceUsecase.execute(
templateList.map(async (template) =>
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,8 +1,4 @@
import {
NotificationTemplateEntity,
SubscriberEntity,
SubscriberPreferenceEntity,
} from '@novu/dal';
import { NotificationTemplateEntity, SubscriberEntity } from '@novu/dal';
import { IsDefined, IsNotEmpty, IsOptional } from 'class-validator';

import { EnvironmentWithSubscriber } from '../../commands';
Expand All @@ -18,10 +14,4 @@ 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,17 +56,15 @@ export class GetSubscriberTemplatePreference {

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

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

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

if (!tenant) {
return null;
Expand Down

0 comments on commit 5cafc05

Please sign in to comment.