From 5cafc0599b567c1a6d37766b76ddc463bac99e68 Mon Sep 17 00:00:00 2001 From: Dima Grossman Date: Mon, 6 May 2024 22:19:16 +0300 Subject: [PATCH] Revert "perf: improve mongodb queries (#5508)" This reverts commit f60fc3e4552e7001f595194e0219d07977ede763. --- libs/dal/src/repositories/base-repository.ts | 3 +- .../get-subscriber-preference.usecase.ts | 29 +++-------------- ...-subscriber-template-preference.command.ts | 12 +------ ...-subscriber-template-preference.usecase.ts | 31 ++++++++----------- 4 files changed, 20 insertions(+), 55 deletions(-) diff --git a/libs/dal/src/repositories/base-repository.ts b/libs/dal/src/repositories/base-repository.ts index b7bd3072b95..bc185020819 100644 --- a/libs/dal/src/repositories/base-repository.ts +++ b/libs/dal/src/repositories/base-repository.ts @@ -72,7 +72,7 @@ export class BaseRepository { async find( query: FilterQuery & T_Enforcement, select: ProjectionType = '', - options: { limit?: number; sort?: any; skip?: number; readPreference?: 'secondaryPreferred' | 'primary' } = {} + options: { limit?: number; sort?: any; skip?: number } = {} ): Promise { const data = await this.MongooseModel.find(query, select, { sort: options.sort || null, @@ -80,7 +80,6 @@ export class BaseRepository { .skip(options.skip as number) .limit(options.limit as number) .lean() - .read(options?.readPreference || 'primary') .exec(); return this.mapEntities(data); diff --git a/packages/application-generic/src/usecases/get-subscriber-preference/get-subscriber-preference.usecase.ts b/packages/application-generic/src/usecases/get-subscriber-preference/get-subscriber-preference.usecase.ts index 7eb4823f436..adf94ec77f0 100644 --- a/packages/application-generic/src/usecases/get-subscriber-preference/get-subscriber-preference.usecase.ts +++ b/packages/application-generic/src/usecases/get-subscriber-preference/get-subscriber-preference.usecase.ts @@ -2,7 +2,6 @@ import { Injectable } from '@nestjs/common'; import { NotificationTemplateRepository, SubscriberRepository, - SubscriberPreferenceRepository, } from '@novu/dal'; import { ISubscriberPreferenceResponse } from '@novu/shared'; @@ -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( @@ -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]', '', @@ -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, }) - ); - }) + ) + ) ); } } diff --git a/packages/application-generic/src/usecases/get-subscriber-template-preference/get-subscriber-template-preference.command.ts b/packages/application-generic/src/usecases/get-subscriber-template-preference/get-subscriber-template-preference.command.ts index 344653a5be6..d875c76a829 100644 --- a/packages/application-generic/src/usecases/get-subscriber-template-preference/get-subscriber-template-preference.command.ts +++ b/packages/application-generic/src/usecases/get-subscriber-template-preference/get-subscriber-template-preference.command.ts @@ -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'; @@ -18,10 +14,4 @@ export class GetSubscriberTemplatePreferenceCommand extends EnvironmentWithSubsc @IsOptional() tenant?: ITenantDefine; - - @IsOptional() - preference?: Pick< - SubscriberPreferenceEntity, - 'channels' | '_templateId' | 'enabled' - >; } diff --git a/packages/application-generic/src/usecases/get-subscriber-template-preference/get-subscriber-template-preference.usecase.ts b/packages/application-generic/src/usecases/get-subscriber-template-preference/get-subscriber-template-preference.usecase.ts index e1f7ea8be2b..9d11366046d 100644 --- a/packages/application-generic/src/usecases/get-subscriber-template-preference/get-subscriber-template-preference.usecase.ts +++ b/packages/application-generic/src/usecases/get-subscriber-template-preference/get-subscriber-template-preference.usecase.ts @@ -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; @@ -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;