fix(workshops): allow re-registration after cancellation

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) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-25 20:04:14 +02:00
1 parent b8ab3c7a50
commit 6c5e6d0a67
2 files changed
+41 -3

No files matched your search

@@ -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)
})
})
// ============================================================================
+8 -3
View File
@@ -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