31 lines
925 B
TypeScript
31 lines
925 B
TypeScript
import { NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const subscription = await request.json();
|
|
|
|
if (!subscription || !subscription.endpoint || !subscription.keys) {
|
|
return NextResponse.json({ error: 'Invalid subscription object' }, { status: 400 });
|
|
}
|
|
|
|
await prisma.pushSubscription.upsert({
|
|
where: { endpoint: subscription.endpoint },
|
|
update: {
|
|
p256dh: subscription.keys.p256dh,
|
|
auth: subscription.keys.auth,
|
|
},
|
|
create: {
|
|
endpoint: subscription.endpoint,
|
|
p256dh: subscription.keys.p256dh,
|
|
auth: subscription.keys.auth,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('Error saving subscription:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|