Skip to content

Commit 3913515

Browse files
committed
fix: minor refactoring
1 parent 1bbb9b1 commit 3913515

File tree

6 files changed

+98
-25
lines changed

6 files changed

+98
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
3+
export class CreateWebhookPortalResponseDto {
4+
@ApiProperty({
5+
description: 'The webhook portal application ID',
6+
})
7+
appId: string;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { EnvironmentWithUserCommand } from '../../../shared/commands/project.command';
2+
3+
export class CreateWebhookPortalCommand extends EnvironmentWithUserCommand {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Inject, Injectable, NotFoundException, Scope } from '@nestjs/common';
2+
import { EnvironmentRepository, OrganizationRepository } from '@novu/dal';
3+
import { LogDecorator } from '@novu/application-generic';
4+
import { Svix } from 'svix';
5+
6+
import { CreateWebhookPortalCommand } from './create-webhook-portal.command';
7+
import { CreateWebhookPortalResponseDto } from '../../dtos/create-webhook-portal-token-response.dto';
8+
9+
@Injectable()
10+
export class CreateWebhookPortalUsecase {
11+
constructor(
12+
private environmentRepository: EnvironmentRepository,
13+
@Inject('SVIX_CLIENT') private svix: Svix,
14+
private organizationRepository: OrganizationRepository
15+
) {}
16+
17+
@LogDecorator()
18+
async execute(command: CreateWebhookPortalCommand): Promise<CreateWebhookPortalResponseDto> {
19+
const environment = await this.environmentRepository.findOne({
20+
_id: command.environmentId,
21+
_organizationId: command.organizationId,
22+
});
23+
24+
if (!environment) {
25+
throw new NotFoundException(
26+
`Environment not found for id ${command.environmentId} and organization ${command.organizationId}`
27+
);
28+
}
29+
30+
const organization = await this.organizationRepository.findById(command.organizationId);
31+
if (!organization) {
32+
throw new NotFoundException(`Organization not found for id ${command.organizationId}`);
33+
}
34+
35+
try {
36+
const app = await this.svix.application.create({
37+
name: organization.name,
38+
uid: command.environmentId,
39+
metadata: {
40+
environmentId: command.environmentId,
41+
},
42+
});
43+
44+
return {
45+
appId: app.uid!,
46+
};
47+
} catch (error) {
48+
throw new Error(`Failed to generate Svix portal token: ${error?.message}`);
49+
}
50+
}
51+
}

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

+11-22
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import { Svix } from 'svix'; // Import Svix SDK type
55

66
import { GetWebhookPortalTokenCommand } from './get-webhook-portal-token.command';
77
import { GetWebhookPortalTokenResponseDto } from '../../dtos/get-webhook-portal-token-response.dto';
8+
import { CreateWebhookPortalUsecase } from '../create-webhook-portal-token/create-webhook-portal.usecase';
9+
import { CreateWebhookPortalCommand } from '../create-webhook-portal-token/create-webhook-portal.command';
810

911
const LOG_CONTEXT = 'GetWebhookPortalTokenUsecase';
1012

11-
@Injectable({
12-
scope: Scope.REQUEST,
13-
})
13+
@Injectable()
1414
export class GetWebhookPortalTokenUsecase {
1515
constructor(
1616
private environmentRepository: EnvironmentRepository,
1717
@Inject('SVIX_CLIENT') private svix: Svix,
18-
private organizationRepository: OrganizationRepository
18+
private createWebhookPortalUsecase: CreateWebhookPortalUsecase
1919
) {}
2020

2121
@LogDecorator()
@@ -44,32 +44,21 @@ export class GetWebhookPortalTokenUsecase {
4444
appId: `${command.organizationId}-${command.environmentId}`,
4545
};
4646
} catch (error) {
47-
console.log('AAAAA', error.code);
4847
if (error.code === 404) {
49-
const organization = await this.organizationRepository.findById(command.organizationId);
50-
if (!organization) {
51-
throw new NotFoundException(`Organization not found for id ${command.organizationId}`);
52-
}
53-
54-
const app = await this.svix.application.create({
55-
name: organization.name,
56-
uid: `${command.organizationId}-${command.environmentId}`,
57-
metadata: {
48+
const app = await this.createWebhookPortalUsecase.execute(
49+
CreateWebhookPortalCommand.create({
5850
environmentId: command.environmentId,
59-
},
60-
});
61-
62-
const svixResponse = await this.svix.authentication.appPortalAccess(
63-
`${command.organizationId}-${command.environmentId}`,
64-
{}
51+
organizationId: command.organizationId,
52+
userId: command.userId,
53+
})
6554
);
6655

67-
console.log('APPsSSSs', svixResponse);
56+
const svixResponse = await this.svix.authentication.appPortalAccess(app.appId, {});
6857

6958
return {
7059
url: svixResponse.url,
7160
token: svixResponse.token,
72-
appId: `${command.organizationId}-${command.environmentId}`,
61+
appId: app.appId,
7362
};
7463
}
7564
// Re-throw or handle specific Svix errors as needed

apps/api/src/app/webhooks/webhooks.controller.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
import { ClassSerializerInterceptor, Controller, Get, UseGuards, UseInterceptors } from '@nestjs/common';
1+
import { ClassSerializerInterceptor, Controller, Get, Post, UseGuards, UseInterceptors } from '@nestjs/common';
22
import { ApiExcludeController, ApiOperation, ApiTags } from '@nestjs/swagger';
33
import { UserSession } from '@novu/application-generic';
44
import { UserSessionData } from '@novu/shared';
55
import { GetWebhookPortalTokenUsecase } from './usecases/get-webhook-portal-token/get-webhook-portal-token.usecase';
66
import { GetWebhookPortalTokenCommand } from './usecases/get-webhook-portal-token/get-webhook-portal-token.command';
77
import { GetWebhookPortalTokenResponseDto } from './dtos/get-webhook-portal-token-response.dto';
88
import { UserAuthentication } from '../shared/framework/swagger/api.key.security';
9+
import { CreateWebhookPortalUsecase } from './usecases/create-webhook-portal-token/create-webhook-portal.usecase';
10+
import { CreateWebhookPortalCommand } from './usecases/create-webhook-portal-token/create-webhook-portal.command';
11+
import { CreateWebhookPortalResponseDto } from './dtos/create-webhook-portal-token-response.dto';
912

1013
@Controller({ path: `/webhooks`, version: '2' })
1114
@UseInterceptors(ClassSerializerInterceptor)
1215
@UserAuthentication()
1316
export class WebhooksController {
14-
constructor(private getWebhookPortalTokenUsecase: GetWebhookPortalTokenUsecase) {}
17+
constructor(
18+
private getWebhookPortalTokenUsecase: GetWebhookPortalTokenUsecase,
19+
private createWebhookPortalTokenUsecase: CreateWebhookPortalUsecase
20+
) {}
1521

1622
@Get('/portal/token')
1723
@ApiOperation({
@@ -28,4 +34,19 @@ export class WebhooksController {
2834
})
2935
);
3036
}
37+
38+
@Post('/portal/token')
39+
@ApiOperation({
40+
summary: 'Create Webhook Portal Access Token',
41+
description: 'Creates a token for accessing the webhook portal for the current environment.',
42+
})
43+
async createPortalToken(@UserSession() user: UserSessionData): Promise<CreateWebhookPortalResponseDto> {
44+
return await this.createWebhookPortalTokenUsecase.execute(
45+
CreateWebhookPortalCommand.create({
46+
environmentId: user.environmentId,
47+
organizationId: user.organizationId,
48+
userId: user._id,
49+
})
50+
);
51+
}
3152
}

apps/api/src/app/webhooks/webhooks.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { SvixProviderService, SendWebhookMessage } from '@novu/application-gener
33
import { SharedModule } from '../shared/shared.module';
44
import { WebhooksController } from './webhooks.controller';
55
import { GetWebhookPortalTokenUsecase } from './usecases/get-webhook-portal-token/get-webhook-portal-token.usecase';
6+
import { CreateWebhookPortalUsecase } from './usecases/create-webhook-portal-token/create-webhook-portal.usecase';
67

78
@Module({
89
imports: [SharedModule],
910
controllers: [WebhooksController],
10-
providers: [GetWebhookPortalTokenUsecase, SvixProviderService, SendWebhookMessage],
11+
providers: [GetWebhookPortalTokenUsecase, CreateWebhookPortalUsecase, SvixProviderService, SendWebhookMessage],
1112
exports: [SendWebhookMessage],
1213
})
1314
export class WebhooksModule {}

0 commit comments

Comments
 (0)