Skip to content

Commit

Permalink
Merge pull request #4616 from novuhq/nv-3019-cancel-trigger-api-is-no…
Browse files Browse the repository at this point in the history
…t-cancelling-all-events-related-to
  • Loading branch information
scopsy authored Oct 22, 2023
2 parents 11e9b3f + 0202474 commit 494d848
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .commitlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"subject-case": [
2,
"always",
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case"]
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case", "camel-case"]
],
"type-enum": [
2,
Expand Down
23 changes: 14 additions & 9 deletions apps/api/src/app/events/e2e/delay-events.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
let standardQueueService: StandardQueueService;
const messageRepository = new MessageRepository();

const triggerEvent = async (payload, transactionId?: string, overrides = {}) => {
const triggerEvent = async (payload, transactionId?: string, overrides = {}, to = [subscriber.subscriberId]) => {
await axiosInstance.post(
`${session.serverUrl}/v1/events/trigger`,
{
transactionId,
name: template.triggers[0].identifier,
to: [subscriber.subscriberId],
to,
payload,
overrides,
},
Expand Down Expand Up @@ -330,6 +330,8 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
});

it('should be able to cancel delay', async function () {
const secondSubscriber = await subscriberService.createSubscriber();

const id = MessageRepository.createObjectId();
template = await session.createTemplate({
steps: [
Expand All @@ -342,7 +344,7 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
content: '',
metadata: {
unit: DigestUnitEnum.SECONDS,
amount: 0.1,
amount: 5,
type: DelayTypeEnum.REGULAR,
},
},
Expand All @@ -357,17 +359,19 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
{
customVar: 'Testing of User Name',
},
id
id,
{},
[subscriber.subscriberId, secondSubscriber.subscriberId]
);

await session.awaitRunningJobs(template?._id, true, 1);
await session.awaitRunningJobs(template?._id, true, 2);
await axiosInstance.delete(`${session.serverUrl}/v1/events/trigger/${id}`, {
headers: {
authorization: `ApiKey ${session.apiKey}`,
},
});

let delayedJob = await jobRepository.findOne({
let delayedJobs = await jobRepository.find({
_environmentId: session.environment._id,
_templateId: template._id,
type: StepTypeEnum.DELAY,
Expand All @@ -380,14 +384,15 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
transactionId: id,
});

expect(pendingJobs).to.equal(1);
expect(pendingJobs).to.equal(2);

delayedJob = await jobRepository.findOne({
delayedJobs = await jobRepository.find({
_environmentId: session.environment._id,
_templateId: template._id,
type: StepTypeEnum.DELAY,
transactionId: id,
});
expect(delayedJob!.status).to.equal(JobStatusEnum.CANCELED);
expect(delayedJobs[0]!.status).to.equal(JobStatusEnum.CANCELED);
expect(delayedJobs[1]!.status).to.equal(JobStatusEnum.CANCELED);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ export class CancelDelayed {
constructor(private jobRepository: JobRepository) {}

public async execute(command: CancelDelayedCommand): Promise<boolean> {
const job = await this.jobRepository.findOne({
_environmentId: command.environmentId,
transactionId: command.transactionId,
status: JobStatusEnum.DELAYED,
});
const jobs = await this.jobRepository.find(
{
_environmentId: command.environmentId,
transactionId: command.transactionId,
status: JobStatusEnum.DELAYED,
},
'_id'
);

if (!job) {
if (!jobs?.length) {
return false;
}

await this.jobRepository.update(
{
_environmentId: command.environmentId,
_id: job._id,
_id: {
$in: jobs.map((job) => job._id),
},
},
{
$set: {
Expand Down
81 changes: 22 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 494d848

Please sign in to comment.