eletrotupi / tcc/ api/src/services/careAction.service.tsfe4f9989edbeafd44d768c2cccbaa35c1af09360
5.3 KB | 227 lines Raw
1
import { prisma } from '@app/lib/prisma';
2
import { paginate } from '@app/utils/calc';
3
import { CareAction, CareActionType } from '@prisma/client';
4
import { CreateCareActionInput, PatchCareActionInput } from '@app/schemas';
5
import { syncMedicineReminderJobs } from '@app/services/medicineReminder.sync';
6

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

13
export const getCareActionsByUserId = async (
14
  userId: number,
15
  params?: {
16
    type?: CareActionType;
17
    from?: string;
18
    to?: string;
19
    page?: number;
20
    limit?: number;
21
  },
22
) => {
23
  const limit = params?.limit ?? 20;
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 total = await prisma.careAction.count({ where });
39
  const { page, skip, nextPage } = paginate(params?.page ?? 1, limit, total);
40

41
  const entries = await prisma.careAction.findMany({
42
    where,
43
    include: careActionIncludes,
44
    orderBy: { moment: 'desc' },
45
    take: limit,
46
    skip,
47
  });
48

49
  return {
50
    entries,
51
    total,
52
    page,
53
    nextPage,
54
  };
55
};
56

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

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

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

85
export const createCareAction = async (
86
  userId: number,
87
  input: CreateCareActionInput,
88
) => {
89
  if (input.type === CareActionType.MEDICINE) {
90
    if (input.regimenId) {
91
      const regimenId = input.regimenId;
92

93
      const [careAction] = await prisma.$transaction([
94
        prisma.careAction.create({
95
          data: {
96
            userId,
97
            type: input.type,
98
            moment: input.moment,
99
            triggerId: input.triggerId,
100
            moodId: input.moodId,
101
            medicineLog: {
102
              create: { regimenId },
103
            },
104
          },
105
          include: careActionIncludes,
106
        }),
107
      ]);
108

109
      return careAction;
110
    }
111

112
    if (input.medicine) {
113
      const med = input.medicine;
114

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

141
      if (careAction.medicineLog?.regimen) {
142
        await syncMedicineReminderJobs(careAction.medicineLog.regimen as any);
143
      }
144

145
      return careAction;
146
    }
147

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

162
    return careAction;
163
  }
164

165
  if (input.type === CareActionType.APPOINTMENT && input.appointment) {
166
    const appt = input.appointment;
167

168
    const [careAction] = await prisma.$transaction([
169
      prisma.careAction.create({
170
        data: {
171
          userId,
172
          type: input.type,
173
          moment: input.moment,
174
          triggerId: input.triggerId,
175
          moodId: input.moodId,
176
          appointment: {
177
            create: {
178
              type: appt.type,
179
              duration: appt.duration,
180
              note: appt.note,
181
            },
182
          },
183
        },
184
        include: careActionIncludes,
185
      }),
186
    ]);
187

188
    return careAction;
189
  }
190

191
  if (input.type === CareActionType.ACTIVITY && input.activity) {
192
    const act = input.activity;
193

194
    const [careAction] = await prisma.$transaction([
195
      prisma.careAction.create({
196
        data: {
197
          userId,
198
          type: input.type,
199
          moment: input.moment,
200
          triggerId: input.triggerId,
201
          moodId: input.moodId,
202
          activity: {
203
            create: {
204
              type: act.type,
205
              duration: act.duration,
206
            },
207
          },
208
        },
209
        include: careActionIncludes,
210
      }),
211
    ]);
212

213
    return careAction;
214
  }
215

216
  // Fallback: create without subtype
217
  return prisma.careAction.create({
218
    data: {
219
      userId,
220
      type: input.type,
221
      moment: input.moment,
222
      triggerId: input.triggerId,
223
      moodId: input.moodId,
224
    },
225
    include: careActionIncludes,
226
  });
227
};