All files / src/application/user export-user-data.use-case.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 3/3
100% Lines 13/13

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                                                                  22x 22x 22x 22x 22x       3x   3x 1x     2x   2x   2x           2x       2x                          
import { UserRepository } from './user-repository';
import { UserNotFoundError } from './errors/user-not-found.error';
import { BabyRepository } from '../baby/baby-repository';
import { Baby } from '../../domain/baby/baby';
import { BabyVaccineRecordRepository } from '../vaccine/baby-vaccine-record-repository';
import { BabyVaccineRecord } from '../../domain/vaccine/baby-vaccine-record';
import { AppointmentRepository } from '../appointment/appointment-repository';
import { Appointment } from '../../domain/appointment/appointment';
import { MilestoneRepository } from '../milestone/milestone-repository';
import { Milestone } from '../../domain/milestone/milestone';
 
export interface ExportUserDataInput {
  userId: string;
}
 
export interface ExportedBaby {
  baby: Baby;
  vaccineRecords: BabyVaccineRecord[];
  appointments: Appointment[];
  milestones: Milestone[];
}
 
export interface ExportedUserData {
  user: { avatarUrl: string | null; id: string; email: string; name: string; emailNotificationsEnabled: boolean; createdAt: Date };
  babies: ExportedBaby[];
}
 
// Powers the LGPD "right to data portability" export (GET /users/me/export) — composes the
// existing per-feature repositories rather than adding a bespoke query, so it stays consistent
// with whatever each feature considers "this baby's data" (e.g. vaccine records include both
// catalog and adhoc entries, matching what /babies/:babyId/vaccines/adhoc already exposes).
export class ExportUserDataUseCase {
  constructor(
    private readonly userRepository: UserRepository,
    private readonly babyRepository: BabyRepository,
    private readonly babyVaccineRecordRepository: BabyVaccineRecordRepository,
    private readonly appointmentRepository: AppointmentRepository,
    private readonly milestoneRepository: MilestoneRepository,
  ) {}
 
  async execute(input: ExportUserDataInput): Promise<ExportedUserData> {
    const user = await this.userRepository.findById(input.userId);
 
    if (!user) {
      throw new UserNotFoundError();
    }
 
    const babies = await this.babyRepository.findAllByUserId(input.userId);
 
    const exportedBabies = await Promise.all(
      babies.map(async (baby): Promise<ExportedBaby> => {
        const [vaccineRecords, appointments, milestones] = await Promise.all([
          this.babyVaccineRecordRepository.findAllByBabyId(baby.id),
          this.appointmentRepository.findAllByBabyId(baby.id),
          this.milestoneRepository.findAllByBabyId(baby.id),
        ]);
 
        return { baby, vaccineRecords, appointments, milestones };
      }),
    );
 
    return {
      user: {
        id: user.id,
        email: user.email,
        name: user.name,
        avatarUrl: user.avatarUrl,
        emailNotificationsEnabled: user.emailNotificationsEnabled,
        createdAt: user.createdAt,
      },
      babies: exportedBabies,
    };
  }
}