-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.ts
268 lines (209 loc) · 6.7 KB
/
queries.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { cache } from "react";
import db from "./drizzle";
import { auth } from "@clerk/nextjs";
import { eq } from "drizzle-orm"
import { challengeProgress, courses, lessons, units, userProgress, userSubscription } from "./schema";
export const DAY_IN_MS = 86_400_000
export const getCourses = cache(async () => {
const data = await db.query.courses.findMany();
return data
})
export const getUserProgress = cache(async () => {
const { userId } = await auth()
if(!userId)
{
return null;
}
const data = await db.query.userProgress.findFirst({
where: eq(userProgress.userId, userId),
with: {
activeCourse: true,
}
})
return data
})
export const getCourseById = cache(async (courseId: number) => {
const data = await db.query.courses.findFirst({
where: eq(courses.id, courseId),
with: {
units: {
orderBy: (units, { asc }) => [asc(units.order)],
with: {
lessons: {
orderBy: (lessons, { asc }) => [asc(lessons.order)]
}
}
}
}
})
return data
})
export const getUnits = cache(async () => {
const userProgress = await getUserProgress()
const { userId } = await auth()
if(!userId || !userProgress?.activeCourseId)
{
return []
}
const data = await db.query.units.findMany({
orderBy: (units, { asc }) => [asc(units.order)],
where: eq(units.courseId, userProgress.activeCourseId),
with: {
lessons: {
orderBy: (lessons, { asc }) => [asc(lessons.order)],
with: {
challenges: {
orderBy: (challenges, { asc }) => [asc(challenges.order)],
with: {
challengeProgress: {
where: eq(challengeProgress.userId, userId)
}
}
}
}
}
}
})
const normalizedData = data.map((unit) => {
const lessonsWithCompletedStatus = unit.lessons.map((lesson) => {
if(lesson.challenges.length===0)
{
return { ...lesson, completed: false }
}
const allCompletedChallenges = lesson.challenges.every((challenge) => {
return challenge.challengeProgress && challenge.challengeProgress.length>0 && challenge.challengeProgress.every((progress) => progress.completed)
})
return { ...lesson, completed: allCompletedChallenges }
})
return { ...unit, lessons: lessonsWithCompletedStatus }
})
return normalizedData
})
export const getCourseProgress = cache(async () => {
const { userId } = await auth()
const userProgress = await getUserProgress()
if(!userId || !userProgress?.activeCourseId)
{
return null
}
const unitsInActiveCourse = await db.query.units.findMany({
orderBy: (units, { asc }) => [asc(units.order)],
where: eq(units.courseId, userProgress.activeCourseId),
with: {
lessons: {
orderBy: (lessons, {asc}) => [asc(lessons.order)],
with: {
unit: true,
challenges: {
with: {
challengeProgress: {
where: eq(challengeProgress.userId, userId)
}
}
}
}
}
}
})
const firstUncompletedLesson = unitsInActiveCourse
.flatMap((unit) => unit.lessons)
.find((lesson) => {
return lesson.challenges.some((challenge) => {
return !challenge.challengeProgress || challenge.challengeProgress.length===0 || challenge.challengeProgress.some((progress) => progress.completed===false)
})
})
return {
activeLesson: firstUncompletedLesson,
activeLessonId: firstUncompletedLesson?.id
}
})
export const getLesson = cache(async (id?: number) => {
const { userId } = await auth()
if(!userId)
{
return null;
}
const courseProgress = await getCourseProgress()
const lessonId = id || courseProgress?.activeLessonId
if(!lessonId)
{
return null
}
const data = await db.query.lessons.findFirst({
where: eq(lessons.id, lessonId),
with: {
challenges: {
orderBy: (challenges, {asc}) => [asc(challenges.order)],
with: {
challengeOptions: true,
challengeProgress: {
where: eq(challengeProgress.userId, userId)
}
}
}
}
})
if(!data || !data.challenges)
{
return null
}
const normalizedChallenges = data.challenges.map((challenge) => {
const completed = challenge.challengeProgress && challenge.challengeProgress.length>0 && challenge.challengeProgress.every((progress) => progress.completed)
return { ...challenge, completed: completed }
})
return { ...data, challenges: normalizedChallenges }
})
export const getLessonPercentage = cache(async () => {
const courseProgress = await getCourseProgress()
if(!courseProgress || !courseProgress?.activeLessonId)
{
return 0;
}
const lesson = await getLesson(courseProgress.activeLessonId)
if(!lesson)
{
return 0;
}
const compeltedChallenges = lesson.challenges.filter((challenge) => challenge.completed)
const percentage = Math.round(
(compeltedChallenges.length / lesson.challenges.length) * 100
)
return percentage
})
export const getUserSubscription = cache(async () => {
const { userId } = await auth()
if(!userId)
{
return null;
}
const data = await db.query.userSubscription.findFirst({
where: eq(userSubscription.userId, userId)
})
if(!data)
{
return null
}
const isActive = data.stripePriceId && data.stripeCurrentPeriodEnd?.getTime()! + DAY_IN_MS > Date.now()
return {
...data,
isActive: !!isActive,
}
})
export const getTopTenUsers = cache(async () => {
const { userId } = await auth()
if(!userId)
{
return []
}
const data = await db.query.userProgress.findMany({
orderBy: (userProgress, { desc }) => [desc(userProgress.points)],
limit: 10,
columns: {
userId: true,
userName: true,
userImageSrc: true,
points: true,
}
})
return data
})