-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-progress.ts
156 lines (118 loc) · 3.7 KB
/
user-progress.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"use server"
import { POINTS_TO_REFILL } from "@/constants"
import db from "@/db/drizzle"
import { getCourseById, getUserProgress, getUserSubscription } from "@/db/queries"
import { challengeProgress, challenges, userProgress } from "@/db/schema"
import { auth, currentUser } from "@clerk/nextjs"
import { and, eq } from "drizzle-orm"
import { revalidatePath } from "next/cache"
import { redirect } from "next/navigation"
export const upsertUserProgress = async (courseId: number) => {
const { userId } = await auth()
const user = await currentUser()
if(!userId || !user)
{
throw new Error("Unauthorized")
}
const course = await getCourseById(courseId)
if(!course)
{
throw new Error("Course not found")
}
if(!course.units.length || !course.units[0].lessons.length)
{
throw new Error("Course is empty")
}
const existingUserProgress = await getUserProgress()
if(existingUserProgress)
{
await db.update(userProgress).set({
activeCourseId: courseId,
userName: user.firstName || "User",
userImageSrc: user.imageUrl || "/mascot.svg",
})
revalidatePath("/courses");
revalidatePath("/learn");
redirect("/learn");
}
await db.insert(userProgress).values({
userId,
activeCourseId: courseId,
userName: user.firstName || "User",
userImageSrc: user.imageUrl || "/mascot.svg",
})
revalidatePath("/courses");
revalidatePath("/learn");
redirect("/learn");
}
export const reduceHearts = async (challengeId: number) => {
const {userId} = await auth()
if(!userId)
{
throw new Error("Unauthorized")
}
const currentUserProgress = await getUserProgress()
const userSubscription = await getUserSubscription()
const challenge = await db.query.challenges.findFirst({
where: eq(challenges.id, challengeId)
})
if(!challenge)
{
throw new Error("Challenge not found")
}
const lessonId = challenge.lessonId
const existingChallengeProgress = await db.query.challengeProgress.findFirst({
where: and(
eq(challengeProgress.userId, userId),
eq(challengeProgress.challengeId, challengeId),
)
})
const isPractice = !!existingChallengeProgress
if(isPractice)
{
return { error: "practice" } //normal api response
}
if(!currentUserProgress)
{
throw new Error("User progree not found") //critical error
}
if(userSubscription?.isActive)
{
return { error: "Subscription" }
}
if(currentUserProgress.hearts===0)
{
return { error: "hearts" }
}
await db.update(userProgress).set({
hearts: Math.max(currentUserProgress.hearts-1, 0)
}).where(eq(userProgress.userId, userId))
revalidatePath("/shop")
revalidatePath("/learn")
revalidatePath("/quests")
revalidatePath("/leaderboard")
revalidatePath(`/lesson/${lessonId}`)
}
export const refillHearts = async () => {
const currentUserProgress = await getUserProgress()
if(!currentUserProgress)
{
throw new Error("User progress not found");
}
if(currentUserProgress.hearts === 5)
{
throw new Error("Hearts are already full");
}
if(currentUserProgress.points < POINTS_TO_REFILL)
{
throw new Error("not enough points")
}
await db.update(userProgress).set({
hearts: 5,
points: currentUserProgress.points - POINTS_TO_REFILL
}).where(eq(userProgress.userId, currentUserProgress.userId))
revalidatePath("/shop")
revalidatePath("/learn")
revalidatePath("/quests")
revalidatePath("/leaderboard")
}