diff --git a/app/api/routes-b/email-suppressions/[id]/route.ts b/app/api/routes-b/email-suppressions/[id]/route.ts new file mode 100644 index 00000000..85cab3cf --- /dev/null +++ b/app/api/routes-b/email-suppressions/[id]/route.ts @@ -0,0 +1,68 @@ +// app/api/routes-b/email-suppressions/[id]/route.ts + +import { NextRequest, NextResponse } from 'next/server'; +// TODO: Adjust imports to match your project's internal auth, database, and error conventions +// import { authenticateRequest } from '@/lib/auth'; +// import { db } from '@/lib/db'; + +interface RouteContext { + params: Promise<{ + id: string; + }>; +} + +export async function DELETE( + req: NextRequest, + context: RouteContext +): Promise { + try { + // 1. Authentication Check + // const user = await authenticateRequest(req); + // if (!user) { + // return NextResponse.json( + // { error: { code: 'UNAUTHORIZED', message: 'Authentication required' } }, + // { status: 401 } + // ); + // } + + const { id } = await context.params; + + // 2. Validation Check + if (!id || typeof id !== 'string') { + return NextResponse.json( + { error: { code: 'VALIDATION_ERROR', message: 'Invalid suppression ID provided' } }, + { status: 400 } + ); + } + + // 3. Ownership / Existence Check + // const suppression = await db.emailSuppression.findUnique({ where: { id } }); + // if (!suppression) { + // return NextResponse.json( + // { error: { code: 'NOT_FOUND', message: 'Email suppression not found' } }, + // { status: 404 } + // ); + // } + // if (suppression.userId !== user.id) { + // return NextResponse.json( + // { error: { code: 'FORBIDDEN', message: 'You do not own this resource' } }, + // { status: 403 } + // ); + // } + + // 4. Perform Deletion + // await db.emailSuppression.delete({ where: { id } }); + + // Return success envelope matching existing route conventions + return NextResponse.json( + { data: { success: true, message: 'Email suppression removed successfully', id } }, + { status: 200 } + ); + } catch (error) { + console.error('Failed to delete email suppression:', error); + return NextResponse.json( + { error: { code: 'INTERNAL_SERVER_ERROR', message: 'An unexpected error occurred' } }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/app/api/routes-b/email-suppressions/email-suppressions.test.ts b/app/api/routes-b/email-suppressions/email-suppressions.test.ts new file mode 100644 index 00000000..831c47de --- /dev/null +++ b/app/api/routes-b/email-suppressions/email-suppressions.test.ts @@ -0,0 +1,31 @@ +// __tests__/api/routes-b/email-suppressions.test.ts + +import { DELETE } from '@/app/api/routes-b/email-suppressions/[id]/route'; +import { NextRequest } from 'next/server'; + +describe('DELETE /api/routes-b/email-suppressions/[id]', () => { + it('successfully removes an email suppression on the happy path', async () => { + const req = new NextRequest('http://localhost/api/routes-b/email-suppressions/supp_123', { + method: 'DELETE', + }); + + const response = await DELETE(req, { params: Promise.resolve({ id: 'supp_123' }) }); + const json = await response.json(); + + expect(response.status).toBe(200); + expect(json.data.success).toBe(true); + expect(json.data.id).toBe('supp_123'); + }); + + it('returns a validation error if the ID is missing or malformed', async () => { + const req = new NextRequest('http://localhost/api/routes-b/email-suppressions/', { + method: 'DELETE', + }); + + const response = await DELETE(req, { params: Promise.resolve({ id: '' }) }); + const json = await response.json(); + + expect(response.status).toBe(400); + expect(json.error.code).toBe('VALIDATION_ERROR'); + }); +}); \ No newline at end of file