Files
CRM_Montzar/apps/api/src/public-api/webhooks.controller.ts
T

126 lines
2.5 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import {
IsArray,
IsBoolean,
IsOptional,
IsString,
IsUrl,
MaxLength,
MinLength,
} from 'class-validator';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { Roles } from '../auth/roles.decorator';
import { RolesGuard } from '../auth/roles.guard';
import { OrgModuleGuard } from '../billing/org-module.guard';
import { RequireOrgModule } from '../billing/org-module.decorator';
import { WebhooksService } from './webhooks.service';
class CreateWebhookDto {
@IsString()
@MinLength(2)
@MaxLength(80)
name!: string;
@IsString()
@IsUrl({ require_tld: false })
url!: string;
@IsArray()
@IsString({ each: true })
events!: string[];
@IsOptional()
@IsString()
@MaxLength(120)
secret?: string;
@IsOptional()
@IsBoolean()
isActive?: boolean;
}
class UpdateWebhookDto {
@IsOptional()
@IsString()
@MinLength(2)
@MaxLength(80)
name?: string;
@IsOptional()
@IsString()
@IsUrl({ require_tld: false })
url?: string;
@IsOptional()
@IsArray()
@IsString({ each: true })
events?: string[];
@IsOptional()
@IsString()
@MaxLength(120)
secret?: string;
@IsOptional()
@IsBoolean()
isActive?: boolean;
}
@Controller('webhooks')
@UseGuards(JwtAuthGuard, RolesGuard, OrgModuleGuard)
@RequireOrgModule('public_api')
@Roles('owner', 'admin')
export class WebhooksController {
constructor(private readonly webhooks: WebhooksService) {}
@Get()
list(@CurrentUser() user: { organizationId: string }) {
return this.webhooks.listEndpoints(user.organizationId);
}
@Get('deliveries')
deliveries(
@CurrentUser() user: { organizationId: string },
@Query('endpointId') endpointId?: string,
) {
return this.webhooks.listDeliveries(user.organizationId, endpointId);
}
@Post()
create(
@CurrentUser() user: { organizationId: string },
@Body() dto: CreateWebhookDto,
) {
return this.webhooks.createEndpoint(user.organizationId, dto);
}
@Patch(':id')
update(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
@Body() dto: UpdateWebhookDto,
) {
return this.webhooks.updateEndpoint(user.organizationId, id, dto);
}
@Delete(':id')
async remove(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
) {
await this.webhooks.removeEndpoint(user.organizationId, id);
return { ok: true };
}
}