All files / modules/webhooks webhooks.controller.ts

100% Statements 30/30
100% Branches 0/0
100% Functions 11/11
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 1301x                       1x 1x 1x 1x                                       1x 10x       1x 1x             1x 1x         1x       1x         1x       1x         1x         1x         1x       1x 1x         1x       1x         1x         1x         1x         1x         1x         1x      
import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
} from '@nestjs/common';
import { JwtAuthGuard } from '@modules/auth/guards/jwt-auth.guard';
import { CurrentUser } from '@modules/auth/decorators/current-user.decorator';
import { WebhookService } from './services';
import {
  CreateWebhookDto,
  UpdateWebhookDto,
  TestWebhookDto,
  ListDeliveriesQueryDto,
  WebhookResponseDto,
  DeliveryResponseDto,
  PaginatedDeliveriesDto,
  AvailableEventsDto,
} from './dto';
 
interface RequestUser {
  id: string;
  tenant_id: string;
  email: string;
  role: string;
}
 
@Controller('webhooks')
@UseGuards(JwtAuthGuard)
export class WebhooksController {
  constructor(private readonly webhookService: WebhookService) {}
 
  // Get available events
  @Get('events')
  getAvailableEvents(): AvailableEventsDto {
    return {
      events: this.webhookService.getAvailableEvents(),
    };
  }
 
  // List all webhooks
  @Get()
  async findAll(@CurrentUser() user: RequestUser): Promise<WebhookResponseDto[]> {
    return this.webhookService.findAll(user.tenant_id);
  }
 
  // Get a single webhook
  @Get(':id')
  async findOne(
    @CurrentUser() user: RequestUser,
    @Param('id', ParseUUIDPipe) id: string,
  ): Promise<WebhookResponseDto> {
    return this.webhookService.findOne(user.tenant_id, id);
  }
 
  // Create a webhook
  @Post()
  async create(
    @CurrentUser() user: RequestUser,
    @Body() dto: CreateWebhookDto,
  ): Promise<WebhookResponseDto> {
    return this.webhookService.create(user.tenant_id, user.id, dto);
  }
 
  // Update a webhook
  @Put(':id')
  async update(
    @CurrentUser() user: RequestUser,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateWebhookDto,
  ): Promise<WebhookResponseDto> {
    return this.webhookService.update(user.tenant_id, id, dto);
  }
 
  // Delete a webhook
  @Delete(':id')
  async remove(
    @CurrentUser() user: RequestUser,
    @Param('id', ParseUUIDPipe) id: string,
  ): Promise<{ message: string }> {
    await this.webhookService.remove(user.tenant_id, id);
    return { message: 'Webhook deleted successfully' };
  }
 
  // Regenerate webhook secret
  @Post(':id/regenerate-secret')
  async regenerateSecret(
    @CurrentUser() user: RequestUser,
    @Param('id', ParseUUIDPipe) id: string,
  ): Promise<{ secret: string }> {
    return this.webhookService.regenerateSecret(user.tenant_id, id);
  }
 
  // Test a webhook
  @Post(':id/test')
  async test(
    @CurrentUser() user: RequestUser,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: TestWebhookDto,
  ): Promise<DeliveryResponseDto> {
    return this.webhookService.testWebhook(user.tenant_id, id, dto);
  }
 
  // Get deliveries for a webhook
  @Get(':id/deliveries')
  async getDeliveries(
    @CurrentUser() user: RequestUser,
    @Param('id', ParseUUIDPipe) id: string,
    @Query() query: ListDeliveriesQueryDto,
  ): Promise<PaginatedDeliveriesDto> {
    return this.webhookService.getDeliveries(user.tenant_id, id, query);
  }
 
  // Retry a failed delivery
  @Post(':id/deliveries/:deliveryId/retry')
  async retryDelivery(
    @CurrentUser() user: RequestUser,
    @Param('id', ParseUUIDPipe) id: string,
    @Param('deliveryId', ParseUUIDPipe) deliveryId: string,
  ): Promise<DeliveryResponseDto> {
    return this.webhookService.retryDelivery(user.tenant_id, id, deliveryId);
  }
}