eletrotupi / tcc/ api/src/services/careAction.service.tsfa18535ea443601fc1f62853fabb636e0930bcb5
5.1 KB | 216 lines Raw
1
import { prisma } from '@app/lib/prisma';
2
import { CareAction, CareActionType } from '@prisma/client';
3
import { CreateCareActionInput, PatchCareActionInput } from '@app/schemas';
4

5
const careActionIncludes = {
6
  medicineLog: { include: { regimen: true } },
7
  appointment: true,
8
  activity: true,
9
} as const;
10

11
export const getCareActionsByUserId = async (
12
  userId: number,
13
  params?: {
14
    type?: CareActionType;
15
    from?: string;
16
    to?: string;
17
    page?: number;
18
    limit?: number;
19
  },
20
) => {
21
  const page = params?.page ?? 1;
22
  const limit = params?.limit ?? 20;
23
  const skip = (page - 1) * limit;
24

25
  const where = {
26
    userId,
27
    ...(params?.type ? { type: params.type } : {}),
28
    ...(params?.from || params?.to
29
      ? {
30
          moment: {
31
            ...(params?.from && { gte: new Date(params.from) }),
32
            ...(params?.to && { lte: new Date(params.to) }),
33
          },
34
        }
35
      : {}),
36
  };
37

38
  const [entries, total] = await Promise.all([
39
    prisma.careAction.findMany({
40
      where,
41
      include: careActionIncludes,
42
      orderBy: { moment: 'desc' },
43
      take: limit,
44
      skip,
45
    }),
46
    prisma.careAction.count({ where }),
47
  ]);
48

49
  const totalPages = Math.ceil(total / limit);
50

51
  return {
52
    entries,
53
    total,
54
    page,
55
    nextPage: page < totalPages ? page + 1 : null,
56
  };
57
};
58

59
export const getCareActionById = async (userId: number, id: number) => {
60
  return prisma.careAction.findUnique({
61
    where: { id, userId },
62
    include: careActionIncludes,
63
  });
64
};
65

66
export const destroyCareActionById = async (
67
  userId: number,
68
  id: number,
69
): Promise<CareAction> => {
70
  return prisma.careAction.delete({
71
    where: { id, userId },
72
  });
73
};
74

75
export const patchCareActionById = async (
76
  userId: number,
77
  id: number,
78
  data: PatchCareActionInput,
79
) => {
80
  return prisma.careAction.update({
81
    where: { id, userId },
82
    data,
83
    include: careActionIncludes,
84
  });
85
};
86

87
export const createCareAction = async (
88
  userId: number,
89
  input: CreateCareActionInput,
90
) => {
91
  if (input.type === CareActionType.MEDICINE) {
92
    if (input.regimenId) {
93
      const regimenId = input.regimenId;
94
      const [careAction] = await prisma.$transaction([
95
        prisma.careAction.create({
96
          data: {
97
            userId,
98
            type: input.type,
99
            moment: input.moment,
100
            triggerId: input.triggerId,
101
            moodId: input.moodId,
102
            medicineLog: {
103
              create: { regimenId },
104
            },
105
          },
106
          include: careActionIncludes,
107
        }),
108
      ]);
109
      return careAction;
110
    }
111

112
    if (input.medicine) {
113
      const med = input.medicine;
114
      const [careAction] = await prisma.$transaction([
115
        prisma.careAction.create({
116
          data: {
117
            userId,
118
            type: input.type,
119
            moment: input.moment,
120
            triggerId: input.triggerId,
121
            moodId: input.moodId,
122
            medicineLog: {
123
              create: {
124
                regimen: {
125
                  create: {
126
                    name: med.name,
127
                    dosage: med.dosage,
128
                    periodicity: med.periodicity,
129
                    scheduledAt: med.scheduledAt,
130
                    userId,
131
                  },
132
                },
133
              },
134
            },
135
          },
136
          include: careActionIncludes,
137
        }),
138
      ]);
139
      return careAction;
140
    }
141

142
    // MEDICINE with no regimen info: create action without log
143
    const [careAction] = await prisma.$transaction([
144
      prisma.careAction.create({
145
        data: {
146
          userId,
147
          type: input.type,
148
          moment: input.moment,
149
          triggerId: input.triggerId,
150
          moodId: input.moodId,
151
        },
152
        include: careActionIncludes,
153
      }),
154
    ]);
155
    return careAction;
156
  }
157

158
  if (input.type === CareActionType.APPOINTMENT && input.appointment) {
159
    const appt = input.appointment;
160
    const [careAction] = await prisma.$transaction([
161
      prisma.careAction.create({
162
        data: {
163
          userId,
164
          type: input.type,
165
          moment: input.moment,
166
          triggerId: input.triggerId,
167
          moodId: input.moodId,
168
          appointment: {
169
            create: {
170
              type: appt.type,
171
              duration: appt.duration,
172
              note: appt.note,
173
            },
174
          },
175
        },
176
        include: careActionIncludes,
177
      }),
178
    ]);
179
    return careAction;
180
  }
181

182
  if (input.type === CareActionType.ACTIVITY && input.activity) {
183
    const act = input.activity;
184
    const [careAction] = await prisma.$transaction([
185
      prisma.careAction.create({
186
        data: {
187
          userId,
188
          type: input.type,
189
          moment: input.moment,
190
          triggerId: input.triggerId,
191
          moodId: input.moodId,
192
          activity: {
193
            create: {
194
              type: act.type,
195
              duration: act.duration,
196
            },
197
          },
198
        },
199
        include: careActionIncludes,
200
      }),
201
    ]);
202
    return careAction;
203
  }
204

205
  // Fallback: create without subtype
206
  return prisma.careAction.create({
207
    data: {
208
      userId,
209
      type: input.type,
210
      moment: input.moment,
211
      triggerId: input.triggerId,
212
      moodId: input.moodId,
213
    },
214
    include: careActionIncludes,
215
  });
216
};