eletrotupi / tcc/ frontend/lib/history/mappers.ts2bd9b6733f955a443d284beecfa664749fcb3900
4.6 KB | 164 lines Raw
1
import type { HistoryCard } from "@/lib/history/types";
2
import type { MoodEntry, SleepRecord, Trigger, CareAction } from "@/lib/api";
3
import { getMood } from "@/constants/moods";
4

5

6
export function mapMoodToHistoryCard(mood: MoodEntry): HistoryCard {
7
  const moodLabel = getMood(mood.selectedMood);
8
  const components = mood.moodComponents
9
    .map((c) => c.component.charAt(0) + c.component.slice(1).toLowerCase())
10
    .join(", ");
11

12
  const summary = [components || null, mood.annotation || null]
13
    .filter(Boolean)
14
    .join(" · ");
15

16
  return {
17
    id: `mood-${mood.id}`,
18
    category: "mood",
19
    timestamp: mood.moment.toString(),
20
    title: moodLabel?.label ?? "Humor",
21
    summary: summary || "Sem detalhes",
22
    badge: undefined,
23
    raw: mood,
24
  };
25
}
26

27
// Sleep Records
28

29
function sleepQualityBadge(hours: number): HistoryCard["badge"] {
30
  if (hours >= 7) return { label: "Excelente", variant: "green" };
31
  if (hours >= 5) return { label: "Regular", variant: "yellow" };
32

33
  return {
34
    label: "Ruim",
35
    variant: "red",
36
  };
37
}
38

39
export function mapSleepToHistoryCard(record: SleepRecord): HistoryCard {
40
  console.log("mapping...", record)
41
  const hours = Math.floor(record.average);
42
  const minutes = Math.round((record.average - hours) * 60);
43
  const duration = `${hours}h ${minutes.toString().padStart(2, "0")}min`;
44
  const timestamp = typeof record.createdAt === 'string'
45
    ? record.createdAt
46
    : record.createdAt.toISOString();
47

48
  return {
49
    id: `sleep-${record.id}`,
50
    category: "sleep",
51
    timestamp,
52
    title: "Sono",
53
    summary: [duration, record.annotations].filter(Boolean).join(" · "),
54
    badge: sleepQualityBadge(record.average),
55
    raw: record,
56
  };
57
}
58

59
export function mapTriggerToHistoryCard(record: Trigger): HistoryCard {
60
  return {
61
    id: `trigger-${record.id}`,
62
    category: "trigger",
63
    timestamp: record.moment.toString(),
64
    title: "Gatilho",
65
    summary: [record.category, record.comment].filter(Boolean).join(" · "),
66
    badge: undefined,
67
    raw: record,
68
  };
69
}
70

71
function activityLabel(type?: string): string {
72
  const labels: Record<string, string> = {
73
    WALK: "Caminhada",
74
    YOGA: "Yoga",
75
    GYM: "Academia",
76
    MEDITATION: "Meditação",
77
    SOCIAL: "Momento social",
78
    CREATIVE: "Atividade criativa",
79
    OTHER: "Atividade",
80
  };
81
  return type ? (labels[type] ?? "Atividade") : "Atividade";
82
}
83

84
function appointmentLabel(type?: string): string {
85
  const labels: Record<string, string> = {
86
    ANALYST: "Analista",
87
    PSYCHIATRIST: "Psiquiatra",
88
    GP: "Clínico geral",
89
    NUTRITIONIST: "Nutricionista",
90
    PHYSIOTHERAPIST: "Fisioterapeuta",
91
    OTHER: "Consulta",
92
  };
93
  return type ? (labels[type] ?? "Consulta") : "Consulta";
94
}
95

96
export function mapCareActionToHistoryCard(record: CareAction): HistoryCard {
97
  switch (record.type) {
98
    case "MEDICINE": {
99
      const log = record.medicineLog;
100
      const name = log?.regimen?.name ?? "Medicamento";
101
      const dosage = log?.regimen?.dosage;
102
      return {
103
        id: `care_action-${record.id}`,
104
        category: "care_action",
105
        subtype: "medicine",
106
        timestamp: record.moment.toString(),
107
        title: name,
108
        summary: dosage ?? "Sem dosagem registrada",
109
        raw: record,
110
      };
111
    }
112

113
    case "APPOINTMENT": {
114
      const appt = record.appointment;
115
      const parts = [
116
        appt?.duration ? `${appt.duration} min` : null,
117
        appt?.note ?? null,
118
      ].filter(Boolean);
119
      const linkedCount =
120
        (record.triggerId ? 1 : 0) + (record.moodId ? 1 : 0);
121
      return {
122
        id: `care_action-${record.id}`,
123
        category: "care_action",
124
        subtype: "appointment",
125
        timestamp: record.moment.toString(),
126
        title: `Consulta · ${appointmentLabel(appt?.type)}`,
127
        summary: parts.join(" · ") || "Sem anotações",
128
        badge:
129
          linkedCount > 0
130
            ? {
131
                label: `${linkedCount} vínculo${linkedCount > 1 ? "s" : ""}`,
132
                variant: "blue",
133
              }
134
            : undefined,
135
        raw: record,
136
      };
137
    }
138

139
    case "ACTIVITY": {
140
      const act = record.activity;
141
      return {
142
        id: `care_action-${record.id}`,
143
        category: "care_action",
144
        subtype: "activity",
145
        timestamp: record.moment.toString(),
146
        title: activityLabel(act?.type),
147
        summary: act?.duration
148
          ? `${act.duration} min`
149
          : "Sem duração registrada",
150
        raw: record,
151
      };
152
    }
153

154
    default:
155
      return {
156
        id: `care_action-${record.id}`,
157
        category: "care_action",
158
        timestamp: record.moment.toString(),
159
        title: "Ação de cuidado",
160
        summary: "",
161
        raw: record,
162
      };
163
  }
164
}