|
| 1 | +import { type NextRequest, NextResponse } from 'next/server' |
| 2 | +import { z } from 'zod' |
| 3 | +import { checkHybridAuth } from '@/lib/auth/hybrid' |
| 4 | +import { env } from '@/lib/env' |
| 5 | +import { createLogger } from '@/lib/logs/console/logger' |
| 6 | +import { type SMSOptions, sendSMS } from '@/lib/sms/service' |
| 7 | +import { generateRequestId } from '@/lib/utils' |
| 8 | + |
| 9 | +export const dynamic = 'force-dynamic' |
| 10 | + |
| 11 | +const logger = createLogger('SMSSendAPI') |
| 12 | + |
| 13 | +const SMSSendSchema = z.object({ |
| 14 | + to: z.string().min(1, 'To phone number is required'), |
| 15 | + body: z.string().min(1, 'SMS body is required'), |
| 16 | +}) |
| 17 | + |
| 18 | +export async function POST(request: NextRequest) { |
| 19 | + const requestId = generateRequestId() |
| 20 | + |
| 21 | + try { |
| 22 | + const authResult = await checkHybridAuth(request, { requireWorkflowId: false }) |
| 23 | + |
| 24 | + if (!authResult.success) { |
| 25 | + logger.warn(`[${requestId}] Unauthorized SMS send attempt: ${authResult.error}`) |
| 26 | + return NextResponse.json( |
| 27 | + { |
| 28 | + success: false, |
| 29 | + message: authResult.error || 'Authentication required', |
| 30 | + }, |
| 31 | + { status: 401 } |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + logger.info(`[${requestId}] Authenticated SMS request via ${authResult.authType}`, { |
| 36 | + userId: authResult.userId, |
| 37 | + }) |
| 38 | + |
| 39 | + const body = await request.json() |
| 40 | + const validatedData = SMSSendSchema.parse(body) |
| 41 | + |
| 42 | + const fromNumber = env.TWILIO_PHONE_NUMBER |
| 43 | + |
| 44 | + if (!fromNumber) { |
| 45 | + logger.error(`[${requestId}] SMS sending failed: No phone number configured`) |
| 46 | + return NextResponse.json( |
| 47 | + { |
| 48 | + success: false, |
| 49 | + message: 'SMS sending failed: No phone number configured.', |
| 50 | + }, |
| 51 | + { status: 500 } |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + logger.info(`[${requestId}] Sending SMS via internal SMS API`, { |
| 56 | + to: validatedData.to, |
| 57 | + bodyLength: validatedData.body.length, |
| 58 | + from: fromNumber, |
| 59 | + }) |
| 60 | + |
| 61 | + const smsOptions: SMSOptions = { |
| 62 | + to: validatedData.to, |
| 63 | + body: validatedData.body, |
| 64 | + from: fromNumber, |
| 65 | + } |
| 66 | + |
| 67 | + const result = await sendSMS(smsOptions) |
| 68 | + |
| 69 | + logger.info(`[${requestId}] SMS send result`, { |
| 70 | + success: result.success, |
| 71 | + message: result.message, |
| 72 | + }) |
| 73 | + |
| 74 | + return NextResponse.json(result) |
| 75 | + } catch (error) { |
| 76 | + if (error instanceof z.ZodError) { |
| 77 | + logger.warn(`[${requestId}] Invalid request data`, { errors: error.errors }) |
| 78 | + return NextResponse.json( |
| 79 | + { |
| 80 | + success: false, |
| 81 | + message: 'Invalid request data', |
| 82 | + errors: error.errors, |
| 83 | + }, |
| 84 | + { status: 400 } |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + logger.error(`[${requestId}] Error sending SMS via API:`, error) |
| 89 | + |
| 90 | + return NextResponse.json( |
| 91 | + { |
| 92 | + success: false, |
| 93 | + message: 'Internal server error while sending SMS', |
| 94 | + data: {}, |
| 95 | + }, |
| 96 | + { status: 500 } |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
0 commit comments