Skip to content

Commit

Permalink
fix(proaws): safely parse user in module progress getter
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtaholik committed May 13, 2024
1 parent a939e2b commit a67618f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-schools-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coursebuilder/adapter-drizzle": minor
---

safely parse user in module progress getter
18 changes: 13 additions & 5 deletions packages/adapter-drizzle/src/lib/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,6 @@ export function mySqlDrizzleAdapter(

const user = await client.query.users.findFirst({
where: or(eq(users.id, userIdOrEmail), eq(users.email, userIdOrEmail)),
with: {
roles: true,
},
})

if (!user) {
Expand All @@ -1002,11 +999,22 @@ export function mySqlDrizzleAdapter(
}
}

const parsedUser = userSchema.parse(user)
const parsedUser = userSchema.safeParse(user)

if (!parsedUser.success) {
console.error('Error parsing user', parsedUser.error)
return {
completedLessons: [],
nextResource: null,
percentCompleted: 0,
completedLessonsCount: 0,
totalLessonsCount: parsedModuleResources.data.length,
}
}

const userProgress = await client.query.resourceProgress.findMany({
where: and(
eq(resourceProgress.userId, parsedUser.id),
eq(resourceProgress.userId, parsedUser.data.id),
isNotNull(resourceProgress.completedAt),
inArray(
resourceProgress.contentResourceId,
Expand Down

0 comments on commit a67618f

Please sign in to comment.