|
3 | 3 | import { revalidatePath } from "next/cache";
|
4 | 4 | import { z } from "zod";
|
5 | 5 | import { action } from "@/lib/actions";
|
| 6 | +import { Prisma } from "@prisma/client"; |
6 | 7 |
|
7 | 8 | // when snippets rating hits this number
|
8 | 9 | // it will no longer be on the race
|
9 | 10 | // and will be reviewed by admin on the review page
|
10 | 11 |
|
11 |
| -const snippetVoteSchema = z.object({ |
12 |
| - snippetId: z.string(), |
13 |
| -}); |
14 |
| - |
15 |
| -const SNIPPET_RATING_THRESHOLD = -10; |
16 |
| - |
17 |
| -export const upvoteSnippetAction = action( |
18 |
| - snippetVoteSchema, |
19 |
| - async ({ snippetId }, { prisma, user }) => { |
20 |
| - if (!user) throw new Error("You must be logged in to vote."); |
21 |
| - |
22 |
| - const snippet = await prisma.snippet.findUnique({ |
23 |
| - where: { |
24 |
| - id: snippetId, |
25 |
| - }, |
26 |
| - }); |
27 |
| - |
28 |
| - if (!snippet) throw new Error("Snippet not found."); |
29 |
| - |
30 |
| - if (snippet.onReview) throw new Error("Snippet is already on review."); |
31 |
| - |
32 |
| - const previousVote = await prisma.snippetVote.findUnique({ |
33 |
| - where: { |
34 |
| - userId_snippetId: { |
35 |
| - userId: user.id, |
36 |
| - snippetId, |
37 |
| - }, |
38 |
| - }, |
39 |
| - }); |
| 12 | +export const saveUserResultAction = action( |
| 13 | + z.object({ |
| 14 | + timeTaken: z.union([z.string(), z.number()]), |
| 15 | + errors: z.number().nullable(), |
| 16 | + cpm: z.number().min(0).max(9999, { |
| 17 | + message: "Cpm is too high. Please, turn off the python bot.", |
| 18 | + }), |
| 19 | + accuracy: z.number().min(0).max(100), |
| 20 | + snippetId: z.string(), |
| 21 | + }), |
| 22 | + async (input, { prisma, user }) => { |
| 23 | + if (!user) { |
| 24 | + throw new Error("Not allowed."); |
| 25 | + } |
40 | 26 |
|
41 |
| - await prisma.$transaction(async (tx) => { |
42 |
| - await tx.snippetVote.upsert({ |
43 |
| - where: { |
44 |
| - userId_snippetId: { |
45 |
| - userId: user.id, |
46 |
| - snippetId, |
47 |
| - }, |
48 |
| - }, |
49 |
| - update: { |
50 |
| - type: "UP", |
51 |
| - }, |
52 |
| - create: { |
| 27 | + prisma.$transaction(async (tx) => { |
| 28 | + await tx.result.create({ |
| 29 | + data: { |
53 | 30 | userId: user.id,
|
54 |
| - snippetId, |
55 |
| - type: "UP", |
| 31 | + takenTime: input.timeTaken.toString(), |
| 32 | + errorCount: input.errors, |
| 33 | + cpm: input.cpm, |
| 34 | + accuracy: new Prisma.Decimal(input.accuracy), |
| 35 | + snippetId: input.snippetId, |
56 | 36 | },
|
57 | 37 | });
|
58 | 38 |
|
59 |
| - await tx.snippet.update({ |
| 39 | + const avgValues = await tx.result.aggregate({ |
60 | 40 | where: {
|
61 |
| - id: snippetId, |
62 |
| - }, |
63 |
| - data: { |
64 |
| - rating: { |
65 |
| - // if user downvoted before, decrement by 2 |
66 |
| - increment: 1 + Number(!!previousVote), |
67 |
| - }, |
68 |
| - }, |
69 |
| - }); |
70 |
| - }); |
71 |
| - |
72 |
| - revalidatePath("/result"); |
73 |
| - }, |
74 |
| -); |
75 |
| - |
76 |
| -export const downVoteSnippetAction = action( |
77 |
| - snippetVoteSchema, |
78 |
| - async ({ snippetId }, { prisma, user }) => { |
79 |
| - if (!user) throw new Error("You must be logged in to vote."); |
80 |
| - |
81 |
| - const snippet = await prisma.snippet.findUnique({ |
82 |
| - where: { |
83 |
| - id: snippetId, |
84 |
| - }, |
85 |
| - }); |
86 |
| - |
87 |
| - if (!snippet) { |
88 |
| - throw new Error("Snippet not found."); |
89 |
| - } |
90 |
| - |
91 |
| - if (snippet.onReview) { |
92 |
| - throw new Error("Snippet is already on review."); |
93 |
| - } |
94 |
| - |
95 |
| - const previousVote = await prisma.snippetVote.findUnique({ |
96 |
| - where: { |
97 |
| - userId_snippetId: { |
98 | 41 | userId: user.id,
|
99 |
| - snippetId, |
100 |
| - }, |
101 |
| - }, |
102 |
| - }); |
103 |
| - |
104 |
| - await prisma.$transaction(async (tx) => { |
105 |
| - await tx.snippetVote.upsert({ |
106 |
| - where: { |
107 |
| - userId_snippetId: { |
108 |
| - userId: user.id, |
109 |
| - snippetId, |
110 |
| - }, |
111 |
| - }, |
112 |
| - update: { |
113 |
| - type: "DOWN", |
114 | 42 | },
|
115 |
| - create: { |
116 |
| - userId: user.id, |
117 |
| - snippetId, |
118 |
| - type: "DOWN", |
| 43 | + _avg: { |
| 44 | + accuracy: true, |
| 45 | + cpm: true, |
119 | 46 | },
|
120 | 47 | });
|
121 | 48 |
|
122 |
| - const updatedSnippet = await tx.snippet.update({ |
| 49 | + await tx.user.update({ |
123 | 50 | where: {
|
124 |
| - id: snippetId, |
| 51 | + id: user.id, |
125 | 52 | },
|
126 | 53 | data: {
|
127 |
| - rating: { |
128 |
| - // if user upvoted before, decrement by 2 |
129 |
| - decrement: 1 + Number(!!previousVote), |
130 |
| - }, |
| 54 | + avarageAccuracy: avgValues._avg.accuracy ?? 0, |
| 55 | + avarageCpm: avgValues._avg.cpm ?? 0, |
131 | 56 | },
|
132 | 57 | });
|
133 |
| - |
134 |
| - if (updatedSnippet.rating === SNIPPET_RATING_THRESHOLD) { |
135 |
| - await tx.snippet.update({ |
136 |
| - where: { |
137 |
| - id: snippetId, |
138 |
| - }, |
139 |
| - data: { |
140 |
| - onReview: true, |
141 |
| - }, |
142 |
| - }); |
143 |
| - } |
144 | 58 | });
|
145 |
| - |
146 |
| - revalidatePath("/result"); |
147 |
| - }, |
148 |
| -); |
149 |
| - |
150 |
| -export const deleteVoteAction = action( |
151 |
| - snippetVoteSchema, |
152 |
| - async ({ snippetId }, { prisma, user }) => { |
153 |
| - if (!user) throw new Error("You must be logged in to vote."); |
154 |
| - |
155 |
| - const snippet = await prisma.snippet.findUnique({ |
156 |
| - where: { |
157 |
| - id: snippetId, |
158 |
| - }, |
159 |
| - }); |
160 |
| - |
161 |
| - if (!snippet) { |
162 |
| - throw new Error("Snippet doesnt exist."); |
163 |
| - } |
164 |
| - |
165 |
| - const previousVote = await prisma.snippetVote.findUnique({ |
166 |
| - where: { |
167 |
| - userId_snippetId: { |
168 |
| - userId: user.id, |
169 |
| - snippetId, |
170 |
| - }, |
171 |
| - }, |
172 |
| - }); |
173 |
| - |
174 |
| - if (!previousVote) { |
175 |
| - throw new Error("Something went wrong..."); |
176 |
| - } |
177 |
| - |
178 |
| - await prisma.$transaction(async (tx) => { |
179 |
| - await tx.snippetVote.delete({ |
180 |
| - where: { |
181 |
| - userId_snippetId: { |
182 |
| - userId: user.id, |
183 |
| - snippetId, |
184 |
| - }, |
185 |
| - }, |
186 |
| - }); |
187 |
| - |
188 |
| - if (previousVote!.type === "DOWN") { |
189 |
| - await tx.snippet.update({ |
190 |
| - where: { |
191 |
| - id: snippetId, |
192 |
| - }, |
193 |
| - data: { |
194 |
| - rating: { |
195 |
| - increment: 1, |
196 |
| - }, |
197 |
| - }, |
198 |
| - }); |
199 |
| - } else { |
200 |
| - await tx.snippet.update({ |
201 |
| - where: { |
202 |
| - id: snippetId, |
203 |
| - }, |
204 |
| - data: { |
205 |
| - rating: { |
206 |
| - decrement: 1, |
207 |
| - }, |
208 |
| - }, |
209 |
| - }); |
210 |
| - } |
211 |
| - }); |
212 |
| - |
213 |
| - revalidatePath("/result"); |
214 | 59 | },
|
215 | 60 | );
|
0 commit comments