/** * User controller — HTTP handlers for the authenticated user's profile (read and update). */ import { Response } from 'express'; import * as authService from '../services/authService'; import { AuthRequest } from '../middleware/auth'; /** * Returns the authenticated user's profile. * @param req - Authenticated request; user ID is taken from the JWT. * @param res - JSON profile object on success, 400 on failure. */ export async function getMe(req: AuthRequest, res: Response): Promise { try { const userId = req.user!.userId; const profile = await authService.getProfile(userId); res.json(profile); } catch (e) { const msg = e instanceof Error ? e.message : 'Failed to load profile'; res.status(400).json({ error: msg }); } } /** * Updates the authenticated user's profile fields (name, date of birth, email, phone). * @param req - Authenticated request. Body: optional `firstName`, `middleName`, `lastName`, `dateOfBirth`, `email`, `phone`. * @param res - JSON `{ message: 'Profile updated' }` on success, 400 on validation or update failure. */ export async function updateMe(req: AuthRequest, res: Response): Promise { try { const userId = req.user!.userId; const { firstName, middleName, lastName, dateOfBirth, email, phone, countryOfResidence, nationality, gender } = req.body; await authService.updateProfile(userId, { firstName: firstName ?? undefined, middleName: middleName ?? undefined, lastName: lastName ?? undefined, dateOfBirth: dateOfBirth ?? undefined, email: email ?? undefined, phone: phone !== undefined ? phone : undefined, countryOfResidence: countryOfResidence !== undefined ? countryOfResidence : undefined, nationality: nationality !== undefined ? nationality : undefined, gender: gender !== undefined ? gender : undefined, }); res.json({ message: 'Profile updated' }); } catch (e) { const msg = e instanceof Error ? e.message : 'Update failed'; res.status(400).json({ error: msg }); } }