返回 presentation-ai
presentationFavoriteActions.ts
根目录 / src / app / _actions / notebook / presentation / presentationFavoriteActions.ts
1 "use server";
2
3 import { auth } from "@/server/auth";
4 import { db } from "@/server/db";
5 import { DocumentType } from "@/prisma/client";
6
7 type PresentationFavoriteResult = {
8 success: boolean;
9 message: string;
10 isFavorite?: boolean;
11 };
12
13 async function canFavoritePresentation(documentId: string, userId: string) {
14 const document = await db.baseDocument.findUnique({
15 where: { id: documentId },
16 select: {
17 isPublic: true,
18 type: true,
19 userId: true,
20 },
21 });
22
23 return (
24 document?.type === DocumentType.PRESENTATION &&
25 (document.isPublic || document.userId === userId)
26 );
27 }
28
29 export async function addPresentationToFavorites(
30 documentId: string,
31 ): Promise<PresentationFavoriteResult> {
32 const session = await auth();
33 if (!session?.user) {
34 return { success: false, message: "Unauthorized", isFavorite: false };
35 }
36
37 const canFavorite = await canFavoritePresentation(
38 documentId,
39 session.user.id,
40 );
41 if (!canFavorite) {
42 return {
43 success: false,
44 message: "Presentation not found",
45 isFavorite: false,
46 };
47 }
48
49 await db.favoriteDocument.upsert({
50 where: {
51 userId_documentId: {
52 userId: session.user.id,
53 documentId,
54 },
55 },
56 update: {},
57 create: {
58 userId: session.user.id,
59 documentId,
60 },
61 });
62
63 return {
64 success: true,
65 message: "Presentation added to favorites",
66 isFavorite: true,
67 };
68 }
69
70 export async function removePresentationFromFavorites(
71 documentId: string,
72 ): Promise<PresentationFavoriteResult> {
73 const session = await auth();
74 if (!session?.user) {
75 return { success: false, message: "Unauthorized", isFavorite: false };
76 }
77
78 const canFavorite = await canFavoritePresentation(
79 documentId,
80 session.user.id,
81 );
82 if (!canFavorite) {
83 return {
84 success: false,
85 message: "Presentation not found",
86 isFavorite: false,
87 };
88 }
89
90 await db.favoriteDocument.deleteMany({
91 where: {
92 userId: session.user.id,
93 documentId,
94 },
95 });
96
97 return {
98 success: true,
99 message: "Presentation removed from favorites",
100 isFavorite: false,
101 };
102 }
103
104 export async function togglePresentationFavorite(
105 documentId: string,
106 ): Promise<PresentationFavoriteResult> {
107 const session = await auth();
108 if (!session?.user) {
109 return { success: false, message: "Unauthorized", isFavorite: false };
110 }
111
112 const canFavorite = await canFavoritePresentation(
113 documentId,
114 session.user.id,
115 );
116 if (!canFavorite) {
117 return {
118 success: false,
119 message: "Presentation not found",
120 isFavorite: false,
121 };
122 }
123
124 const favorite = await db.favoriteDocument.findUnique({
125 where: {
126 userId_documentId: {
127 userId: session.user.id,
128 documentId,
129 },
130 },
131 select: { id: true },
132 });
133
134 if (favorite) {
135 await db.favoriteDocument.delete({
136 where: { id: favorite.id },
137 });
138
139 return {
140 success: true,
141 message: "Presentation removed from favorites",
142 isFavorite: false,
143 };
144 }
145
146 await db.favoriteDocument.create({
147 data: {
148 userId: session.user.id,
149 documentId,
150 },
151 });
152
153 return {
154 success: true,
155 message: "Presentation added to favorites",
156 isFavorite: true,
157 };
158 }
159
159 lines TYPESCRIPT