Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /** * Push Notification Unsubscribe API * * Removes push notification subscriptions. * POST /api/push/unsubscribe */ import { NextRequest, NextResponse } from 'next/server'; import { withAuth } from '@/lib/api/middleware'; import { successResponse, errorResponse, validationErrorResponse, type ApiResponse, } from '@/lib/api/responses'; import { prisma } from '@/lib/prisma'; import { z } from 'zod'; import type { Session } from 'next-auth'; const unsubscribeSchema = z.object({ endpoint: z.string().url(), }); export const POST = withAuth<ApiResponse<{ message: string }>>( async (request: NextRequest, _context, session: Session) => { try { const userId = session.user?.id; if (!userId || typeof userId !== 'number') { return errorResponse('INVALID_USER', 'User ID not found', { status: 401 }); } const body = await request.json(); const parseResult = unsubscribeSchema.safeParse(body); if (!parseResult.success) { const errorMessages = parseResult.error.issues .map((issue) => issue.message) .join(', '); return validationErrorResponse(errorMessages, parseResult.error.issues); } const { endpoint } = parseResult.data; // Find and delete the subscription const subscription = await prisma.pushSubscription.findFirst({ where: { userId, endpoint, }, }); if (!subscription) { return successResponse({ message: 'Not found' }); } await prisma.pushSubscription.delete({ where: { id: subscription.id }, }); return successResponse({ message: 'Unsubscribed' }); } catch (error) { console.error('Push unsubscribe error:', error); return errorResponse('UNSUBSCRIBE_ERROR', 'Failed to remove push subscription'); } } ) as ( request: Request, context?: { params?: Promise<Record<string, string>> } ) => Promise<NextResponse>; |