import { BadRequestException, Body, Controller, Get, HttpCode, HttpStatus, Post, Query, Req, } from '@nestjs/common'; import { OtpService } from '../service/otp.service'; import { ConfigService } from '@nestjs/config'; import { EmailService } from '../service/email.service'; import { UAParser } from 'ua-parser-js'; import { Request } from 'express'; @Controller('otp') export class OtpController { constructor( private readonly otpService: OtpService, private readonly emailService: EmailService, private configService: ConfigService, ) {} otpExpiry = this.configService.get('OTP_EXPIRY') || '2'; otpLength = this.configService.get('OTP_LENGTH') || '6'; @Post('generate') @HttpCode(HttpStatus.OK) async generateOtp(@Body() request: any) { const identifier = request.identifier; const service = request.service; if (!identifier || !service) { throw new BadRequestException('Missing identifier or service!'); } const otp = await this.otpService.generate( identifier, service, parseInt(this.otpExpiry), parseInt(this.otpLength), ); return { otp_id: otp.otp_id, success: true }; } @Post('verify') @HttpCode(HttpStatus.OK) async verifyOtp( @Body() data: { otp: string; otp_id: string; identifier: string; subdomain: string; fcm_token: string; }, @Req() req: Request, ) { const ip = (req.headers['x-forwarded-for'] as string)?.split(',')[0].trim() || req.socket.remoteAddress || req.ip || ''; const userAgent = req.headers['user-agent'] || ''; const parser = new UAParser(userAgent); const browser = parser.getBrowser().name || 'Unknown'; const os = parser.getOS().name || 'Unknown'; return await this.otpService.verifyOtp({ ...data, ip, browser, os, }); } @Post('send-mail') @HttpCode(HttpStatus.OK) async sendMail( @Body() body: { to: string; cc?: string; bcc?: string; subject: string; message: string; templateCode: string; payload: any; }, ) { return await this.emailService.sendEmailWithDynamicTemplate( body.to, body.subject, body.templateCode, body.cc ? body.cc.split(',') : [], body.bcc ? body.bcc.split(',') : [], body.message, body.payload, { title: 'Project Kickoff Meeting', description: 'Discuss goals and deliverables', startsAt: '2025-08-30T10:00:00+05:30', durationMinutes: 60, meetingUrl: 'https://meet.google.com/xyz-1234-abc', location: 'Google Meet', organizerName: 'Darshil', organizerEmail: 'sample@sample', attendees: [ { name: 'John Doe', email: 'john@example.com' }, { name: 'Jane Smith', email: 'jane@example.com' }, ], }, ); } }