| 1 | import { prisma } from '@app/lib/prisma';
|
| 2 | import { CareAction, CareActionType } from '@prisma/client';
|
| 3 | import { CreateCareActionInput } 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 createCareAction = async (
|
| 76 | userId: number,
|
| 77 | input: CreateCareActionInput,
|
| 78 | ) => {
|
| 79 | if (input.type === CareActionType.MEDICINE) {
|
| 80 | if (input.regimenId) {
|
| 81 | const regimenId = input.regimenId;
|
| 82 | const [careAction] = await prisma.$transaction([
|
| 83 | prisma.careAction.create({
|
| 84 | data: {
|
| 85 | userId,
|
| 86 | type: input.type,
|
| 87 | moment: input.moment,
|
| 88 | triggerId: input.triggerId,
|
| 89 | moodId: input.moodId,
|
| 90 | medicineLog: {
|
| 91 | create: { regimenId },
|
| 92 | },
|
| 93 | },
|
| 94 | include: careActionIncludes,
|
| 95 | }),
|
| 96 | ]);
|
| 97 | return careAction;
|
| 98 | }
|
| 99 |
|
| 100 | if (input.medicine) {
|
| 101 | const med = input.medicine;
|
| 102 | const [careAction] = await prisma.$transaction([
|
| 103 | prisma.careAction.create({
|
| 104 | data: {
|
| 105 | userId,
|
| 106 | type: input.type,
|
| 107 | moment: input.moment,
|
| 108 | triggerId: input.triggerId,
|
| 109 | moodId: input.moodId,
|
| 110 | medicineLog: {
|
| 111 | create: {
|
| 112 | regimen: {
|
| 113 | create: {
|
| 114 | name: med.name,
|
| 115 | dosage: med.dosage,
|
| 116 | periodicity: med.periodicity,
|
| 117 | scheduledAt: med.scheduledAt,
|
| 118 | userId,
|
| 119 | },
|
| 120 | },
|
| 121 | },
|
| 122 | },
|
| 123 | },
|
| 124 | include: careActionIncludes,
|
| 125 | }),
|
| 126 | ]);
|
| 127 | return careAction;
|
| 128 | }
|
| 129 |
|
| 130 | // MEDICINE with no regimen info: create action without log
|
| 131 | const [careAction] = await prisma.$transaction([
|
| 132 | prisma.careAction.create({
|
| 133 | data: {
|
| 134 | userId,
|
| 135 | type: input.type,
|
| 136 | moment: input.moment,
|
| 137 | triggerId: input.triggerId,
|
| 138 | moodId: input.moodId,
|
| 139 | },
|
| 140 | include: careActionIncludes,
|
| 141 | }),
|
| 142 | ]);
|
| 143 | return careAction;
|
| 144 | }
|
| 145 |
|
| 146 | if (input.type === CareActionType.APPOINTMENT && input.appointment) {
|
| 147 | const appt = input.appointment;
|
| 148 | const [careAction] = await prisma.$transaction([
|
| 149 | prisma.careAction.create({
|
| 150 | data: {
|
| 151 | userId,
|
| 152 | type: input.type,
|
| 153 | moment: input.moment,
|
| 154 | triggerId: input.triggerId,
|
| 155 | moodId: input.moodId,
|
| 156 | appointment: {
|
| 157 | create: {
|
| 158 | type: appt.type,
|
| 159 | duration: appt.duration,
|
| 160 | note: appt.note,
|
| 161 | },
|
| 162 | },
|
| 163 | },
|
| 164 | include: careActionIncludes,
|
| 165 | }),
|
| 166 | ]);
|
| 167 | return careAction;
|
| 168 | }
|
| 169 |
|
| 170 | if (input.type === CareActionType.ACTIVITY && input.activity) {
|
| 171 | const act = input.activity;
|
| 172 | const [careAction] = await prisma.$transaction([
|
| 173 | prisma.careAction.create({
|
| 174 | data: {
|
| 175 | userId,
|
| 176 | type: input.type,
|
| 177 | moment: input.moment,
|
| 178 | triggerId: input.triggerId,
|
| 179 | moodId: input.moodId,
|
| 180 | activity: {
|
| 181 | create: {
|
| 182 | type: act.type,
|
| 183 | duration: act.duration,
|
| 184 | },
|
| 185 | },
|
| 186 | },
|
| 187 | include: careActionIncludes,
|
| 188 | }),
|
| 189 | ]);
|
| 190 | return careAction;
|
| 191 | }
|
| 192 |
|
| 193 | // Fallback: create without subtype
|
| 194 | return prisma.careAction.create({
|
| 195 | data: {
|
| 196 | userId,
|
| 197 | type: input.type,
|
| 198 | moment: input.moment,
|
| 199 | triggerId: input.triggerId,
|
| 200 | moodId: input.moodId,
|
| 201 | },
|
| 202 | include: careActionIncludes,
|
| 203 | });
|
| 204 | };
|