fix email message x2
This commit is contained in:
parent
ca0a4c6968
commit
0bd38101b4
|
|
@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||||
import pool from '@/lib/db';
|
import pool from '@/lib/db';
|
||||||
import { verifyToken, TokenPayload } from '@/lib/auth';
|
import { verifyToken, TokenPayload } from '@/lib/auth';
|
||||||
import { RowDataPacket, ResultSetHeader } from 'mysql2';
|
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 {
|
function getUserFromRequest(request: NextRequest): TokenPayload | null {
|
||||||
const authHeader = request.headers.get('authorization');
|
const authHeader = request.headers.get('authorization');
|
||||||
|
|
@ -94,36 +94,32 @@ export async function POST(request: NextRequest) {
|
||||||
|
|
||||||
await connection.commit();
|
await connection.commit();
|
||||||
|
|
||||||
// Notify admins via email if they are offline
|
// Fetch full sender details for notification
|
||||||
const [admins] = await pool.query<RowDataPacket[]>(
|
const [senderDetails] = await pool.query<RowDataPacket[]>(
|
||||||
`SELECT id, email FROM users WHERE role = 'admin'`
|
`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) {
|
// Send ONE email to CONTACT_EMAIL with premium template
|
||||||
// Check if admin is online (has active session)
|
const { sendMessageNotification } = await import('@/lib/email');
|
||||||
const [sessions] = await pool.query<RowDataPacket[]>(
|
const adminEmail = process.env.CONTACT_EMAIL || 'info@nordicstorium.se';
|
||||||
`SELECT COUNT(*) as count FROM user_sessions
|
|
||||||
WHERE user_id = ? AND expires_at > NOW()`,
|
|
||||||
[admin.id]
|
|
||||||
);
|
|
||||||
|
|
||||||
const isOnline = sessions[0].count > 0;
|
await sendMessageNotification(
|
||||||
|
adminEmail,
|
||||||
if (!isOnline) {
|
`Nytt kundmeddelande: ${subject}`,
|
||||||
await sendEmail(
|
sender.full_name || 'Kund',
|
||||||
admin.email,
|
sender.email,
|
||||||
`Nytt meddelande: ${subject}`,
|
sender.mobile,
|
||||||
`
|
sender.personnummer,
|
||||||
<h2>Nytt kundmeddelande</h2>
|
sender.address,
|
||||||
<p><strong>Från:</strong> ${user.email}</p>
|
sender.zip_code,
|
||||||
<p><strong>Ämne:</strong> ${subject}</p>
|
sender.city,
|
||||||
<p><strong>Meddelande:</strong></p>
|
message,
|
||||||
<blockquote style="border-left: 3px solid #ccc; padding-left: 10px; margin: 10px 0;">${message}</blockquote>
|
`${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/admin/messages`,
|
||||||
<p><a href="${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/admin/messages/${conversationId}">Svara här</a></p>
|
'Svara i Adminpanelen',
|
||||||
`
|
true // Customer is sender
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue