59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Body,
|
|
Headers,
|
|
UseGuards,
|
|
HttpCode,
|
|
HttpStatus,
|
|
BadRequestException,
|
|
} from "@nestjs/common";
|
|
import { WebhooksService } from "./webhooks.service";
|
|
import { ApiTags, ApiOperation, ApiResponse, ApiHeader } from "@nestjs/swagger";
|
|
import { ThrottlerGuard } from "@nestjs/throttler";
|
|
import { WebhookSignatureGuard } from "./guards/webhook-signature.guard";
|
|
|
|
@ApiTags("webhooks")
|
|
@Controller("webhooks")
|
|
@UseGuards(ThrottlerGuard) // Rate limit webhook endpoints
|
|
export class WebhooksController {
|
|
constructor(private webhooksService: WebhooksService) {}
|
|
|
|
@Post("whmcs")
|
|
@HttpCode(HttpStatus.OK)
|
|
@UseGuards(WebhookSignatureGuard)
|
|
@ApiOperation({ summary: "WHMCS webhook endpoint" })
|
|
@ApiResponse({ status: 200, description: "Webhook processed successfully" })
|
|
@ApiResponse({ status: 400, description: "Invalid webhook data" })
|
|
@ApiResponse({ status: 401, description: "Invalid signature" })
|
|
@ApiHeader({ name: "X-WHMCS-Signature", description: "WHMCS webhook signature" })
|
|
async handleWhmcsWebhook(@Body() payload: any, @Headers("x-whmcs-signature") signature: string) {
|
|
try {
|
|
await this.webhooksService.processWhmcsWebhook(payload, signature);
|
|
return { success: true, message: "Webhook processed successfully" };
|
|
} catch {
|
|
throw new BadRequestException("Failed to process webhook");
|
|
}
|
|
}
|
|
|
|
@Post("salesforce")
|
|
@HttpCode(HttpStatus.OK)
|
|
@UseGuards(WebhookSignatureGuard)
|
|
@ApiOperation({ summary: "Salesforce webhook endpoint" })
|
|
@ApiResponse({ status: 200, description: "Webhook processed successfully" })
|
|
@ApiResponse({ status: 400, description: "Invalid webhook data" })
|
|
@ApiResponse({ status: 401, description: "Invalid signature" })
|
|
@ApiHeader({ name: "X-SF-Signature", description: "Salesforce webhook signature" })
|
|
async handleSalesforceWebhook(
|
|
@Body() payload: any,
|
|
@Headers("x-sf-signature") signature: string
|
|
) {
|
|
try {
|
|
await this.webhooksService.processSalesforceWebhook(payload, signature);
|
|
return { success: true, message: "Webhook processed successfully" };
|
|
} catch {
|
|
throw new BadRequestException("Failed to process webhook");
|
|
}
|
|
}
|
|
}
|