All files / src/presentation/http/routes user.routes.ts

98.18% Statements 54/55
87.5% Branches 21/24
91.66% Functions 11/12
98.18% Lines 54/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305                                                    1x       22x 22x 22x 22x 22x 22x 22x 22x 22x               22x                                       9x 9x                   3x                   6x 1x     5x 1x     4x 3x     1x         22x                                 4x 4x   2x                 2x                         1x           1x                           1x                             1x                         2x 1x     1x         22x                                           2x   2x 1x     1x   1x 1x       1x                 1x         1x       22x                                         7x     7x           2x 2x   2x   5x 1x     4x 3x     1x          
import { z } from 'zod';
import type { App } from '../../../infrastructure/http/build-app';
import { UpdateUserProfileUseCase } from '../../../application/user/update-user-profile.use-case';
import { DeleteUserAccountUseCase } from '../../../application/user/delete-user-account.use-case';
import { ExportUserDataUseCase } from '../../../application/user/export-user-data.use-case';
import { EmailAlreadyInUseError } from '../../../application/user/errors/email-already-in-use.error';
import { UserNotFoundError } from '../../../application/user/errors/user-not-found.error';
import { DomainError } from '../../../shared/errors/domain-error';
import { PrismaUserRepository } from '../../../infrastructure/database/repositories/prisma-user.repository';
import { emailService } from '../../../infrastructure/email/email-service.instance';
import { verificationCodeService } from '../../../infrastructure/security/verification-code-service.instance';
import { PrismaBabyRepository } from '../../../infrastructure/database/repositories/prisma-baby.repository';
import { PrismaBabyVaccineRecordRepository } from '../../../infrastructure/database/repositories/prisma-baby-vaccine-record.repository';
import { PrismaAppointmentRepository } from '../../../infrastructure/database/repositories/prisma-appointment.repository';
import { PrismaMilestoneRepository } from '../../../infrastructure/database/repositories/prisma-milestone.repository';
import { BcryptPasswordHasher } from '../../../infrastructure/security/bcrypt-password-hasher';
import { prisma } from '../../../infrastructure/database/prisma-client';
import { revokeRefreshTokenIfPresent } from '../../../infrastructure/security/revoke-refresh-token';
import { authenticate } from '../plugins/authenticate';
import { env } from '../../../shared/config/env';
import { authSuccessResponseSchema } from '../schemas/auth.schema';
import { REFRESH_TOKEN_COOKIE, clearAuthCookies } from '../utils/auth-cookies';
import { authErrorResponseSchema, currentUserResponseSchema } from '../schemas/auth.schema';
import { deleteAccountBodySchema, exportUserDataResponseSchema, updateProfileBodySchema } from '../schemas/user.schema';
 
function toDateOnly(date: Date | null): string | null {
  return date ? date.toISOString().slice(0, 10) : null;
}
 
