-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(worker): timed digest event merging (#4979)
* fix: timed digest event gathering * feat: remove un needed digest filtering steps * fix: spelling * fix: object id typing * fix: increase timeout * chore(api): remove mocha timeout on package.json scripts --------- Co-authored-by: Paweł <pawel.tymczuk@gmail.com>
- Loading branch information
Showing
14 changed files
with
207 additions
and
341 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
.idea/runConfigurations/_template__of_mocha_javascript_test_runner.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"timeout": 10000, | ||
"timeout": 20000, | ||
"require": "ts-node/register", | ||
"file": ["e2e/setup.ts"], | ||
"exit": true, | ||
|
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 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,121 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import axios from 'axios'; | ||
import { expect } from 'chai'; | ||
import { | ||
MessageRepository, | ||
NotificationTemplateEntity, | ||
SubscriberEntity, | ||
JobRepository, | ||
JobStatusEnum, | ||
JobEntity, | ||
} from '@novu/dal'; | ||
import { StepTypeEnum, DigestTypeEnum, DigestUnitEnum, IDigestRegularMetadata } from '@novu/shared'; | ||
import { UserSession, SubscribersService } from '@novu/testing'; | ||
|
||
const axiosInstance = axios.create(); | ||
|
||
const promiseTimeout = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
describe('Trigger event - Scheduled Digest Mode - /v1/events/trigger (POST)', function () { | ||
let session: UserSession; | ||
let template: NotificationTemplateEntity; | ||
let subscriber: SubscriberEntity; | ||
let subscriberService: SubscribersService; | ||
const jobRepository = new JobRepository(); | ||
const messageRepository = new MessageRepository(); | ||
|
||
const triggerEvent = async (payload, transactionId?: string): Promise<void> => { | ||
await axiosInstance.post( | ||
`${session.serverUrl}/v1/events/trigger`, | ||
{ | ||
transactionId, | ||
name: template.triggers[0].identifier, | ||
to: [subscriber.subscriberId], | ||
payload, | ||
}, | ||
{ | ||
headers: { | ||
authorization: `ApiKey ${session.apiKey}`, | ||
}, | ||
} | ||
); | ||
}; | ||
|
||
beforeEach(async () => { | ||
session = new UserSession(); | ||
await session.initialize(); | ||
template = await session.createTemplate(); | ||
subscriberService = new SubscribersService(session.organization._id, session.environment._id); | ||
subscriber = await subscriberService.createSubscriber(); | ||
}); | ||
|
||
it('should digest events using a scheduled digest', async () => { | ||
template = await session.createTemplate({ | ||
steps: [ | ||
{ | ||
type: StepTypeEnum.DIGEST, | ||
content: '', | ||
metadata: { | ||
unit: DigestUnitEnum.MINUTES, | ||
amount: 1, | ||
type: DigestTypeEnum.TIMED, | ||
}, | ||
}, | ||
{ | ||
type: StepTypeEnum.IN_APP, | ||
content: 'Hello world {{step.events.length}}' as string, | ||
}, | ||
], | ||
}); | ||
|
||
const events = [ | ||
{ customVar: 'Testing of User Name' }, | ||
{ customVar: 'digest' }, | ||
{ customVar: 'merged' }, | ||
{ customVar: 'digest' }, | ||
{ customVar: 'merged' }, | ||
{ customVar: 'digest' }, | ||
{ customVar: 'merged' }, | ||
]; | ||
|
||
await Promise.all(events.map((event) => triggerEvent(event))); | ||
|
||
const handler = await session.awaitRunningJobs(template?._id, false, 1); | ||
|
||
await handler.runDelayedImmediately(); | ||
await session.awaitRunningJobs(template?._id); | ||
|
||
const jobs = await jobRepository.find({ | ||
_environmentId: session.environment._id, | ||
_templateId: template._id, | ||
_subscriberId: subscriber._id, | ||
type: StepTypeEnum.DIGEST, | ||
}); | ||
|
||
expect(jobs && jobs.length).to.eql(7); | ||
|
||
const completedJob = jobs.find((elem) => elem.status === JobStatusEnum.COMPLETED); | ||
expect(completedJob).to.ok; | ||
|
||
const mergedJob = jobs.find((elem) => elem.status === JobStatusEnum.MERGED); | ||
expect(mergedJob).to.ok; | ||
|
||
const generatedMessageJob = await jobRepository.find({ | ||
_environmentId: session.environment._id, | ||
_templateId: template._id, | ||
_subscriberId: subscriber._id, | ||
type: StepTypeEnum.IN_APP, | ||
}); | ||
|
||
expect(generatedMessageJob && generatedMessageJob.length).to.equal(7); | ||
|
||
const mergedInApp = generatedMessageJob.filter((elem) => elem.status === JobStatusEnum.MERGED); | ||
expect(mergedInApp && mergedInApp.length).to.equal(6); | ||
|
||
const completedInApp = generatedMessageJob.filter((elem) => elem.status === JobStatusEnum.COMPLETED); | ||
expect(completedInApp && completedInApp.length).to.equal(1); | ||
|
||
const digestEventLength = completedInApp.find((i) => i.digest?.events?.length === 7); | ||
expect(digestEventLength).to.be.ok; | ||
}); | ||
}); |
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 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 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 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 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.