-
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.
feat(worker): refactor get digest events flow
- Loading branch information
1 parent
fda0dc9
commit c3dfd91
Showing
6 changed files
with
91 additions
and
47 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
apps/worker/src/app/workflow/usecases/send-message/digest/digest-events.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,11 @@ | ||
import { IsDefined } from 'class-validator'; | ||
import { JobEntity } from '@novu/dal'; | ||
import { BaseCommand } from '@novu/application-generic'; | ||
|
||
export class DigestEventsCommand extends BaseCommand { | ||
@IsDefined() | ||
_subscriberId: string; | ||
|
||
@IsDefined() | ||
currentJob: JobEntity; | ||
} |
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
45 changes: 20 additions & 25 deletions
45
...worker/src/app/workflow/usecases/send-message/digest/get-digest-events-regular.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 |
---|---|---|
@@ -1,47 +1,42 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { Injectable } from '@nestjs/common'; | ||
import { sub } from 'date-fns'; | ||
import { IDigestRegularMetadata } from '@novu/shared'; | ||
import { DigestFilterSteps, InstrumentUsecase } from '@novu/application-generic'; | ||
import { InstrumentUsecase } from '@novu/application-generic'; | ||
|
||
import { PlatformException } from '../../../../shared/utils'; | ||
import { SendMessageCommand } from '../send-message.command'; | ||
import { DigestEventsCommand } from './digest-events.command'; | ||
import { GetDigestEvents } from './get-digest-events.usecase'; | ||
|
||
@Injectable() | ||
export class GetDigestEventsRegular extends GetDigestEvents { | ||
@InstrumentUsecase() | ||
public async execute(command: SendMessageCommand) { | ||
const currentJob = await this.jobRepository.findOne({ | ||
_environmentId: command.environmentId, | ||
_id: command.jobId, | ||
}); | ||
if (!currentJob) throw new PlatformException('Digest job is not found'); | ||
public async execute(command: DigestEventsCommand) { | ||
const currentJob = command.currentJob; | ||
|
||
const digestMeta = currentJob.digest as IDigestRegularMetadata | undefined; | ||
const amount = | ||
typeof digestMeta?.amount === 'number' | ||
const { digestKey, digestMeta, digestValue } = this.getJobDigest(currentJob); | ||
|
||
const amount = digestMeta | ||
? typeof digestMeta.amount === 'number' | ||
? digestMeta.amount | ||
: // @ts-ignore | ||
parseInt(digestMeta?.amount, 10); | ||
const earliest = sub(new Date(currentJob.createdAt), { | ||
// @ts-ignore | ||
[digestMeta?.unit]: amount, | ||
}); | ||
: parseInt(digestMeta.amount, 10) | ||
: undefined; | ||
|
||
const digestKey = digestMeta?.digestKey; | ||
const digestValue = DigestFilterSteps.getNestedValue(currentJob.payload, digestKey); | ||
const createdDate = new Date(currentJob.createdAt); | ||
const subtractedTime = digestMeta | ||
? { | ||
[digestMeta.unit]: amount, | ||
} | ||
: {}; | ||
const earliest = sub(new Date(currentJob.createdAt), subtractedTime); | ||
|
||
const jobs = await this.jobRepository.findJobsToDigest( | ||
earliest, | ||
currentJob._templateId, | ||
command.environmentId, | ||
// backward compatibility - ternary needed to be removed once the queue renewed | ||
command._subscriberId ? command._subscriberId : command.subscriberId, | ||
currentJob._environmentId, | ||
command._subscriberId, | ||
digestKey, | ||
digestValue | ||
); | ||
|
||
return this.filterJobs(currentJob, command.transactionId, jobs); | ||
return this.filterJobs(currentJob, currentJob.transactionId, jobs); | ||
} | ||
} |
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