From 6c5e6d0a67ae143cd43f28da677d0456e58ebe63 Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Mon, 25 May 2026 20:04:14 +0200 Subject: [PATCH] fix(workshops): allow re-registration after cancellation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Real evidence-based Rule 8 gap found by reading the workshop signup flow code top-to-bottom (the audit-driven approach having proven unreliable in prior sessions). The cancel route at registrations/[id]/route.ts sets the registration row's status='cancelled' rather than deleting it (line 60-72). The register route's duplicate-check at register/route.ts:41-50 then matches that row when the same user tries to sign up again — without filtering by status — and returns a 409 "Du bist bereits angemeldet." Net: a user who explicitly cancels a workshop registration is then permanently locked out of re-registering for that same workshop. Confusing because the message says "already registered" when they just cancelled. Silent failure mode that's invisible to the cancel flow author and the register flow author independently — the bug only emerges when both flows are read together. One-line fix: import `ne` from drizzle-orm and add `ne(workshopRegistrations.status, WORKSHOP_REGISTRATION_STATUS.CANCELLED)` to the duplicate-check WHERE. Tests: added `ne` to the drizzle-orm mock (was failing because the new import returns undefined). Added a new test case that inspects the duplicate-check WHERE clause and asserts it contains an `__ne` condition excluding 'cancelled' — locks in the fix so a future refactor can't drop the filter silently. 8 register-route tests pass; 214 workshop tests across 26 suites all green; tsc clean. This is the THIRD successful pivot to a non-IT-Hilfe area for evidence-based Rule 8 (after marketplace and intake). Workshop area is now spot-audited; the rest of the workshop instance/proposal/ materials flow looked correctly wired in the read-through. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../register/__tests__/route.test.ts | 33 +++++++++++++++++++ src/app/api/workshops/register/route.ts | 11 +++++-- 2 files changed, 41 insertions(+), 3 deletions(-) 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