| import { NextRequest, NextResponse } from 'next/server'; | |
| import { adminFirestore, adminMessaging } from '@/lib/firebase-admin'; | |
| export async function POST(req: NextRequest) { | |
| console.log("[/api/call-notification] Received a POST request."); | |
| let recipientUidForError: string | null = null; | |
| try { | |
| const { recipientUid, callerName, channelName, callType, callerPhoto } = await req.json(); | |
| recipientUidForError = recipientUid; | |
| console.log("[/api/call-notification] Request body parsed:", { recipientUid, callerName, channelName, callType }); | |
| if (!recipientUid || !callerName || !channelName || !callType) { | |
| console.log("[/api/call-notification] Missing required fields."); | |
| return NextResponse.json({ error: 'Missing required fields' }, { status: 400 }); | |
| } | |
| const userDoc = await adminFirestore.collection('users').doc(recipientUid).get(); | |
| if (!userDoc.exists) { | |
| console.log(`[/api/call-notification] Recipient user not found: ${recipientUid}`); | |
| return NextResponse.json({ error: 'Recipient user not found' }, { status: 404 }); | |
| } | |
| const fcmToken = userDoc.data()?.fcmToken; | |
| if (!fcmToken) { | |
| console.log(`[/api/call-notification] Notification not sent: User ${recipientUid} does not have an FCM token.`); | |
| return NextResponse.json({ success: true, message: 'User has no FCM token.' }); | |
| } | |
| const message = { | |
| token: fcmToken, | |
| notification: { | |
| title: `Incoming ${callType} call`, | |
| body: `From: ${callerName}`, | |
| }, | |
| webpush: { | |
| notification: { | |
| icon: callerPhoto || '/favicon.ico', | |
| badge: '/favicon.ico', | |
| actions: [ | |
| { action: 'answer', title: 'Answer' }, | |
| { action: 'decline', title: 'Decline' } | |
| ], | |
| data: { | |
| type: 'incoming-call', | |
| channelName: channelName, | |
| }, | |
| requireInteraction: "true" as const, | |
| tag: `call-${channelName}` | |
| } | |
| }, | |
| }; | |
| console.log("[/api/call-notification] Prepared message payload:", message); | |
| const response = await adminMessaging.send(message); | |
| console.log('[/api/call-notification] Successfully sent call notification:', response); | |
| return NextResponse.json({ success: true, messageId: response }); | |
| } catch (error: any) { | |
| console.error('[/api/call-notification] CRITICAL ERROR:', error); | |
| let errorMessage = 'Internal Server Error'; | |
| let statusCode = 500; | |
| if (error.code === 'messaging/invalid-argument') { | |
| errorMessage = 'Invalid argument provided for FCM message.'; | |
| statusCode = 400; | |
| } else if (error.code === 'messaging/registration-token-not-registered') { | |
| errorMessage = `The FCM token for user ${recipientUidForError} is no longer valid. It might be expired.`; | |
| if (recipientUidForError) { | |
| await adminFirestore.collection('users').doc(recipientUidForError).update({ fcmToken: '' }); | |
| } | |
| statusCode = 404; | |
| } else if (error.code === 'messaging/invalid-registration-token') { | |
| errorMessage = `Invalid FCM registration token for user ${recipientUidForError}.`; | |
| if (recipientUidForError) { | |
| await adminFirestore.collection('users').doc(recipientUidForError).update({ fcmToken: '' }); | |
| } | |
| statusCode = 400; | |
| } | |
| return NextResponse.json({ error: 'Failed to send notification', details: errorMessage, rawError: error.message }, { status: statusCode }); | |
| } | |
| } | |