-
-
Notifications
You must be signed in to change notification settings - Fork 740
Expand file tree
/
Copy pathdelete-workout-session.action.ts
More file actions
42 lines (33 loc) · 1.28 KB
/
Copy pathdelete-workout-session.action.ts
File metadata and controls
42 lines (33 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"use server";
import { z } from "zod";
import { prisma } from "@/shared/lib/prisma";
import { authenticatedActionClient } from "@/shared/api/safe-actions";
const deleteWorkoutSessionSchema = z.object({
id: z.string(),
});
export const deleteWorkoutSessionAction = authenticatedActionClient.schema(deleteWorkoutSessionSchema).action(async ({ parsedInput, ctx }) => {
try {
const { id } = parsedInput;
const session = await prisma.workoutSession.findUnique({
where: { id },
select: { userId: true },
});
// Return the same error for missing and non-owned sessions so the endpoint
// cannot be used to disclose which session ids exist.
if (!session || session.userId !== ctx.user.id) {
console.error("❌ Session not found:", id);
return { serverError: "Session not found" };
}
// Supprimer la session (cascade supprimera automatiquement les exercices et sets)
await prisma.workoutSession.delete({
where: { id },
});
if (process.env.NODE_ENV === "development") {
console.log("✅ Workout session deleted successfully:", id);
}
return { success: true };
} catch (error) {
console.error("❌ Error deleting workout session:", error);
return { serverError: "Failed to delete workout session" };
}
});