-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into nv-4097-controls-auto-complete-in-the-dashboard
- Loading branch information
Showing
71 changed files
with
772 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
apps/api/src/app/inbox/dtos/get-preferences-response.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IPreferenceChannels, IPreferenceOverride, ITemplateConfiguration, PreferenceLevelEnum } from '@novu/shared'; | ||
import { IsDefined, IsEnum, IsOptional } from 'class-validator'; | ||
|
||
export class GetPreferencesResponseDto { | ||
@IsDefined() | ||
@IsEnum({ | ||
enum: PreferenceLevelEnum, | ||
}) | ||
level: PreferenceLevelEnum; | ||
|
||
@IsOptional() | ||
workflow?: ITemplateConfiguration; | ||
|
||
@IsDefined() | ||
preferences: { enabled: boolean; channels: IPreferenceChannels; overrides?: IPreferenceOverride[] }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { SubscriberRepository } from '@novu/dal'; | ||
import { UserSession } from '@novu/testing'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Get all preferences - /inbox/preferences (GET)', function () { | ||
let session: UserSession; | ||
let subscriberRepository: SubscriberRepository; | ||
|
||
beforeEach(async () => { | ||
session = new UserSession(); | ||
subscriberRepository = new SubscriberRepository(); | ||
await session.initialize(); | ||
}); | ||
|
||
it('should always get the global preferences even if workflow preferences are not present', async function () { | ||
const response = await session.testAgent | ||
.get('/v1/inbox/preferences') | ||
.set('Authorization', `Bearer ${session.subscriberToken}`); | ||
|
||
const globalPreference = response.body.data[0]; | ||
|
||
expect(globalPreference.preferences.channels.email).to.equal(true); | ||
expect(globalPreference.preferences.channels.in_app).to.equal(true); | ||
expect(globalPreference.level).to.equal('global'); | ||
expect(response.body.data.length).to.equal(1); | ||
}); | ||
|
||
it('should get both global and workflow preferences if workflow is present', async function () { | ||
await session.createTemplate({ | ||
noFeedId: true, | ||
}); | ||
|
||
const response = await session.testAgent | ||
.get('/v1/inbox/preferences') | ||
.set('Authorization', `Bearer ${session.subscriberToken}`); | ||
|
||
const globalPreference = response.body.data[0]; | ||
|
||
expect(globalPreference.preferences.channels.email).to.equal(true); | ||
expect(globalPreference.preferences.channels.in_app).to.equal(true); | ||
expect(globalPreference.level).to.equal('global'); | ||
|
||
const workflowPreference = response.body.data[1]; | ||
|
||
expect(workflowPreference.preferences.channels.email).to.equal(true); | ||
expect(workflowPreference.preferences.channels.in_app).to.equal(true); | ||
expect(workflowPreference.level).to.equal('template'); | ||
}); | ||
|
||
it('should throw error when made unauthorized call', async function () { | ||
const response = await session.testAgent.get(`/v1/inbox/preferences`).set('Authorization', `Bearer InvalidToken`); | ||
|
||
expect(response.status).to.equal(401); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
apps/api/src/app/inbox/usecases/get-preferences/get-preferences.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { EnvironmentWithSubscriber } from '../../../shared/commands/project.command'; | ||
|
||
export class GetPreferencesCommand extends EnvironmentWithSubscriber {} |
155 changes: 155 additions & 0 deletions
155
apps/api/src/app/inbox/usecases/get-preferences/get-preferences.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { | ||
AnalyticsService, | ||
GetSubscriberGlobalPreference, | ||
GetSubscriberTemplatePreference, | ||
} from '@novu/application-generic'; | ||
import { NotificationTemplateRepository, SubscriberRepository } from '@novu/dal'; | ||
import { PreferenceLevelEnum } from '@novu/shared'; | ||
import { expect } from 'chai'; | ||
import { sub } from 'date-fns'; | ||
import sinon from 'sinon'; | ||
import { AnalyticsEventsEnum } from '../../utils'; | ||
import { GetPreferences } from './get-preferences.usecase'; | ||
|
||
const mockedSubscriber: any = { _id: '123', subscriberId: 'test-mockSubscriber', firstName: 'test', lastName: 'test' }; | ||
const mockedWorkflowPreference: any = { | ||
template: {}, | ||
preference: { | ||
enabled: true, | ||
channels: { | ||
email: true, | ||
in_app: true, | ||
sms: false, | ||
push: false, | ||
chat: true, | ||
}, | ||
overrides: { | ||
email: false, | ||
in_app: false, | ||
sms: true, | ||
push: true, | ||
chat: false, | ||
}, | ||
}, | ||
}; | ||
|
||
const mockedGlobalPreferences: any = { | ||
preference: { | ||
enabled: true, | ||
channels: { | ||
email: true, | ||
in_app: true, | ||
sms: false, | ||
push: false, | ||
chat: true, | ||
}, | ||
}, | ||
}; | ||
const mockedPreferencesResponse: any = [ | ||
{ level: PreferenceLevelEnum.GLOBAL, preferences: mockedGlobalPreferences.preference }, | ||
{ | ||
level: PreferenceLevelEnum.TEMPLATE, | ||
preferences: mockedWorkflowPreference.preference, | ||
workflow: mockedWorkflowPreference.template, | ||
}, | ||
]; | ||
|
||
const mockedWorkflow: any = [{}]; | ||
|
||
describe('GetPreferences', () => { | ||
let getPreferences: GetPreferences; | ||
let subscriberRepositoryMock: sinon.SinonStubbedInstance<SubscriberRepository>; | ||
let getSubscriberWorkflowMock: sinon.SinonStubbedInstance<GetSubscriberTemplatePreference>; | ||
let analyticsServiceMock: sinon.SinonStubbedInstance<AnalyticsService>; | ||
let getSubscriberGlobalPreferenceMock: sinon.SinonStubbedInstance<GetSubscriberGlobalPreference>; | ||
let notificationTemplateRepositoryMock: sinon.SinonStubbedInstance<NotificationTemplateRepository>; | ||
|
||
beforeEach(() => { | ||
subscriberRepositoryMock = sinon.createStubInstance(SubscriberRepository); | ||
getSubscriberWorkflowMock = sinon.createStubInstance(GetSubscriberTemplatePreference); | ||
analyticsServiceMock = sinon.createStubInstance(AnalyticsService); | ||
getSubscriberGlobalPreferenceMock = sinon.createStubInstance(GetSubscriberGlobalPreference); | ||
notificationTemplateRepositoryMock = sinon.createStubInstance(NotificationTemplateRepository); | ||
|
||
getPreferences = new GetPreferences( | ||
subscriberRepositoryMock as any, | ||
notificationTemplateRepositoryMock as any, | ||
getSubscriberWorkflowMock as any, | ||
getSubscriberGlobalPreferenceMock as any, | ||
analyticsServiceMock as any | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('it should throw exception when subscriber is not found', async () => { | ||
const command = { | ||
environmentId: 'env-1', | ||
organizationId: 'org-1', | ||
subscriberId: 'not-found', | ||
}; | ||
|
||
subscriberRepositoryMock.findOne.resolves(undefined); | ||
|
||
try { | ||
await getPreferences.execute(command); | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(Error); | ||
expect(error.message).to.equal(`Subscriber with id: ${command.subscriberId} not found`); | ||
} | ||
}); | ||
|
||
it('it should return subscriber preferences', async () => { | ||
const command = { | ||
environmentId: 'env-1', | ||
organizationId: 'org-1', | ||
subscriberId: 'test-mockSubscriber', | ||
}; | ||
|
||
subscriberRepositoryMock.findBySubscriberId.resolves(mockedSubscriber); | ||
getSubscriberGlobalPreferenceMock.execute.resolves(mockedGlobalPreferences); | ||
notificationTemplateRepositoryMock.getActiveList.resolves(mockedWorkflow); | ||
getSubscriberWorkflowMock.execute.resolves(mockedWorkflowPreference); | ||
|
||
const result = await getPreferences.execute(command); | ||
|
||
expect(subscriberRepositoryMock.findBySubscriberId.calledOnce).to.be.true; | ||
expect(subscriberRepositoryMock.findBySubscriberId.firstCall.args).to.deep.equal([ | ||
command.environmentId, | ||
command.subscriberId, | ||
]); | ||
expect(getSubscriberGlobalPreferenceMock.execute.calledOnce).to.be.true; | ||
expect(getSubscriberGlobalPreferenceMock.execute.firstCall.args).to.deep.equal([command]); | ||
expect(notificationTemplateRepositoryMock.getActiveList.calledOnce).to.be.true; | ||
expect(notificationTemplateRepositoryMock.getActiveList.firstCall.args).to.deep.equal([ | ||
command.organizationId, | ||
command.environmentId, | ||
true, | ||
]); | ||
expect(getSubscriberWorkflowMock.execute.calledOnce).to.be.true; | ||
expect(getSubscriberWorkflowMock.execute.firstCall.args).to.deep.equal([ | ||
{ | ||
organizationId: command.organizationId, | ||
subscriberId: command.subscriberId, | ||
environmentId: command.environmentId, | ||
template: mockedWorkflow[0], | ||
subscriber: mockedSubscriber, | ||
}, | ||
]); | ||
|
||
expect(analyticsServiceMock.mixpanelTrack.calledOnce).to.be.true; | ||
expect(analyticsServiceMock.mixpanelTrack.firstCall.args).to.deep.equal([ | ||
AnalyticsEventsEnum.FETCH_PREFERENCES, | ||
'', | ||
{ | ||
_organization: command.organizationId, | ||
subscriberId: command.subscriberId, | ||
workflowSize: 1, | ||
}, | ||
]); | ||
|
||
expect(result).to.deep.equal(mockedPreferencesResponse); | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
apps/api/src/app/inbox/usecases/get-preferences/get-preferences.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { | ||
AnalyticsService, | ||
GetSubscriberGlobalPreference, | ||
GetSubscriberGlobalPreferenceCommand, | ||
GetSubscriberTemplatePreference, | ||
GetSubscriberTemplatePreferenceCommand, | ||
} from '@novu/application-generic'; | ||
import { NotificationTemplateRepository, SubscriberRepository } from '@novu/dal'; | ||
import { ISubscriberPreferences, PreferenceLevelEnum } from '@novu/shared'; | ||
import { AnalyticsEventsEnum } from '../../utils'; | ||
import { GetPreferencesCommand } from './get-preferences.command'; | ||
|
||
@Injectable() | ||
export class GetPreferences { | ||
constructor( | ||
private subscriberRepository: SubscriberRepository, | ||
private notificationTemplateRepository: NotificationTemplateRepository, | ||
private getSubscriberTemplatePreferenceUsecase: GetSubscriberTemplatePreference, | ||
private getSubscriberGlobalPreference: GetSubscriberGlobalPreference, | ||
private analyticsService: AnalyticsService | ||
) {} | ||
|
||
async execute(command: GetPreferencesCommand): Promise<ISubscriberPreferences[]> { | ||
const subscriber = await this.subscriberRepository.findBySubscriberId(command.environmentId, command.subscriberId); | ||
|
||
if (!subscriber) { | ||
throw new NotFoundException(`Subscriber with id: ${command.subscriberId} not found`); | ||
} | ||
|
||
const globalPreference = await this.getSubscriberGlobalPreference.execute( | ||
GetSubscriberGlobalPreferenceCommand.create({ | ||
organizationId: command.organizationId, | ||
environmentId: command.environmentId, | ||
subscriberId: command.subscriberId, | ||
}) | ||
); | ||
|
||
const updatedGlobalPreference = { | ||
level: PreferenceLevelEnum.GLOBAL, | ||
preferences: globalPreference.preference, | ||
}; | ||
|
||
const workflowList = | ||
(await this.notificationTemplateRepository.getActiveList(command.organizationId, command.environmentId, true)) || | ||
[]; | ||
|
||
this.analyticsService.mixpanelTrack(AnalyticsEventsEnum.FETCH_PREFERENCES, '', { | ||
_organization: command.organizationId, | ||
subscriberId: command.subscriberId, | ||
workflowSize: workflowList.length, | ||
}); | ||
|
||
const workflowPreferences = await Promise.all( | ||
workflowList.map(async (workflow) => { | ||
const workflowPreference = await this.getSubscriberTemplatePreferenceUsecase.execute( | ||
GetSubscriberTemplatePreferenceCommand.create({ | ||
organizationId: command.organizationId, | ||
subscriberId: command.subscriberId, | ||
environmentId: command.environmentId, | ||
template: workflow, | ||
subscriber, | ||
}) | ||
); | ||
|
||
return { | ||
level: PreferenceLevelEnum.TEMPLATE, | ||
preferences: workflowPreference.preference, | ||
workflow: workflowPreference.template, | ||
}; | ||
}) | ||
); | ||
|
||
return [updatedGlobalPreference, ...workflowPreferences]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.