michangarrito-whatsapp-serv.../src/webhook/dto/webhook.dto.ts
2026-01-16 08:27:00 -06:00

143 lines
2.6 KiB
TypeScript

import { IsString, IsArray, ValidateNested, IsOptional } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';
class WebhookMessageText {
@ApiProperty()
@IsString()
body: string;
}
class WebhookMessageInteractive {
@IsString()
type: string;
@IsOptional()
button_reply?: { id: string; title: string };
@IsOptional()
list_reply?: { id: string; title: string; description?: string };
}
class WebhookMessage {
@IsString()
from: string;
@IsString()
id: string;
@IsString()
timestamp: string;
@IsString()
type: string;
@IsOptional()
@ValidateNested()
@Type(() => WebhookMessageText)
text?: WebhookMessageText;
@IsOptional()
interactive?: WebhookMessageInteractive;
@IsOptional()
image?: { id: string; mime_type: string; caption?: string };
@IsOptional()
audio?: { id: string; mime_type: string };
@IsOptional()
document?: { id: string; mime_type: string; filename?: string };
@IsOptional()
location?: { latitude: number; longitude: number; name?: string; address?: string };
}
class WebhookContact {
profile: { name: string };
wa_id: string;
}
class WebhookStatus {
@IsString()
id: string;
@IsString()
status: string;
@IsString()
timestamp: string;
@IsString()
recipient_id: string;
@IsOptional()
errors?: Array<{ code: number; title: string; message: string }>;
}
class WebhookMetadata {
@IsString()
display_phone_number: string;
@IsString()
phone_number_id: string;
}
class WebhookValue {
@IsString()
messaging_product: string;
@ValidateNested()
@Type(() => WebhookMetadata)
metadata: WebhookMetadata;
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => WebhookContact)
contacts?: WebhookContact[];
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => WebhookMessage)
messages?: WebhookMessage[];
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => WebhookStatus)
statuses?: WebhookStatus[];
}
class WebhookChange {
@ValidateNested()
@Type(() => WebhookValue)
value: WebhookValue;
@IsString()
field: string;
}
class WebhookEntry {
@IsString()
id: string;
@IsArray()
@ValidateNested({ each: true })
@Type(() => WebhookChange)
changes: WebhookChange[];
}
export class WebhookPayloadDto {
@ApiProperty({ description: 'Always "whatsapp_business_account"' })
@IsString()
object: string;
@ApiProperty({ description: 'Array of webhook entries' })
@IsArray()
@ValidateNested({ each: true })
@Type(() => WebhookEntry)
entry: WebhookEntry[];
}