import { Body, Controller, Get, Param, Put } from '@nestjs/common'; import { ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'; import { CustomerContactsService } from './customer-contacts.service'; interface SaveContactBody { phone?: string | null; whatsappPhone?: string | null; callSmsPhone?: string | null; email?: string | null; name?: string | null; company?: string | null; note?: string | null; userId?: number | null; } @ApiTags('Contatos') @Controller('contacts') export class CustomerContactsController { constructor(private readonly customerContactsService: CustomerContactsService) {} @ApiOperation({ summary: 'Lista contatos', description: 'Retorna a agenda de contatos usada para atendimento, abertura ativa e consulta rapida no chat.', }) @ApiResponse({ status: 200, description: 'Lista de contatos retornada.' }) @Get() listContacts() { return this.customerContactsService.listContacts(); } @ApiOperation({ summary: 'Consulta contato por chat', description: 'Busca os dados salvos de um contato usando o identificador da conversa.', }) @ApiParam({ name: 'chatId', description: 'Identificador codificado do chat.' }) @Get(':chatId') getContact(@Param('chatId') chatId: string) { return this.customerContactsService.getContact(decodeURIComponent(chatId)); } @ApiOperation({ summary: 'Salva contato', description: 'Cria ou atualiza dados do contato associados ao chat, incluindo telefone, email, etiqueta de identificacao e observacao.', }) @ApiParam({ name: 'chatId', description: 'Identificador codificado do chat.' }) @ApiBody({ schema: { type: 'object', properties: { phone: { type: 'string', nullable: true, example: '5511999999999' }, whatsappPhone: { type: 'string', nullable: true, example: '5511999999999' }, callSmsPhone: { type: 'string', nullable: true, example: '5511988888888' }, email: { type: 'string', nullable: true, example: 'cliente@empresa.com' }, name: { type: 'string', nullable: true, example: 'Tony Silva' }, company: { type: 'string', nullable: true, example: 'Departamento de RH' }, note: { type: 'string', nullable: true, example: 'Contato prefere WhatsApp.' }, userId: { type: 'number', nullable: true, example: 10 }, }, }, }) @Put(':chatId') saveContact( @Param('chatId') chatId: string, @Body() body: SaveContactBody, ) { return this.customerContactsService.saveContact({ chatId: decodeURIComponent(chatId), phone: body.whatsappPhone || body.phone, callSmsPhone: body.callSmsPhone, email: body.email, name: body.name, company: body.company, note: body.note, userId: body.userId, }); } }