Files
atahan/server/api/contact/post.create.ts
Beyhan Oğur 763b147cc3 first commit
2026-04-26 22:04:35 +03:00

121 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// http://127.0.0.1:8000/api/v1/contact/create/
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig();
const apiUrl = config.public.BASE_API_URL || 'http://127.0.0.1:8000';
// Backend credentials
const BACKEND_EMAIL = config.backendEmail || 'admin@example.com';
const BACKEND_PASSWORD = config.backendPassword || 'admin123';
try {
// Form verilerini al
const body = await readBody(event);
// Gerekli alanları kontrol et
const { name, email, subject, message } = body;
if (!name || !email || !subject || !message) {
throw createError({
statusCode: 400,
statusMessage: 'Tüm alanlar zorunludur'
});
}
// Client IP adresini al
const ip = getRequestIP(event, { xForwardedFor: true }) || 'unknown';
// 1. Önce backend'e login ol
let loginResponse;
try {
loginResponse = await $fetch(`${apiUrl}/api/v1/auth/jwt/create/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
email: BACKEND_EMAIL,
password: BACKEND_PASSWORD
}
});
} catch (loginError: any) {
// Detaylı hatayı sadece console'da göster
console.error('Backend login error:', {
status: loginError?.status,
statusText: loginError?.statusText,
message: loginError?.message
});
throw createError({
statusCode: 503,
statusMessage: 'Servis geçici olarak kullanılamıyor. Lütfen daha sonra tekrar deneyin.'
});
}
// @ts-expect-error - API response type
const accessToken = loginResponse?.access;
if (!accessToken) {
console.error('No access token received from backend');
throw createError({
statusCode: 503,
statusMessage: 'Servis geçici olarak kullanılamıyor. Lütfen daha sonra tekrar deneyin.'
});
}
// 2. Token ile contact form'u post et (IP adresi ile birlikte)
let contactResponse;
try {
contactResponse = await $fetch(`${apiUrl}/api/v1/contact/create/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: {
name,
email,
ip,
subject,
message
}
});
} catch (contactError: any) {
// Detaylı hatayı sadece console'da göster
console.error('Contact form submission error:', {
status: contactError?.status,
statusText: contactError?.statusText,
message: contactError?.message
});
throw createError({
statusCode: 500,
statusMessage: 'Mesaj gönderilemedi. Lütfen daha sonra tekrar deneyin.'
});
}
return {
success: true,
data: contactResponse,
message: 'Mesajınız başarıyla gönderildi'
};
} catch (error: any) {
// Eğer zaten createError ile oluşturulmuş bir hata ise, direkt fırlat
if (error.statusCode) {
throw error;
}
// Beklenmeyen hatalar için
console.error('Unexpected contact form error:', error);
throw createError({
statusCode: 500,
statusMessage: 'Bir hata oluştu. Lütfen daha sonra tekrar deneyin.'
});
}
});