export async function userRoutes(app: App) {
  const userRepository = new PrismaUserRepository(prisma);
  const babyRepository = new PrismaBabyRepository(prisma);
  const babyVaccineRecordRepository = new PrismaBabyVaccineRecordRepository(prisma);
  const appointmentRepository = new PrismaAppointmentRepository(prisma);
  const milestoneRepository = new PrismaMilestoneRepository(prisma);
  const passwordHasher = new BcryptPasswordHasher();
  const updateUserProfileUseCase = new UpdateUserProfileUseCase(userRepository, passwordHasher);
  const deleteUserAccountUseCase = new DeleteUserAccountUseCase(userRepository, passwordHasher, verificationCodeService);
  const exportUserDataUseCase = new ExportUserDataUseCase(
    userRepository,
    babyRepository,
    babyVaccineRecordRepository,
    appointmentRepository,
    milestoneRepository,
  );
 
  app.route({
    method: 'PATCH',
    url: '/users/me',
    preHandler: authenticate,
    schema: {
      tags: ['Users'],
      summary: "Update the authenticated user's profile",
      description:
        'Updates name, email and/or password for the authenticated account. currentPassword is required ' +
        'whenever email or password are being changed.',
      body: updateProfileBodySchema,
      response: {
        200: currentUserResponseSchema,
        400: authErrorResponseSchema,
        401: authErrorResponseSchema,
        409: authErrorResponseSchema,
        500: authErrorResponseSchema,
      },
    },
    handler: async (request, reply) => {
      try {
        const user = await updateUserProfileUseCase.execute({
          userId: request.userId,
          name: request.body.name,
          avatarUrl: request.body.avatarUrl,
          email: request.body.email,
          password: request.body.password,
          currentPassword: request.body.currentPassword,
          emailNotificationsEnabled: request.body.emailNotificationsEnabled,
        });
 
        return reply.status(200).send({
          id: user.id,
          email: user.email,
          name: user.name,
          avatarUrl: user.avatarUrl,
          emailNotificationsEnabled: user.emailNotificationsEnabled,
          createdAt: user.createdAt.toISOString(),
          demoExpiresAt: user.demoExpiresAt?.toISOString() ?? null,
        });
      } catch (error) {
        if (error instanceof EmailAlreadyInUseError) {
          return reply.status(409).send({ status: 'error', message: error.message });
        }
 
        if (error instanceof UserNotFoundError) {
          return reply.status(401).send({ status: 'error', message: 'Authentication required' });
        }
 
        if (error instanceof DomainError) {
          return reply.status(400).send({ status: 'error', message: error.message });
        }
 
        throw error;
      }
    },
  });
 
  app.route({
    method: 'GET',
    url: '/users/me/export',
    preHandler: authenticate,
    schema: {
      tags: ['Users'],
      summary: "Export the authenticated user's full data set",
      description:
        'LGPD data portability export: the user profile and, for every baby they own, the baby profile, all ' +
        'vaccine records, appointments and milestones — as a single JSON document.',
      response: {
        200: exportUserDataResponseSchema,
        401: authErrorResponseSchema,
        500: authErrorResponseSchema,
      },
    },
    handler: async (request, reply) => {
      try {
        const data = await exportUserDataUseCase.execute({ userId: request.userId });
 
        return reply.status(200).send({
          user: {
            id: data.user.id,
            email: data.user.email,
            name: data.user.name,
            avatarUrl: data.user.avatarUrl,
            emailNotificationsEnabled: data.user.emailNotificationsEnabled,
            createdAt: data.user.createdAt.toISOString(),
          },
          babies: data.babies.map(({ baby, vaccineRecords, appointments, milestones }) => ({
            profile: {
              id: baby.id,
              userId: baby.userId,
              name: baby.name,
              birthDate: baby.birthDate.toISOString().slice(0, 10),
              sexAtBirth: baby.sexAtBirth,
              bloodType: baby.bloodType,
              allergies: baby.allergies,
              healthPlanName: baby.healthPlanName,
              healthPlanNumber: baby.healthPlanNumber,
              avatarUrl: baby.avatarUrl,
              avatarColor: baby.avatarColor,
              measurements: baby.measurements.map((measurement) => ({
                ...measurement,
                measuredOn: measurement.measuredOn.toISOString().slice(0, 10),
              })),
              createdAt: baby.createdAt.toISOString(),
            },
            vaccineRecords: vaccineRecords.map((record) => ({
              id: record.id,
              vaccineId: record.vaccineId,
              source: record.source,
              customName: record.customName,
              customDose: record.customDose,
              status: 'APPLIED' as const,
              applicationDate: toDateOnly(record.applicationDate),
              notes: record.notes,
              batchNumber: record.batchNumber,
              location: record.location,
              professional: record.professional,
              photoUrl: record.photoUrl,
            })),
            appointments: appointments.map((appointment) => ({
              id: appointment.id,
              babyId: appointment.babyId,
              scheduledAt: appointment.scheduledAt.toISOString(),
              doctorName: appointment.doctorName,
              specialty: appointment.specialty,
              location: appointment.location,
              reason: appointment.reason,
              notes: appointment.notes,
              status: appointment.status,
              specialistId: appointment.specialistId,
              weightGrams: appointment.weightGrams,
              heightMillimeters: appointment.heightMillimeters,
              createdAt: appointment.createdAt.toISOString(),
            })),
            milestones: milestones.map((milestone) => ({
              id: milestone.id,
              babyId: milestone.babyId,
              title: milestone.title,
              description: milestone.description,
              achievedAt: milestone.achievedAt.toISOString().slice(0, 10),
              category: milestone.category,
              photoUrl: milestone.photoUrl,
              createdAt: milestone.createdAt.toISOString(),
            })),
          })),
        });
      } catch (error) {
        if (error instanceof UserNotFoundError) {
          return reply.status(401).send({ status: 'error', message: 'Authentication required' });
        }
 
        throw error;
      }
    },
  });
 
  app.route({
    method: 'POST',
    url: '/users/me/deletion-code',
    preHandler: authenticate,
    // Same ceiling as the assisted flows: this mails somebody, and the address is theirs, but a
    // loop hitting it is still a loop hitting a mailbox.
    config: { rateLimit: { max: env.rateLimits.authPerMinute, timeWindow: '1 minute' } },
    schema: {
      tags: ['Users'],
      summary: 'Mail a code that confirms deleting this account',
      description:
        'Mails a 6-digit code to the address on the account, spendable only on DELETE /users/me. The address is ' +
        'read from the session, never from the request, so the caller cannot choose whose deletion they are ' +
        'confirming. Answers 200 even when the address is throttled — the caller has nothing to do differently.',
      response: {
        200: authSuccessResponseSchema,
        401: authErrorResponseSchema,
        429: authErrorResponseSchema,
        500: authErrorResponseSchema,
      },
    },
    handler: async (request, reply) => {
      const user = await userRepository.findById(request.userId);
 
      if (!user) {
        return reply.status(401).send({ status: 'error', message: 'Authentication required' });
      }
 
      const code = await verificationCodeService.issue('account-deletion', user.email);
 
      Eif (code) {
        Eif (env.NODE_ENV !== 'production') {
          // Same escape hatch the assisted flows have, and for the same reason: EmailService
          // no-ops without RESEND_API_KEY, so off-production this flow would be a 200 with
          // nothing behind it.
          request.log.info(
            { purpose: 'account-deletion', email: user.email, code },
            'auth.verification_code_issued (non-production only)',
          );
        }
 
        // Not awaited, like the assisted flows: a third-party network call has no business
        // deciding how long this response takes, and a delivery failure is not something the
        // caller can act on.
        void emailService.sendAccountDeletionCodeEmail(user.email, code).catch((error: unknown) => {
          request.log.error({ err: error, userId: user.id }, 'auth.deletion_code_email_failed');
        });
      }
 
      return reply.status(200).send({ status: 'ok', message: 'A code is on its way' });
    },
  });
 
  app.route({
    method: 'DELETE',
    url: '/users/me',
    preHandler: authenticate,
    schema: {
      tags: ['Users'],
      summary: "Delete the authenticated user's account",
      description:
        'Permanently deletes the account, cascading to all owned babies, their vaccine records, appointments, ' +
        'milestones and notifications, and clears the session cookies. Confirmed by **either** currentPassword or ' +
        'a code from POST /users/me/deletion-code — never both. The code path exists because an account created by ' +
        'passwordless sign-in has no usable password, and would otherwise have no way to delete itself.',
      body: deleteAccountBodySchema,
      response: {
        204: z.null().describe('Account deleted successfully'),
        400: authErrorResponseSchema,
        401: authErrorResponseSchema,
        500: authErrorResponseSchema,
      },
    },
    handler: async (request, reply) => {
      try {
        // The schema already refused "neither" and "both", so exactly one of these is set and the
        // narrowing below is the shape the use case asks for.
        await deleteUserAccountUseCase.execute(
          request.body.code !== undefined
            ? { userId: request.userId, code: request.body.code }
            : { userId: request.userId, currentPassword: request.body.currentPassword! },
        );
 
        await revokeRefreshTokenIfPresent(request.cookies[REFRESH_TOKEN_COOKIE]);
        clearAuthCookies(reply);
 
        return reply.status(204).send(null);
      } catch (error) {
        if (error instanceof UserNotFoundError) {
          return reply.status(401).send({ status: 'error', message: 'Authentication required' });
        }
 
        if (error instanceof DomainError) {
          return reply.status(400).send({ status: 'error', message: error.message });
        }
 
        throw error;
      }
    },
  });
}