api: patch a care action with their respective mood and trigger
Parents:
cd24e525 file(s) changed
- api/src/controllers/careActions.ts +26 -1
- api/src/routes/careActions.ts +1 -0
- api/src/schemas/careAction.schema.ts +7 -0
- api/src/schemas/index.ts +2 -0
- api/src/services/careAction.service.ts +13 -1
api/src/controllers/careActions.ts
1 1
import { Response, NextFunction } from 'express';
2 2
import { AuthenticatedRequest } from '@app/middleware/auth';
3 3
import { CareActionType } from '@prisma/client';
4 -
import { CreateCareActionSchema } from '@app/schemas';
4 +
import { CreateCareActionSchema, PatchCareActionSchema } from '@app/schemas';
5 5
import {
6 6
getCareActionsByUserId,
7 7
getCareActionById,
8 8
createCareAction,
9 9
destroyCareActionById,
10 +
patchCareActionById,
10 11
} from '@app/services/careAction.service';
11 12
12 13
export const CareActionsController = {
13 14
index: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
14 15
try {
▸ 50 unchanged lines
65 66
destroy: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
66 67
try {
67 68
await destroyCareActionById(req.userId!, Number(req.params.id));
68 69
69 70
return res.status(200).json({});
71 +
} catch (err) {
72 +
next(err);
73 +
}
74 +
},
75 +
76 +
patch: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
77 +
try {
78 +
const parsed = PatchCareActionSchema.safeParse(req.body.careAction);
79 +
80 +
if (!parsed.success) {
81 +
return res.status(400).json({ errors: parsed.error.issues });
82 +
}
83 +
84 +
const careAction = await patchCareActionById(
85 +
req.userId!,
86 +
Number(req.params.id),
87 +
parsed.data,
88 +
);
89 +
90 +
if (!careAction) {
91 +
return res.status(404).json({ errors: 'Care action was not found' });
92 +
}
93 +
94 +
return res.status(200).json(careAction);
70 95
} catch (err) {
71 96
next(err);
72 97
}
73 98
},
74 99
};
api/src/routes/careActions.ts
1 1
import { Router } from 'express';
2 2
import { CareActionsController } from '@app/controllers/careActions';
3 3
import { requireAuth } from '@app/middleware/auth';
4 4
5 5
const router = Router();
6 6
7 7
router.get('/', requireAuth, CareActionsController.index);
8 8
router.post('/', requireAuth, CareActionsController.create);
9 9
router.get('/:id', requireAuth, CareActionsController.show);
10 +
router.patch('/:id', requireAuth, CareActionsController.patch);
10 11
router.delete('/:id', requireAuth, CareActionsController.destroy);
11 12
12 13
export default router;
api/src/schemas/careAction.schema.ts
1 1
import { z } from 'zod';
2 2
import {
3 3
CareActionType,
4 4
AppointmentType,
5 5
ActivityType,
▸ 34 unchanged lines
40 40
})
41 41
.optional(),
42 42
});
43 43
44 44
export type CreateCareActionInput = z.infer<typeof CreateCareActionSchema>;
45 +
46 +
export const PatchCareActionSchema = z.object({
47 +
triggerId: z.number().int().positive().optional(),
48 +
moodId: z.number().int().positive().optional(),
49 +
}).strict();
50 +
51 +
export type PatchCareActionInput = z.infer<typeof PatchCareActionSchema>;
api/src/schemas/index.ts
1 1
export {
2 2
CreateMoodSchema,
3 3
MoodComponentSchema,
4 4
} from "@app/schemas/mood.schema";
5 5
▸ 42 unchanged lines
48 48
} from "@app/schemas/triggerMoodLink.schema";
49 49
50 50
export {
51 51
CreateCareActionSchema,
52 52
type CreateCareActionInput,
53 +
PatchCareActionSchema,
54 +
type PatchCareActionInput,
53 55
} from "@app/schemas/careAction.schema";
54 56
55 57
export {
56 58
CreateMedicineRegimenSchema,
57 59
type CreateMedicineRegimenInput,
58 60
UpdateMedicineRegimenSchema,
59 61
type UpdateMedicineRegimenInput,
60 62
} from "@app/schemas/medicineRegimen.schema";
api/src/services/careAction.service.ts
1 1
import { prisma } from '@app/lib/prisma';
2 2
import { CareAction, CareActionType } from '@prisma/client';
3 -
import { CreateCareActionInput } from '@app/schemas';
3 +
import { CreateCareActionInput, PatchCareActionInput } from '@app/schemas';
4 4
5 5
const careActionIncludes = {
6 6
medicineLog: { include: { regimen: true } },
7 7
appointment: true,
8 8
activity: true,
▸ 58 unchanged lines
67 67
userId: number,
68 68
id: number,
69 69
): Promise<CareAction> => {
70 70
return prisma.careAction.delete({
71 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,
72 84
});
73 85
};
74 86
75 87
export const createCareAction = async (
76 88
userId: number,
▸ 123 unchanged lines
200 212
moodId: input.moodId,
201 213
},
202 214
include: careActionIncludes,
203 215
});
204 216
};