import { Controller, Get, Post, Patch, Body, UseGuards, Param, Res, Query } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { Response } from 'express'; import { SettingsService } from './settings.service'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { EmailOtpService } from '../auth/email-otp.service'; import { MailService } from '../mail/mail.service'; @ApiTags('Settings') @Controller('settings') export class SettingsController { constructor(private settings: SettingsService, private emailOtp: EmailOtpService, private mail: MailService) {} @Get() getGlobal() { return this.settings.getGlobal(); } @Patch() @UseGuards(JwtAuthGuard) @ApiBearerAuth() updateGlobal(@Body() body: Record) { return this.settings.updateGlobal(body); } // Policies admin (protected) @Get('policies') @UseGuards(JwtAuthGuard) @ApiBearerAuth() getPolicies() { return this.settings.getPolicies(); } @Patch('policies/:key') @UseGuards(JwtAuthGuard) @ApiBearerAuth() updatePolicy(@Param('key') key: 'privacy' | 'terms', @Body() body: { content: string }) { return this.settings.updatePolicy(key, body.content); } // SMTP config @Get('smtp') @UseGuards(JwtAuthGuard) @ApiBearerAuth() async getSmtp() { const cfg = await this.emailOtp.getSmtpConfig(); return cfg ?? {}; } @Patch('smtp') @UseGuards(JwtAuthGuard) @ApiBearerAuth() saveSmtp(@Body() body: { host: string; port: string; user: string; pass: string; from?: string }) { return this.emailOtp.saveSmtpConfig(body); } @Post('test-email') @UseGuards(JwtAuthGuard) @ApiBearerAuth() async testEmail(@Body() body: { to: string }) { await this.mail.tryMail( body.to, 'Correo de prueba — ProsApp', `

✅ Configuración SMTP correcta

Si recibes este correo, el servidor SMTP está funcionando correctamente en ProsApp.

Enviado desde el panel de administración.

`, ); return { message: `Correo de prueba enviado a ${body.to}` }; } // Message logs @Get('message-logs') @UseGuards(JwtAuthGuard) @ApiBearerAuth() getMessageLogs(@Query('page') page = '1', @Query('limit') limit = '50', @Query('channel') channel?: string) { return this.settings.getMessageLogs(+page, +limit, channel); } // Maps key @Get('maps-key') async getMapsKey() { const api_key = await this.settings.getMapsKey(); return { configured: !!api_key, api_key: api_key ?? '' }; } @Patch('maps') @UseGuards(JwtAuthGuard) @ApiBearerAuth() saveMapsKey(@Body() body: { api_key: string }) { return this.settings.saveMapsKey(body.api_key); } // Public policy pages @Get('policy/:key') async getPublicPolicy(@Param('key') key: string, @Res() res: Response) { const content = await this.settings.getPolicy(key); const titles: Record = { privacy: 'Politica de Privacidad', terms: 'Terminos y Condiciones', }; const title = titles[key] || 'Politica'; if (!content) { return res.status(404).send(`

${title}

No disponible aun.

`); } const html = ` ${title} - ProsApp
ProsApp

${title}

${content.replace(//g, '>')}
`; res.setHeader('Content-Type', 'text/html; charset=utf-8'); return res.send(html); } }