diff --git a/src/app/api/conversations/route.ts b/src/app/api/conversations/route.ts index 9e27f0d..8b81de4 100644 --- a/src/app/api/conversations/route.ts +++ b/src/app/api/conversations/route.ts @@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'; import pool from '@/lib/db'; import { verifyToken, TokenPayload } from '@/lib/auth'; import { RowDataPacket, ResultSetHeader } from 'mysql2'; -import { sendEmail } from '@/lib/email'; +// Email import removed - using dynamic import for sendMessageNotification in POST handler function getUserFromRequest(request: NextRequest): TokenPayload | null { const authHeader = request.headers.get('authorization'); @@ -94,36 +94,32 @@ export async function POST(request: NextRequest) { await connection.commit(); - // Notify admins via email if they are offline - const [admins] = await pool.query( - `SELECT id, email FROM users WHERE role = 'admin'` + // Fetch full sender details for notification + const [senderDetails] = await pool.query( + `SELECT full_name, email, mobile, personnummer, address, zip_code, city FROM users WHERE id = ?`, + [user.userId] ); + const sender = senderDetails[0]; - for (const admin of admins) { - // Check if admin is online (has active session) - const [sessions] = await pool.query( - `SELECT COUNT(*) as count FROM user_sessions - WHERE user_id = ? AND expires_at > NOW()`, - [admin.id] - ); + // Send ONE email to CONTACT_EMAIL with premium template + const { sendMessageNotification } = await import('@/lib/email'); + const adminEmail = process.env.CONTACT_EMAIL || 'info@nordicstorium.se'; - const isOnline = sessions[0].count > 0; - - if (!isOnline) { - await sendEmail( - admin.email, - `Nytt meddelande: ${subject}`, - ` -

Nytt kundmeddelande

-

Från: ${user.email}

-

Ämne: ${subject}

-

Meddelande:

-
${message}
-

Svara här

- ` - ); - } - } + await sendMessageNotification( + adminEmail, + `Nytt kundmeddelande: ${subject}`, + sender.full_name || 'Kund', + sender.email, + sender.mobile, + sender.personnummer, + sender.address, + sender.zip_code, + sender.city, + message, + `${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/admin/messages`, + 'Svara i Adminpanelen', + true // Customer is sender + ); return NextResponse.json({ success: true,