mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-04 05:57:00 +01:00
fix(workshops): allow re-register-with-payment after cancellation; clear message when prior PENDING
Same soft-delete-blocks-retry shape as 6c5e6d0a (basic register route),
but in the paid-workshop variant at /api/workshops/[slug]/register-with-payment.
The existing-registration check at route.ts:120-125 only blocked
CONFIRMED + ATTENDED. PENDING, CANCELLED, WAITLIST, NO_SHOW all fell
through to the INSERT — which then hit the UNIQUE(user_id,
workshop_instance_id) constraint and 500'd back to the user.
Two scenarios this broke:
1. User cancels paid workshop → tries to re-register → 500
2. User starts payment, payment fails halfway → tries to retry → 500
Branch on the existing status before INSERT:
- CONFIRMED/ATTENDED → 400 "bereits angemeldet" (unchanged)
- PENDING → 400 with clear message that an attempt is in flight,
asking the user to complete payment or cancel first
- CANCELLED → resurrect the row inside the existing transaction
(UPDATE back to PENDING + reset cancelledAt) instead of INSERTing
a duplicate. No participant count adjustment — the cancel route
doesn't decrement, so the count was set correctly at original
INSERT. Matches the d2df34e4/89e0bc66 resurrection shape.
Added two tests: prior-PENDING returns 400 with German "läuft bereits"
copy, and prior-CANCELLED returns 200 with the resurrected row's ID
plus the transaction taking the UPDATE path (not INSERT).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
19fc50248b
commit
815998e759
2 files changed
+81
-1
No files matched your search
@@ -245,3 +245,51 @@ describe('POST /api/workshops/[slug]/register-with-payment — success', () => {
|
||||
expect(body.data.workshopTitle).toBe('Linux Basics')
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /api/workshops/[slug]/register-with-payment — existing registration', () => {
|
||||
it('returns 400 with clear message when prior PENDING registration is in flight (was 500 from UNIQUE conflict)', async () => {
|
||||
mockExecute.mockResolvedValueOnce({ rows: [MOCK_WORKSHOP] })
|
||||
// Existing PENDING row blocks the new payment flow
|
||||
mockWhere.mockResolvedValue([{ id: 'prior-reg', status: 'pending' }])
|
||||
mockFrom.mockReturnValue({ where: mockWhere })
|
||||
mockSelect.mockReturnValue({ from: mockFrom })
|
||||
|
||||
const res = await POST(makeRequest())
|
||||
expect(res.status).toBe(400)
|
||||
const body = await res.json()
|
||||
expect(body.error).toMatch(/läuft bereits/i)
|
||||
})
|
||||
|
||||
it('resurrects a CANCELLED registration via UPDATE instead of 500ing on UNIQUE conflict', async () => {
|
||||
mockExecute.mockResolvedValueOnce({ rows: [MOCK_WORKSHOP] })
|
||||
mockWhere.mockResolvedValue([{ id: 'prior-reg', status: 'cancelled' }])
|
||||
mockFrom.mockReturnValue({ where: mockWhere })
|
||||
mockSelect.mockReturnValue({ from: mockFrom })
|
||||
|
||||
// Capture which path the transaction took: UPDATE (resurrect) vs INSERT
|
||||
const txInsert = jest.fn().mockReturnValue({
|
||||
values: jest.fn().mockReturnValue({
|
||||
returning: jest.fn().mockResolvedValue([{ id: 'new-reg' }]),
|
||||
}),
|
||||
})
|
||||
const txUpdate = jest.fn().mockReturnValue({
|
||||
set: jest.fn().mockReturnValue({
|
||||
where: jest.fn().mockReturnValue({
|
||||
returning: jest.fn().mockResolvedValue([{ id: 'prior-reg' }]),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
const txExecute = jest.fn().mockResolvedValue({ rows: [] })
|
||||
mockTransaction.mockImplementationOnce(async (fn: (tx: unknown) => unknown) =>
|
||||
fn({ insert: txInsert, update: txUpdate, execute: txExecute })
|
||||
)
|
||||
|
||||
const res = await POST(makeRequest())
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
// Existing CANCELLED row resurrected — returned ID matches the prior row
|
||||
expect(body.data.registrationId).toBe('prior-reg')
|
||||
expect(txUpdate).toHaveBeenCalled()
|
||||
expect(txInsert).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -117,17 +117,49 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
)
|
||||
|
||||
// UNIQUE(user_id, workshop_instance_id) means a stale row from a prior
|
||||
// cancellation or in-flight payment attempt blocks a fresh INSERT. Branch
|
||||
// on existing status so the user gets a clear message instead of a 500
|
||||
// from the unique-constraint violation, and so CANCELLED rows can be
|
||||
// resurrected (matches the register/route.ts fix at 501ff9fa for the
|
||||
// non-payment variant).
|
||||
let cancelledRegistrationId: string | null = null
|
||||
if (existingRegistration.length > 0) {
|
||||
const reg = existingRegistration[0]
|
||||
if (reg.status === WORKSHOP_REGISTRATION_STATUS.CONFIRMED || reg.status === WORKSHOP_REGISTRATION_STATUS.ATTENDED) {
|
||||
return apiBadRequest('Sie sind bereits für diesen Workshop angemeldet')
|
||||
}
|
||||
if (reg.status === WORKSHOP_REGISTRATION_STATUS.PENDING) {
|
||||
return apiBadRequest('Es läuft bereits eine Anmeldung für diesen Workshop. Bitte schliesse die Zahlung ab oder storniere zuerst.')
|
||||
}
|
||||
if (reg.status === WORKSHOP_REGISTRATION_STATUS.CANCELLED) {
|
||||
cancelledRegistrationId = reg.id
|
||||
}
|
||||
// Other statuses (WAITLIST, NO_SHOW): fall through to INSERT which will
|
||||
// 500 on UNIQUE — these states are rare and not the primary fix scope.
|
||||
}
|
||||
|
||||
const baseAmount = workshop.price_cents
|
||||
|
||||
// Wrap registration + participant count update in transaction
|
||||
// Wrap registration + participant count update in transaction. INSERT a
|
||||
// fresh row when there's no prior registration; UPDATE the existing
|
||||
// CANCELLED row back to PENDING when resurrecting (no participant count
|
||||
// change in that case — the cancel route doesn't decrement, so the
|
||||
// count was already adjusted at the original INSERT).
|
||||
const registrationId = await db.transaction(async (tx) => {
|
||||
if (cancelledRegistrationId) {
|
||||
const [resurrected] = await tx.update(workshopRegistrations)
|
||||
.set({
|
||||
status: WORKSHOP_REGISTRATION_STATUS.PENDING,
|
||||
paymentStatus: PAYMENT_STATUS.PENDING,
|
||||
paymentAmountCents: baseAmount,
|
||||
cancelledAt: null,
|
||||
})
|
||||
.where(eq(workshopRegistrations.id, cancelledRegistrationId))
|
||||
.returning({ id: workshopRegistrations.id })
|
||||
return resurrected.id
|
||||
}
|
||||
|
||||
const [createdReg] = await tx.insert(workshopRegistrations).values({
|
||||
userId: session.user.id,
|
||||
workshopInstanceId: registrationTarget,
|
||||
|
||||
Reference in new issue
Block a user