Skip to content

Commit 5118e7f

Browse files
committed
fix: webhook app id
1 parent 3913515 commit 5118e7f

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

apps/api/src/app/webhooks/usecases/create-webhook-portal-token/create-webhook-portal.usecase.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ export class CreateWebhookPortalUsecase {
3535
try {
3636
const app = await this.svix.application.create({
3737
name: organization.name,
38-
uid: command.environmentId,
38+
uid: `${command.organizationId}-${command.environmentId}`,
3939
metadata: {
4040
environmentId: command.environmentId,
41+
organizationId: command.organizationId,
4142
},
4243
});
4344

45+
await this.environmentRepository.updateOne({ _id: command.environmentId }, { $set: { webhookAppId: app.uid } });
46+
4447
return {
4548
appId: app.uid!,
4649
};

libs/application-generic/src/webhooks/usecases/send-webhook-message/send-webhook-message.usecase.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Svix } from 'svix';
33
import shortid from 'shortid';
44

55
import { PinoLogger } from 'nestjs-pino';
6+
import { EnvironmentRepository } from '@novu/dal';
67
import { SendWebhookMessageCommand } from './send-webhook-message.command';
78
import { WrapperDto } from '../../dtos/webhook-payload.dto';
89

@@ -12,13 +13,32 @@ const LOG_CONTEXT = 'SendWebhookMessageUseCase';
1213
export class SendWebhookMessage {
1314
constructor(
1415
@Inject('SVIX_CLIENT') private svix: Svix,
15-
private logger: PinoLogger
16+
private logger: PinoLogger,
17+
private environmentRepository: EnvironmentRepository
1618
) {
1719
this.logger.setContext(LOG_CONTEXT);
1820
}
1921

2022
async execute(command: SendWebhookMessageCommand): Promise<{ eventId: string } | undefined> {
2123
const eventId = `evt_${shortid.generate()}`;
24+
const environment = await this.environmentRepository.findOne(
25+
{
26+
_id: command.environmentId,
27+
},
28+
'webhookAppId'
29+
);
30+
31+
if (!environment) {
32+
throw new Error(`Environment not found for id ${command.environmentId}`);
33+
}
34+
35+
const appId = environment.webhookAppId;
36+
37+
if (!appId) {
38+
this.logger.debug(`Webhook app ID not found for environment ${command.environmentId}, Event ID: ${eventId}`);
39+
40+
return;
41+
}
2242

2343
const webhookPayload: WrapperDto<any> = {
2444
type: command.eventType,
@@ -33,7 +53,7 @@ export class SendWebhookMessage {
3353
`Attempting to send webhook ${command.eventType} for application ${command.organizationId}-${command.environmentId}, Event ID: ${eventId}`
3454
);
3555

36-
const message = await this.svix.message.create(`${command.organizationId}-${command.environmentId}`, {
56+
const message = await this.svix.message.create(appId, {
3757
eventType: command.eventType,
3858
eventId,
3959
payload: webhookPayload,

libs/dal/src/repositories/environment/environment.entity.ts

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export class EnvironmentEntity {
5353
url: string;
5454
};
5555

56+
webhookAppId?: string;
57+
5658
createdAt?: string;
5759

5860
updatedAt?: string;

libs/dal/src/repositories/environment/environment.schema.ts

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const environmentSchema = new Schema<EnvironmentDBModel>(
5353
bridge: {
5454
url: Schema.Types.String,
5555
},
56+
webhookAppId: {
57+
type: Schema.Types.String,
58+
},
5659
_parentId: {
5760
type: Schema.Types.ObjectId,
5861
ref: 'Environment',

0 commit comments

Comments
 (0)