diff --git a/src/app/api/workshops/register/__tests__/route.test.ts b/src/app/api/workshops/register/__tests__/route.test.ts index 0a29687f1..de724e0e9 100644 --- a/src/app/api/workshops/register/__tests__/route.test.ts +++ b/src/app/api/workshops/register/__tests__/route.test.ts @@ -59,6 +59,7 @@ jest.mock('@/db/schema', () => ({ jest.mock('drizzle-orm', () => ({ eq: (a: unknown, b: unknown) => ({ __eq: [a, b] }), and: (...args: unknown[]) => ({ __and: args }), + ne: (a: unknown, b: unknown) => ({ __ne: [a, b] }), sql: Object.assign( (_strings: TemplateStringsArray, ..._values: unknown[]) => ({ __sql: true }), { raw: (s: string) => ({ __raw: s }) } @@ -250,6 +251,38 @@ describe('POST /api/workshops/register — already registered', () => { const body = await response.json() expect(body.error).toMatch(/bereits/i) }) + + it('duplicate check WHERE excludes cancelled registrations', async () => { + // Without this filter, a user who cancelled a prior registration is + // locked out forever — the cancel route sets status='cancelled' but + // leaves the row in place. Re-registering should succeed once they + // change their mind. + const parallelChain = makeParallelChain(null) + const instanceChain = makeParallelChain(MOCK_INSTANCE) + mockSelect + .mockReturnValueOnce(makeWorkshopChain(MOCK_WORKSHOP)) + .mockReturnValueOnce(parallelChain) + .mockReturnValue(instanceChain) + + const req = new NextRequest('http://localhost/api/workshops/register', { + method: 'POST', + body: JSON.stringify({ workshopSlug: 'linux-einfuehrung' }), + headers: { 'Content-Type': 'application/json' }, + }) + await POST(req) + + // Inspect the duplicate-check's WHERE clause: it should be an `and(...)` + // wrapping eq(userId), eq(workshopId), AND ne(status, 'cancelled'). + const whereMock = (parallelChain.from as jest.Mock).mock.results[0].value.innerJoin.mock.results[0].value.where as jest.Mock + const whereArg = whereMock.mock.calls[0][0] as { __and?: unknown[] } + expect(whereArg.__and).toBeDefined() + const containsCancelledExclusion = whereArg.__and!.some( + cond => typeof cond === 'object' && cond !== null && '__ne' in cond && + Array.isArray((cond as { __ne: unknown[] }).__ne) && + (cond as { __ne: unknown[] }).__ne[1] === 'cancelled' + ) + expect(containsCancelledExclusion).toBe(true) + }) }) // ============================================================================ diff --git a/src/app/api/workshops/register/route.ts b/src/app/api/workshops/register/route.ts index 4051e6a70..c62588f7d 100644 --- a/src/app/api/workshops/register/route.ts +++ b/src/app/api/workshops/register/route.ts @@ -2,7 +2,7 @@ import { NextRequest } from 'next/server' import { withAuth, ValidSession } from '@/lib/api/middleware' import { db } from '@/db' import { workshops, workshopInstances, workshopRegistrations } from '@/db/schema' -import { eq, and } from 'drizzle-orm' +import { eq, and, ne } from 'drizzle-orm' import { apiError, apiSuccess, apiBadRequest, apiNotFound } from '@/lib/api/helpers' import { ERROR_MESSAGES, SUCCESS_MESSAGES } from '@/config/error-messages' import { WORKSHOP_REGISTRATION_STATUS } from '@/config/workshop-registration-status' @@ -37,7 +37,11 @@ export const POST = withAuth(async (request: NextRequest, session: ValidSession) return apiNotFound('Workshop') } - // Duplicate check + next instance lookup are independent — run in parallel + // Duplicate check + next instance lookup are independent — run in parallel. + // Excludes cancelled registrations so a user who explicitly cancelled can + // re-register; without that filter the cancel route's set-status-to- + // 'cancelled' (vs. row deletion) would lock them out forever with a + // confusing 409. const [existing, instance] = await Promise.all([ db .select({ id: workshopRegistrations.id }) @@ -45,7 +49,8 @@ export const POST = withAuth(async (request: NextRequest, session: ValidSession) .innerJoin(workshopInstances, eq(workshopRegistrations.workshopInstanceId, workshopInstances.id)) .where(and( eq(workshopRegistrations.userId, session.user.id), - eq(workshopInstances.workshopId, workshop.id) + eq(workshopInstances.workshopId, workshop.id), + ne(workshopRegistrations.status, WORKSHOP_REGISTRATION_STATUS.CANCELLED), )) .then(rows => rows[0]), db