diff --git a/apps/api/src/app/inbox/usecases/update-preferences/update-preferences.spec.ts b/apps/api/src/app/inbox/usecases/update-preferences/update-preferences.spec.ts index 091f865b27b..a95cbb8ac0b 100644 --- a/apps/api/src/app/inbox/usecases/update-preferences/update-preferences.spec.ts +++ b/apps/api/src/app/inbox/usecases/update-preferences/update-preferences.spec.ts @@ -128,51 +128,7 @@ describe('UpdatePreferences', () => { } }); - it('should create user preference if absent', async () => { - const command = { - environmentId: 'env-1', - organizationId: 'org-1', - subscriberId: 'test-mockSubscriber', - level: PreferenceLevelEnum.GLOBAL, - chat: true, - }; - - subscriberRepositoryMock.findBySubscriberId.resolves(mockedSubscriber); - subscriberPreferenceRepositoryMock.findOne.resolves(undefined); - getSubscriberGlobalPreferenceMock.execute.resolves(mockedGlobalPreference); - - const result = await updatePreferences.execute(command); - - expect(getSubscriberGlobalPreferenceMock.execute.called).to.be.true; - expect(getSubscriberGlobalPreferenceMock.execute.lastCall.args).to.deep.equal([ - GetSubscriberGlobalPreferenceCommand.create({ - environmentId: command.environmentId, - organizationId: command.organizationId, - subscriberId: mockedSubscriber.subscriberId, - }), - ]); - - expect(analyticsServiceMock.mixpanelTrack.firstCall.args).to.deep.equal([ - AnalyticsEventsEnum.CREATE_PREFERENCES, - '', - { - _organization: command.organizationId, - _subscriber: mockedSubscriber._id, - level: command.level, - _workflowId: undefined, - channels: { - chat: true, - }, - }, - ]); - - expect(result).to.deep.equal({ - level: command.level, - ...mockedGlobalPreference.preference, - }); - }); - - it('should update user preference if preference exists', async () => { + it('should update subscriber preference', async () => { const command = { environmentId: 'env-1', organizationId: 'org-1', @@ -216,7 +172,7 @@ describe('UpdatePreferences', () => { }); }); - it('should update user preference if preference exists and level is template', async () => { + it('should update subscriber preference if preference exists and level is template', async () => { const command = { environmentId: 'env-1', organizationId: 'org-1', diff --git a/apps/api/src/app/inbox/usecases/update-preferences/update-preferences.usecase.ts b/apps/api/src/app/inbox/usecases/update-preferences/update-preferences.usecase.ts index 96fa65a3f49..0ae63fb2f8d 100644 --- a/apps/api/src/app/inbox/usecases/update-preferences/update-preferences.usecase.ts +++ b/apps/api/src/app/inbox/usecases/update-preferences/update-preferences.usecase.ts @@ -16,7 +16,6 @@ import { NotificationTemplateRepository, PreferenceLevelEnum, SubscriberEntity, - SubscriberPreferenceEntity, SubscriberPreferenceRepository, SubscriberRepository, } from '@novu/dal'; @@ -56,46 +55,16 @@ export class UpdatePreferences { } } - const subscriberPreference = await this.findPreference(command, subscriber); - if (!subscriberPreference) { - await this.createUserPreference(command, subscriber); - } else { - await this.updateUserPreference(command, subscriber); - } + await this.updateSubscriberPreference(command, subscriber); return await this.findPreference(command, subscriber); } @Instrument() - private async createUserPreference(command: UpdatePreferencesCommand, subscriber: SubscriberEntity): Promise { - const channelPreferences: IPreferenceChannels = this.buildPreferenceChannels(command); - - await this.storePreferencesV2({ - channels: channelPreferences, - organizationId: command.organizationId, - environmentId: command.environmentId, - _subscriberId: subscriber._id, - templateId: command.workflowId, - }); - - this.analyticsService.mixpanelTrack(AnalyticsEventsEnum.CREATE_PREFERENCES, '', { - _organization: command.organizationId, - _subscriber: subscriber._id, - _workflowId: command.workflowId, - level: command.level, - channels: channelPreferences, - }); - - const query = this.commonQuery(command, subscriber); - await this.subscriberPreferenceRepository.create({ - ...query, - enabled: true, - channels: channelPreferences, - }); - } - - @Instrument() - private async updateUserPreference(command: UpdatePreferencesCommand, subscriber: SubscriberEntity): Promise { + private async updateSubscriberPreference( + command: UpdatePreferencesCommand, + subscriber: SubscriberEntity + ): Promise { const channelPreferences: IPreferenceChannels = this.buildPreferenceChannels(command); await this.storePreferencesV2({ diff --git a/apps/api/src/app/inbox/utils/analytics.ts b/apps/api/src/app/inbox/utils/analytics.ts index 4952783f8a7..1333e0934cb 100644 --- a/apps/api/src/app/inbox/utils/analytics.ts +++ b/apps/api/src/app/inbox/utils/analytics.ts @@ -6,5 +6,4 @@ export enum AnalyticsEventsEnum { UPDATE_ALL_NOTIFICATIONS = 'Update All Notifications - [Inbox]', FETCH_PREFERENCES = 'Fetch Preferences - [Inbox]', UPDATE_PREFERENCES = 'Update Preferences - [Inbox]', - CREATE_PREFERENCES = 'Create Preferences - [Inbox]', }