diff --git a/src/app/api/it-hilfe/requests/__tests__/route.test.ts b/src/app/api/it-hilfe/requests/__tests__/route.test.ts index 196ed47ce..4892f9d27 100644 --- a/src/app/api/it-hilfe/requests/__tests__/route.test.ts +++ b/src/app/api/it-hilfe/requests/__tests__/route.test.ts @@ -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 () => { diff --git a/src/app/api/it-hilfe/requests/route.ts b/src/app/api/it-hilfe/requests/route.ts index db7ca49f0..a755f754f 100644 --- a/src/app/api/it-hilfe/requests/route.ts +++ b/src/app/api/it-hilfe/requests/route.ts @@ -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({ diff --git a/src/lib/it-hilfe/__tests__/notifications.test.ts b/src/lib/it-hilfe/__tests__/notifications.test.ts index d17301f50..3916f73db 100644 --- a/src/lib/it-hilfe/__tests__/notifications.test.ts +++ b/src/lib/it-hilfe/__tests__/notifications.test.ts @@ -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() diff --git a/src/lib/it-hilfe/notifications.ts b/src/lib/it-hilfe/notifications.ts index 1b9e6f00d..fae9a67fb 100644 --- a/src/lib/it-hilfe/notifications.ts +++ b/src/lib/it-hilfe/notifications.ts @@ -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,