Skip to content

Commit d325118

Browse files
authored
Merge branch 'webdevcody:main' into bugfix-randomsnippet
2 parents 7e18d1c + 801b2f6 commit d325118

File tree

8 files changed

+293
-223
lines changed

8 files changed

+293
-223
lines changed

prisma/schema.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ model User {
3030
results Result[]
3131
achievements UserAchievement[]
3232
33+
avarageAccuracy Decimal @default(0) @db.Decimal(5, 2)
34+
avarageCpm Decimal @default(0) @db.Decimal(6, 2)
35+
3336
role UserRole @default(USER)
3437
3538
snippets Snippet[]

src/app/_actions/result.ts

Lines changed: 31 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -3,213 +3,58 @@
33
import { revalidatePath } from "next/cache";
44
import { z } from "zod";
55
import { action } from "@/lib/actions";
6+
import { Prisma } from "@prisma/client";
67

78
// when snippets rating hits this number
89
// it will no longer be on the race
910
// and will be reviewed by admin on the review page
1011

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+
}
4026

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: {
5330
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,
5636
},
5737
});
5838

59-
await tx.snippet.update({
39+
const avgValues = await tx.result.aggregate({
6040
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: {
9841
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",
11442
},
115-
create: {
116-
userId: user.id,
117-
snippetId,
118-
type: "DOWN",
43+
_avg: {
44+
accuracy: true,
45+
cpm: true,
11946
},
12047
});
12148

122-
const updatedSnippet = await tx.snippet.update({
49+
await tx.user.update({
12350
where: {
124-
id: snippetId,
51+
id: user.id,
12552
},
12653
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,
13156
},
13257
});
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-
}
14458
});
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");
21459
},
21560
);

0 commit comments

Comments
 (0)