From 27a30f2080f8f250404c11b69769e4cda8341429 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 21:02:36 +0200 Subject: [PATCH] feat(contact): add logged-in user to contact form mail Read the Authentication cookie server-side and add a line with the logged-in user (name, email, id) to the mail, or 'not logged in'. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_012GTicqeyaQ8prWTxxfKSrP --- app/__tests__/app.controller.spec.ts | 20 ++++++++++++++++++++ app/app.controller.ts | 6 ++++-- auth/__tests__/auth.service.spec.ts | 17 +++++++++++++++++ auth/auth.service.ts | 11 +++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/app/__tests__/app.controller.spec.ts b/app/__tests__/app.controller.spec.ts index 1ce150a..812a5a1 100644 --- a/app/__tests__/app.controller.spec.ts +++ b/app/__tests__/app.controller.spec.ts @@ -33,6 +33,7 @@ describe('AppController', () => { getProfile: jest.fn().mockResolvedValue({}), updateSettings: jest.fn().mockResolvedValue({}), updateProfile: jest.fn().mockResolvedValue({}), + getUserFromToken: jest.fn().mockReturnValue(undefined), }, }, { @@ -213,6 +214,25 @@ describe('AppController', () => { expect(result).toEqual({ success: true }); }); + it('should mark an anonymous contact form submission as not logged in', async () => { + await appController.submitContact({ name: 'A', email: 'a@b.com', message: 'hi' }, undefined, { cookies: {} } as any); + + expect(mailSendService.sendMail).toHaveBeenCalledWith( + expect.objectContaining({ html: expect.stringContaining('לא מחובר') }), + ); + }); + + it('should add the logged in user to the contact form mail', async () => { + (authService.getUserFromToken as jest.Mock).mockReturnValue({ id: 7, email: 'user@school.com', name: 'School' }); + + await appController.submitContact({ name: 'A', email: 'a@b.com', message: 'hi' }, undefined, { cookies: { Authentication: 'tok' } } as any); + + expect(authService.getUserFromToken).toHaveBeenCalledWith('tok'); + expect(mailSendService.sendMail).toHaveBeenCalledWith( + expect.objectContaining({ html: expect.stringContaining('user@school.com, מזהה 7') }), + ); + }); + it('should reject a contact form submission missing required fields', async () => { await expect( appController.submitContact({ name: 'Test User' }, undefined), diff --git a/app/app.controller.ts b/app/app.controller.ts index 4c8d095..f3c68e2 100644 --- a/app/app.controller.ts +++ b/app/app.controller.ts @@ -16,7 +16,7 @@ import { AppService } from './app.service'; import { AuthService, UpdateProfileData } from '@shared/auth/auth.service'; import { JwtAuthGuard } from '@shared/auth/jwt-auth.guard'; import { LocalAuthGuard } from '@shared/auth/local-auth.guard'; -import { Response } from 'express'; +import { Request as ExpressRequest, Response } from 'express'; import { LocalRegisterAuthGuard } from '@shared/auth/local-register-auth.guard'; import { getUserIdFromUser } from '@shared/auth/auth.util'; import { isAdmin } from '@shared/utils/permissionsUtil'; @@ -123,10 +123,11 @@ export class AppController { @Post('contact') @HttpCode(200) - async submitContact(@Body() body: ContactFormBody, @Headers('origin') origin?: string) { + async submitContact(@Body() body: ContactFormBody, @Headers('origin') origin?: string, @Request() req?: ExpressRequest) { if (!body?.name || !body?.email || !body?.message) { throw new BadRequestException('נא למלא שם, אימייל והודעה'); } + const loggedInUser = this.authService.getUserFromToken(req?.cookies?.Authentication); const html = `

שם: ${body.name}

אימייל: ${body.email}

@@ -134,6 +135,7 @@ export class AppController {

הודעה:

${body.message}

${origin ? `

מקור: ${origin}

` : ''} +

משתמש מחובר: ${loggedInUser ? `${loggedInUser.name || ''} (${loggedInUser.email}, מזהה ${loggedInUser.id})` : 'לא מחובר'}

`; await this.mailSendService.sendMail({ to: CONTACT_MAIL_TO, diff --git a/auth/__tests__/auth.service.spec.ts b/auth/__tests__/auth.service.spec.ts index 046f1f7..8f9c550 100644 --- a/auth/__tests__/auth.service.spec.ts +++ b/auth/__tests__/auth.service.spec.ts @@ -34,6 +34,7 @@ describe('AuthService', () => { provide: JwtService, useValue: { sign: jest.fn(), + verify: jest.fn(), }, }, { @@ -583,4 +584,20 @@ describe('AuthService', () => { }); }); }); + + describe('getUserFromToken', () => { + it('returns undefined without a token', () => { + expect(service.getUserFromToken(undefined)).toBeUndefined(); + }); + + it('returns the verified payload', () => { + (jwtService.verify as jest.Mock).mockReturnValue({ id: 3 }); + expect(service.getUserFromToken('tok')).toEqual({ id: 3 }); + }); + + it('returns undefined for an invalid token', () => { + (jwtService.verify as jest.Mock).mockImplementation(() => { throw new Error('bad'); }); + expect(service.getUserFromToken('tok')).toBeUndefined(); + }); + }); }); diff --git a/auth/auth.service.ts b/auth/auth.service.ts index db92e96..02e85f6 100644 --- a/auth/auth.service.ts +++ b/auth/auth.service.ts @@ -101,6 +101,17 @@ export class AuthService { }); } + getUserFromToken(token?: string): IAuthenticatedUser | undefined { + if (!token) { + return undefined; + } + try { + return this.jwtService.verify(token); + } catch { + return undefined; + } + } + async getCookieForLogOut(user?: IAuthenticatedUser) { if (!user?.impersonated) { return cookie.serialize('Authentication', '', {