fix(it-hilfe): suppress duplicate confirmation email for new-anonymous-account submissions

Adjacent-broken fix on the just-shipped anonymous-post flow.
sendRequestCreatedNotifications fired unconditionally for every new
request, which meant new-anonymous-user submissions got TWO emails:

  1. The standard "Deine IT-Hilfe Anfrage wurde erstellt" with a
     "view your request" link → bounces to /auth/login → user has
     no password yet → frustrated.
  2. The new "Konto aktivieren" claim email (aa71c332's 7-day TTL)
     with the actual activation link.

Two emails to the same person, one of which doesn't work yet. The
user might open the wrong one first. Real ship-blocking UX issue.

Fix:

1. sendRequestCreatedNotifications (src/lib/it-hilfe/notifications.ts)
   gains an optional `includeRequesterConfirmation` flag in NotifyParams.
   Default behaviour preserved (true) — the standard requester email
   still goes out for all existing callers. Admin notification and
   matching-helper notifications fire regardless of the flag.

2. POST /api/it-hilfe/requests passes
   `includeRequesterConfirmation: !isNewAnonymousUser`. Only the new
   claim email goes out for fresh anonymous accounts; existing-account
   anonymous submitters (wasCreated: false) still get the standard
   confirmation since they have a password and the link works for them.

Tests: 2 new in notifications.test.ts (SUPPRESSES requester confirmation
when flag is false; sends it when explicit true). 1 new in the route
test (asserts notifArgs.includeRequesterConfirmation is false for
new-account path AND true for existing-account path). 919 tests
across 58 suites (it-hilfe + auth + password) pass; tsc clean.

End-to-end anonymous-post is now structurally clean: one email goes
out, with one CTA, that does exactly what the user needs to do next.

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 18:26:37 +02:00
1 parent aa71c332be
commit 62ba111c99
4 files changed
+69 -3

No files matched your search

@@ -374,6 +374,33 @@ describe('POST /api/it-hilfe/requests — anonymous submissions', () => {
expect(claimUrl).toContain('callbackUrl=')
expect(claimUrl).toContain(encodeURIComponent('/it-hilfe/req-new'))
expect(claimUrl).toContain('token=')
// The standard request-confirmation email must be SUPPRESSED for new
// anonymous accounts (the claim email replaces it). Two emails to the
// same person would confuse and the standard one's "view your request"
// link wouldn't work until they've claimed.
const notifMod = require('@/lib/it-hilfe/notifications')
const notifArgs = notifMod.sendRequestCreatedNotifications.mock.calls[0][0]
expect(notifArgs.includeRequesterConfirmation).toBe(false)
})
it('keeps requester confirmation enabled when wasCreated is false (existing account)', async () => {
mockAuth.mockResolvedValueOnce(null)
mockFindOrCreateAnonymousUser.mockResolvedValueOnce({ userId: 'existing-2', wasCreated: false })
const schemas = require('@/lib/schemas/it-hilfe')
schemas.validateAndRespond.mockReturnValueOnce({
success: true,
data: { title: 'X', submitterEmail: 'known@example.com' },
})
await POST(makePostRequest({ submitterEmail: 'known@example.com' }))
await new Promise(resolve => setImmediate(resolve))
const notifMod = require('@/lib/it-hilfe/notifications')
const notifArgs = notifMod.sendRequestCreatedNotifications.mock.calls[0][0]
// Existing user: they have a password, the standard confirmation email's
// link works for them — DON'T suppress it.
expect(notifArgs.includeRequesterConfirmation).toBe(true)
})
it('does NOT send the claim email when wasCreated is false (existing account)', async () => {
+8 -1
View File
@@ -307,7 +307,13 @@ export async function POST(request: NextRequest) {
.catch(err => logger.error('Failed to send anonymous-request claim email', { err, requestId, email: requesterEmail }))
}
// Fire-and-forget: Send all notifications (confirmation, admin, matching helpers)
// Fire-and-forget: Send all notifications (confirmation, admin, matching helpers).
// For brand-new anonymous accounts, the dedicated claim email above
// replaces the standard requester confirmation — sending both would
// give the user two emails, one with a working "Konto aktivieren" CTA
// and one with a "view your request" link they can't follow until
// they've claimed the account. Admin + matching-helper notifications
// still fire.
sendRequestCreatedNotifications({
requestId,
requesterId,
@@ -320,6 +326,7 @@ export async function POST(request: NextRequest) {
serviceType: serviceType ?? 'flexible',
skillsNeeded: skillsNeeded || [],
aiDiagnosis: aiDiagnosis || null,
includeRequesterConfirmation: !isNewAnonymousUser,
})
return apiSuccess({
@@ -172,6 +172,27 @@ describe('sendRequestCreatedNotifications', () => {
expect(calls).toContain('notify@revamp-it.ch')
})
it('SUPPRESSES requester confirmation when includeRequesterConfirmation is false', async () => {
// The IT-Hilfe anonymous-post flow uses this: the dedicated claim
// email replaces the standard confirmation (which would link to a
// request the new user can't yet view).
sendRequestCreatedNotifications({ ...BASE_PARAMS, includeRequesterConfirmation: false })
await flushAsync()
const calls = (mockSendCustomEmail.mock.calls as [string, unknown][]).map(([to]) => to)
expect(calls).not.toContain('hans@example.com')
// Admin notification still goes through
expect(calls).toContain('notify@revamp-it.ch')
})
it('sends requester confirmation when includeRequesterConfirmation is true (explicit)', async () => {
sendRequestCreatedNotifications({ ...BASE_PARAMS, includeRequesterConfirmation: true })
await flushAsync()
const calls = (mockSendCustomEmail.mock.calls as [string, unknown][]).map(([to]) => to)
expect(calls).toContain('hans@example.com')
})
it('queries DB for matching helpers when skillsNeeded is non-empty', async () => {
sendRequestCreatedNotifications(BASE_PARAMS)
await flushAsync()
+13 -2
View File
@@ -41,6 +41,16 @@ interface NotifyParams {
serviceType: string
skillsNeeded: string[]
aiDiagnosis: string | null
/**
* When true (default), sends the standard request-confirmation email
* to the requester. Set to false when the caller is already sending a
* dedicated email that supersedes the confirmation — e.g. the IT-Hilfe
* anonymous-post claim flow, where the requester has no active session
* yet and the standard email's "view your request" link would bounce
* them to login they can't perform until they claim their account.
* Admin notification + matching-helper notifications still fire.
*/
includeRequesterConfirmation?: boolean
}
/**
@@ -52,8 +62,9 @@ export function sendRequestCreatedNotifications(params: NotifyParams): void {
const categoryName = getCategoryById(params.categoryId)?.name || params.categoryId
const urgencyName = getUrgencyById(params.urgency)?.name || params.urgency
// 1. Confirmation to requester
if (params.requesterEmail) {
// 1. Confirmation to requester (suppressed for new-anonymous accounts
// where the dedicated claim email replaces it)
if (params.requesterEmail && params.includeRequesterConfirmation !== false) {
const content = itHilfeRequestConfirmation(
params.requesterName,
params.title,