| 1 | import {
|
| 2 | Activity,
|
| 3 | Appointment,
|
| 4 | CareAction,
|
| 5 | Insight,
|
| 6 | MedicineLog,
|
| 7 | MedicineRegimen,
|
| 8 | Mood,
|
| 9 | MoodComponent,
|
| 10 | Notification,
|
| 11 | SleepRecord,
|
| 12 | Trigger,
|
| 13 | TriggerMoodLink,
|
| 14 | User,
|
| 15 | } from "@prisma/client";
|
| 16 |
|
| 17 | import { prisma } from "@app/lib/prisma";
|
| 18 | import { findUserById } from "@app/services/user.service";
|
| 19 |
|
| 20 | export type ExportData = {
|
| 21 | user: User;
|
| 22 | moods: Mood[];
|
| 23 | moodComponents: MoodComponent[];
|
| 24 | sleepRecords: SleepRecord[];
|
| 25 | triggers: Trigger[];
|
| 26 | triggerMoodLinks: TriggerMoodLink[];
|
| 27 | careActions: CareAction[];
|
| 28 | medicineLogs: MedicineLog[];
|
| 29 | appointments: Appointment[];
|
| 30 | activities: Activity[];
|
| 31 | medicineRegimens: MedicineRegimen[];
|
| 32 | insights: Insight[];
|
| 33 | notifications: Notification[];
|
| 34 | };
|
| 35 |
|
| 36 | export async function gatherExportData(userId: number): Promise<ExportData> {
|
| 37 | const user = await findUserById(userId);
|
| 38 |
|
| 39 | if (!user) {
|
| 40 | throw new Error(`Usuário ${userId} não encontrado para exportação`);
|
| 41 | }
|
| 42 |
|
| 43 | const [
|
| 44 | moods,
|
| 45 | sleepRecords,
|
| 46 | triggers,
|
| 47 | triggerMoodLinks,
|
| 48 | careActions,
|
| 49 | medicineRegimens,
|
| 50 | insights,
|
| 51 | notifications,
|
| 52 | ] = await Promise.all([
|
| 53 | prisma.mood.findMany({
|
| 54 | where: { userId },
|
| 55 | include: { moodComponents: true },
|
| 56 | orderBy: { moment: "asc" },
|
| 57 | }),
|
| 58 | prisma.sleepRecord.findMany({
|
| 59 | where: { userId },
|
| 60 | orderBy: { date: "asc" },
|
| 61 | }),
|
| 62 | prisma.trigger.findMany({
|
| 63 | where: { userId },
|
| 64 | orderBy: { moment: "asc" },
|
| 65 | }),
|
| 66 | prisma.triggerMoodLink.findMany({
|
| 67 | where: { trigger: { userId } },
|
| 68 | orderBy: { linkedAt: "asc" },
|
| 69 | }),
|
| 70 | prisma.careAction.findMany({
|
| 71 | where: { userId },
|
| 72 | include: { medicineLog: true, appointment: true, activity: true },
|
| 73 | orderBy: { moment: "asc" },
|
| 74 | }),
|
| 75 | prisma.medicineRegimen.findMany({
|
| 76 | where: { userId },
|
| 77 | orderBy: { createdAt: "asc" },
|
| 78 | }),
|
| 79 | prisma.insight.findMany({
|
| 80 | where: { userId },
|
| 81 | orderBy: { generatedAt: "asc" },
|
| 82 | }),
|
| 83 | prisma.notification.findMany({
|
| 84 | where: { userId },
|
| 85 | orderBy: { scheduledFor: "asc" },
|
| 86 | }),
|
| 87 | ]);
|
| 88 |
|
| 89 | return {
|
| 90 | user,
|
| 91 | moods,
|
| 92 | moodComponents: moods.flatMap((mood) => mood.moodComponents),
|
| 93 | sleepRecords,
|
| 94 | triggers,
|
| 95 | triggerMoodLinks,
|
| 96 | careActions,
|
| 97 | medicineLogs: careActions.flatMap((action) =>
|
| 98 | action.medicineLog ? [action.medicineLog] : [],
|
| 99 | ),
|
| 100 | appointments: careActions.flatMap((action) =>
|
| 101 | action.appointment ? [action.appointment] : [],
|
| 102 | ),
|
| 103 | activities: careActions.flatMap((action) =>
|
| 104 | action.activity ? [action.activity] : [],
|
| 105 | ),
|
| 106 | medicineRegimens,
|
| 107 | insights,
|
| 108 | notifications,
|
| 109 | };
|
| 110 | }
|