145 lines
3.8 KiB
TypeScript
Raw Permalink 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.

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '../auth/[...nextauth]/route';
import { prisma } from '@/lib/prisma';
// Rotaları getir
export async function GET() {
try {
// Oturum kontrolü
const session = await getServerSession(authOptions);
if (!session) {
return NextResponse.json(
{ message: 'Oturum açmanız gerekiyor' },
{ status: 401 }
);
}
// Kullanıcıyı bul
const user = await prisma.user.findUnique({
where: { email: session.user?.email || '' }
});
if (!user) {
return NextResponse.json(
{ message: 'Kullanıcı bulunamadı' },
{ status: 401 }
);
}
// Kullanıcının rotalarını getir
const routes = await prisma.route.findMany({
where: { userId: user.id },
include: {
addresses: {
orderBy: { order: 'asc' }
}
},
orderBy: { createdAt: 'desc' }
});
return NextResponse.json(routes);
} catch (error) {
console.error('Rotaları getirme hatası:', error);
return NextResponse.json(
{ message: 'Rotalar yüklenirken bir hata oluştu' },
{ status: 500 }
);
}
}
// Yeni rota oluştur
export async function POST(req: Request) {
try {
// Oturum kontrolü
const session = await getServerSession(authOptions);
console.log('Session:', session);
if (!session?.user?.email) {
return NextResponse.json(
{ success: false, error: 'Oturum açmanız gerekiyor' },
{ status: 401 }
);
}
// Kullanıcı ID'sini veritabanından al
const user = await prisma.user.findUnique({
where: { email: session.user.email }
});
if (!user) {
return NextResponse.json(
{ success: false, error: 'Kullanıcı bulunamadı' },
{ status: 401 }
);
}
const { addresses } = await req.json();
// Adres kontrolü
if (!addresses || !Array.isArray(addresses) || addresses.length < 2) {
return NextResponse.json(
{ success: false, error: 'En az 2 geçerli adres gerekli' },
{ status: 400 }
);
}
try {
// Yeni rota oluştur
const route = await prisma.route.create({
data: {
userId: user.id,
status: 'CREATED',
addresses: {
create: addresses.map((address: string, index: number) => {
// Adresi parçalara ayır
const parts = address.split(',').map((part: string) => part.trim());
const streetPart = parts[0] || '';
const locationPart = parts[1] || '';
// Posta kodu ve şehir bilgisini ayır
const locationMatch = locationPart.match(/(\d{4})\s+([^0]+)/);
const postcode = locationMatch ? locationMatch[1] : '';
const city = locationMatch ? locationMatch[2].replace(/\s*0+$/, '').trim() : '';
return {
street: streetPart,
city: city,
postcode: postcode,
country: 'Switzerland',
order: index
};
})
}
},
include: {
addresses: true
}
});
return NextResponse.json({
success: true,
data: route
}, { status: 201 });
} catch (dbError) {
console.error('Veritabanı hatası:', dbError);
return NextResponse.json(
{
success: false,
error: 'Rota veritabanına kaydedilirken bir hata oluştu'
},
{ status: 500 }
);
}
} catch (error) {
console.error('Rota oluşturma hatası:', error);
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Rota oluşturulurken bir hata oluştu'
},
{ status: 500 }
);
}
}