forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.controller.ts
More file actions
84 lines (80 loc) · 2.72 KB
/
Copy pathwebhooks.controller.ts
File metadata and controls
84 lines (80 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {
Controller,
Get,
HttpCode,
HttpStatus,
Post,
UseGuards,
} from "@nestjs/common";
import { Auth, CurrentUser } from "../auth/guard/auth.guard";
import { User } from "../users/user.entity";
import { PrismaService } from "../prisma/prisma.service";
import {
WebhooksService,
WebhookSecretMetadata,
WebhookSecretRotationResult,
} from "./webhooks.service";
import { Roles } from "../common/decorators/roles.decorator";
import { MerchantRole } from "../common/enums/merchant-role.enum";
import { MerchantRolesGuard } from "../common/guards/merchant-roles.guard";
import { WebhookTestDeliveryResultDto } from "./dto/webhook-test-delivery.dto";
@Controller("webhooks")
export class WebhooksController {
constructor(
private readonly webhooksService: WebhooksService,
private readonly prisma: PrismaService,
) {}
/**
* Read masked metadata for the current merchant's webhook secret.
*/
@Auth()
@Get("secret")
async getSecretMetadata(
@CurrentUser() user: User,
): Promise<WebhookSecretMetadata> {
return this.prisma.runWithMerchantScope(user.merchantId, () =>
this.webhooksService.getWebhookSecretMetadata(user.id, user.merchantId),
);
}
/**
* Generate and persist a new webhook secret for the current merchant.
*
* The raw secret is returned once so the caller can copy it into their
* signing configuration. Restricted to merchant OWNERs and ADMINs.
*/
@Roles(MerchantRole.OWNER, MerchantRole.ADMIN)
@UseGuards(MerchantRolesGuard)
@Post("secret/rotate")
@HttpCode(HttpStatus.OK)
async rotateSecret(
@CurrentUser() user: User,
): Promise<WebhookSecretRotationResult> {
return this.prisma.runWithMerchantScope(user.merchantId, () =>
this.webhooksService.rotateWebhookSecret(user.id, user.merchantId),
);
}
/**
* Send a signed test webhook delivery to the merchant's configured endpoint.
*
* Sends a sample payload signed with the active webhook secret so merchants
* can verify their endpoint is reachable and correctly processes HMAC
* signatures before real production traffic arrives.
*
* Test deliveries are fully isolated – they are never written to the
* delivery history or dead-letter queue, and the payload is clearly
* labelled `"event": "test"` so it cannot be confused with a real event.
*
* Restricted to merchant OWNERs and ADMINs.
*/
@Roles(MerchantRole.OWNER, MerchantRole.ADMIN)
@UseGuards(MerchantRolesGuard)
@Post("test")
@HttpCode(HttpStatus.OK)
async sendTestDelivery(
@CurrentUser() user: User,
): Promise<WebhookTestDeliveryResultDto> {
return this.prisma.runWithMerchantScope(user.merchantId, () =>
this.webhooksService.sendTestDelivery(user.id, user.merchantId),
);
}
}