|
1 | | -"use server"; |
2 | | - |
3 | | -import { z } from "zod"; |
4 | | -import client from "@repo/db/client"; |
5 | | -import { JoinRoomSchema } from "@repo/common/types"; |
6 | | -import { getServerSession } from "next-auth"; |
7 | | -import { authOptions } from "@/utils/auth"; |
8 | | -import { Shape } from "@/types/canvas"; |
9 | | - |
10 | | -export async function getShapes(data: { roomName: string }) { |
11 | | - try { |
12 | | - const validatedRoomName = JoinRoomSchema.parse(data); |
13 | | - |
14 | | - const room = await client.room.findUnique({ |
15 | | - where: { slug: validatedRoomName.roomName }, |
16 | | - }); |
17 | | - |
18 | | - if (!room || !room.id) { |
19 | | - return { success: false, error: "Room not found" }; |
20 | | - } |
21 | | - |
22 | | - const shapesResponse = await client.shape.findMany({ |
23 | | - where: { roomId: room.id }, |
24 | | - }); |
25 | | - |
26 | | - if (!shapesResponse.length) { |
27 | | - return { success: true, shapes: [] }; |
28 | | - } |
29 | | - |
30 | | - const shapes: Shape[] = shapesResponse.map((x) => JSON.parse(x.message)); |
31 | | - |
32 | | - return { success: true, shapes }; |
33 | | - } catch (error) { |
34 | | - if (error instanceof z.ZodError) { |
35 | | - return { success: false, error: "Invalid room code format" }; |
36 | | - } |
37 | | - console.error("Failed to get shapes:", error); |
38 | | - return { success: false, error: "Failed to get shapes" }; |
39 | | - } |
40 | | -} |
41 | | - |
42 | | -export async function clearAllShapes(data: { roomName: string }) { |
43 | | - try { |
44 | | - const session = await getServerSession(authOptions); |
45 | | - |
46 | | - if (!session || !session.user || !session.user.email) { |
47 | | - return { success: false, error: "Authentication required" }; |
48 | | - } |
49 | | - |
50 | | - const userEmail = session.user.email; |
51 | | - const validatedRoomName = JoinRoomSchema.parse(data); |
52 | | - |
53 | | - const room = await client.room.findUnique({ |
54 | | - where: { slug: validatedRoomName.roomName }, |
55 | | - include: { admin: true }, |
56 | | - }); |
57 | | - |
58 | | - if (!room || !room.id) { |
59 | | - return { success: false, error: "Room not found" }; |
60 | | - } |
61 | | - |
62 | | - if (room.admin.email !== userEmail) { |
63 | | - return { |
64 | | - success: false, |
65 | | - error: "Unauthorized: Only the room creator can clear chats", |
66 | | - }; |
67 | | - } |
68 | | - |
69 | | - const result = await client.shape.deleteMany({ |
70 | | - where: { roomId: room.id }, |
71 | | - }); |
72 | | - |
73 | | - return { |
74 | | - success: true, |
75 | | - count: result.count, |
76 | | - }; |
77 | | - } catch (error) { |
78 | | - if (error instanceof z.ZodError) { |
79 | | - return { success: false, error: "Invalid room code format" }; |
80 | | - } |
81 | | - console.error("Failed to clear shapes:", error); |
82 | | - return { success: false, error: "Failed to clear shapes" }; |
83 | | - } |
84 | | -} |
| 1 | +// "use server"; |
| 2 | + |
| 3 | +// import { z } from "zod"; |
| 4 | +// import client from "@repo/db/client"; |
| 5 | +// import { JoinRoomSchema } from "@repo/common/types"; |
| 6 | +// import { getServerSession } from "next-auth"; |
| 7 | +// import { authOptions } from "@/utils/auth"; |
| 8 | +// import { Shape } from "@/types/canvas"; |
| 9 | + |
| 10 | +// export async function getShapes(data: { roomName: string }) { |
| 11 | +// try { |
| 12 | +// const validatedRoomName = JoinRoomSchema.parse(data); |
| 13 | + |
| 14 | +// const room = await client.room.findUnique({ |
| 15 | +// where: { slug: validatedRoomName.roomName }, |
| 16 | +// }); |
| 17 | + |
| 18 | +// if (!room || !room.id) { |
| 19 | +// return { success: false, error: "Room not found" }; |
| 20 | +// } |
| 21 | + |
| 22 | +// const shapesResponse = await client.shape.findMany({ |
| 23 | +// where: { roomId: room.id }, |
| 24 | +// }); |
| 25 | + |
| 26 | +// if (!shapesResponse.length) { |
| 27 | +// return { success: true, shapes: [] }; |
| 28 | +// } |
| 29 | + |
| 30 | +// const shapes: Shape[] = shapesResponse.map((x) => JSON.parse(x.message)); |
| 31 | + |
| 32 | +// return { success: true, shapes }; |
| 33 | +// } catch (error) { |
| 34 | +// if (error instanceof z.ZodError) { |
| 35 | +// return { success: false, error: "Invalid room code format" }; |
| 36 | +// } |
| 37 | +// console.error("Failed to get shapes:", error); |
| 38 | +// return { success: false, error: "Failed to get shapes" }; |
| 39 | +// } |
| 40 | +// } |
| 41 | + |
| 42 | +// export async function clearAllShapes(data: { roomName: string }) { |
| 43 | +// try { |
| 44 | +// const session = await getServerSession(authOptions); |
| 45 | + |
| 46 | +// if (!session || !session.user || !session.user.email) { |
| 47 | +// return { success: false, error: "Authentication required" }; |
| 48 | +// } |
| 49 | + |
| 50 | +// const userEmail = session.user.email; |
| 51 | +// const validatedRoomName = JoinRoomSchema.parse(data); |
| 52 | + |
| 53 | +// const room = await client.room.findUnique({ |
| 54 | +// where: { slug: validatedRoomName.roomName }, |
| 55 | +// include: { admin: true }, |
| 56 | +// }); |
| 57 | + |
| 58 | +// if (!room || !room.id) { |
| 59 | +// return { success: false, error: "Room not found" }; |
| 60 | +// } |
| 61 | + |
| 62 | +// if (room.admin.email !== userEmail) { |
| 63 | +// return { |
| 64 | +// success: false, |
| 65 | +// error: "Unauthorized: Only the room creator can clear chats", |
| 66 | +// }; |
| 67 | +// } |
| 68 | + |
| 69 | +// const result = await client.shape.deleteMany({ |
| 70 | +// where: { roomId: room.id }, |
| 71 | +// }); |
| 72 | + |
| 73 | +// return { |
| 74 | +// success: true, |
| 75 | +// count: result.count, |
| 76 | +// }; |
| 77 | +// } catch (error) { |
| 78 | +// if (error instanceof z.ZodError) { |
| 79 | +// return { success: false, error: "Invalid room code format" }; |
| 80 | +// } |
| 81 | +// console.error("Failed to clear shapes:", error); |
| 82 | +// return { success: false, error: "Failed to clear shapes" }; |
| 83 | +// } |
| 84 | +// } |
0 commit comments