Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: enable querying tasks required for course completion #977

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions backend/graphql/RequiredForCompletion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { enumType, objectType } from "nexus"

import { RequiredForCompletionEnum } from "../typeDefs"

function convertTsEnum(toConvert: any) {
const converted: { [key: string]: number } = {}

Object.keys(toConvert).forEach((key) => {
if (/^[_a-zA-Z][_a-zA-Z0-9]*$/.test(key)) {
converted[key] = toConvert[key]
}
})

return converted
}

export const RequiredForCompletionType = enumType({
name: "RequiredForCompletionType",
members: convertTsEnum(RequiredForCompletionEnum),
})

export const RequiredForCompletion = objectType({
name: "RequiredForCompletion",
definition(t) {
t.nonNull.field("type", {
type: "RequiredForCompletionType",
})
t.int("current_amount")
t.int("required_amount")
t.list.field("required_actions", {
type: "ExerciseCompletionRequiredAction",
})
},
})
18 changes: 15 additions & 3 deletions backend/graphql/User/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,28 @@ export const User = objectType({

t.list.field("user_course_summary", {
type: "UserCourseSummary",
resolve: async (parent, _, ctx) => {
args: {
include_hidden_exercises: booleanArg({ default: false }),
},
resolve: async (parent, { include_hidden_exercises }, ctx) => {
// not very optimal, as the exercise completions will be queried twice if that field is selected
const exerciseCompletionCourses = await ctx.prisma.user
.findUnique({
where: { id: parent.id },
})
.exercise_completions({
select: {
where: {
...(!include_hidden_exercises
? {
exercise: {
deleted: { not: true },
},
}
: {}),
},
include: {
exercise: {
select: {
include: {
course: {
select: {
id: true,
Expand Down
99 changes: 99 additions & 0 deletions backend/graphql/UserCourseSummary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { UserInputError } from "apollo-server-express"
import { uniqBy } from "lodash"
import { objectType } from "nexus"

import { RequiredForCompletionEnum } from "../typeDefs"
import { RequiredForCompletion } from "./RequiredForCompletion"

export const UserCourseSummary = objectType({
name: "UserCourseSummary",
definition(t) {
Expand All @@ -9,6 +13,101 @@ export const UserCourseSummary = objectType({
t.id("inherit_settings_from_id")
t.id("completions_handled_by_id")

t.nonNull.list.nonNull.field("required_for_completion", {
type: RequiredForCompletion,
resolve: async ({ course_id, user_id }, _, ctx) => {
if (!course_id || !user_id) {
throw new UserInputError("must provide course_id and user_id")
}

const completion = await ctx.prisma.completion.findMany({
where: {
user_id,
course_id,
},
})

if (completion.length) {
return []
}

const result = []

const { points_needed, exercise_completions_needed } =
(await ctx.prisma.course.findUnique({
where: {
id: course_id,
},
})) ?? {}

const { user_course_progresses, exercise_completions } =
(await ctx.prisma.user.findUnique({
where: {
id: user_id,
},
include: {
user_course_progresses: {
where: {
course_id,
},
select: {
n_points: true,
},
orderBy: {
created_at: "asc",
},
take: 1,
},
exercise_completions: {
where: {
exercise: {
course_id,
deleted: { not: true },
},
},
include: {
exercise_completion_required_actions: true,
},
},
},
})) ?? {}

const n_points = user_course_progresses?.[0]?.n_points ?? 0
const uniqueExerciseCompletions = uniqBy(exercise_completions, "id")
const requiredActions = uniqueExerciseCompletions.flatMap(
(ec) => ec.exercise_completion_required_actions,
)

if (points_needed && n_points < points_needed) {
result.push({
type: RequiredForCompletionEnum.NOT_ENOUGH_POINTS,
current_amount: n_points,
needed_amount: points_needed,
})
}

if (
exercise_completions_needed &&
uniqueExerciseCompletions.length < exercise_completions_needed
) {
result.push({
type: RequiredForCompletionEnum.NOT_ENOUGH_EXERCISE_COMPLETIONS,
current_amount: uniqueExerciseCompletions.length,
required_amount: exercise_completions_needed,
})
}

if (requiredActions.length) {
result.push({
type: RequiredForCompletionEnum.REQUIRED_ACTIONS,
required_actions: requiredActions,
})
}

return result
},
})

t.field("course", {
type: "Course",
resolve: async ({ course_id }, _, ctx) => {
Expand Down
1 change: 1 addition & 0 deletions backend/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from "./Organization"
export * from "./OrganizationTranslation"
export * from "./PointsByGroup"
export * from "./Progress"
export * from "./RequiredForCompletion"
export * from "./Service"
export * from "./StoredData"
export * from "./StudyModule"
Expand Down
5 changes: 5 additions & 0 deletions backend/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export default makeSchema({
module: require.resolve(".prisma/client/index.d.ts"),
alias: "prisma",
},
{
module: path.join(__dirname, "typeDefs.ts"),
alias: "t",
},
],
},
plugins: createPlugins(),
Expand All @@ -80,4 +84,5 @@ export default makeSchema({
},
shouldGenerateArtifacts: true,
shouldExitAfterGenerateArtifacts: Boolean(NEXUS_REFLECTION),
prettierConfig: require.resolve("../.prettierrc.yaml"),
})
17 changes: 17 additions & 0 deletions backend/typeDefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ExerciseCompletionRequiredAction } from "@prisma/client"

export enum RequiredForCompletionEnum {
NOT_ENOUGH_POINTS = "NOT_ENOUGH_POINTS",
NOT_ENOUGH_EXERCISE_COMPLETIONS = "NOT_ENOUGH_EXERCISE_COMPLETIONS",
REQUIRED_ACTIONS = "REQUIRED_ACTIONS",
}

export type RequiredForCompletion = {
type:
| RequiredForCompletionEnum.NOT_ENOUGH_POINTS
| RequiredForCompletionEnum.NOT_ENOUGH_EXERCISE_COMPLETIONS
| RequiredForCompletionEnum.REQUIRED_ACTIONS
current_amount?: number
required_amount?: number
required_actions?: ExerciseCompletionRequiredAction[]
}
23 changes: 13 additions & 10 deletions frontend/components/Dashboard/PointsListItemCard.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { useState } from "react"
import { Grid } from "@mui/material"
import PointsItemTable from "./PointsItemTable"
import styled from "@emotion/styled"
import { gql } from "@apollo/client"
import formatPointsData, {
formattedGroupPointsDictionary,
} from "/util/formatPointsData"
import { CardTitle, CardSubtitle } from "/components/Text/headers"

import { FormSubmitButton } from "/components/Buttons/FormSubmitButton"
import PointsProgress from "/components/Dashboard/PointsProgress"
import { CardSubtitle, CardTitle } from "/components/Text/headers"
import { ProgressUserCourseProgressFragment } from "/graphql/fragments/userCourseProgress"
import { ProgressUserCourseServiceProgressFragment } from "/graphql/fragments/userCourseServiceProgress"
import { UserCourseProgressFragment } from "/static/types/generated/UserCourseProgressFragment"
import { UserCourseServiceProgressFragment } from "/static/types/generated/UserCourseServiceProgressFragment"
import { UserPoints_currentUser_progresses_course } from "/static/types/generated/UserPoints"
import formatPointsData, {
formattedGroupPointsDictionary,
} from "/util/formatPointsData"

import { gql } from "@apollo/client"
import styled from "@emotion/styled"
import { Grid } from "@mui/material"

import PointsItemTable from "./PointsItemTable"

const UserFragment = gql`
fragment UserPointsFragment on User {
Expand Down Expand Up @@ -104,11 +107,11 @@ function PointsListItemCard(props: Props) {
{showProgress ? (
<>
<PointsProgress
total={formattedPointsData.total * 100}
percentage={formattedPointsData.total * 100}
title="Total progress"
/>
<PointsProgress
total={formattedPointsData.exercises * 100}
percentage={formattedPointsData.exercises * 100}
title="Exercises completed"
/>
<hr />
Expand Down
26 changes: 22 additions & 4 deletions frontend/components/Dashboard/PointsProgress.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CardSubtitle } from "/components/Text/headers"
import notEmpty from "/util/notEmpty"

import styled from "@emotion/styled"
import { LinearProgress } from "@mui/material"
import { CardSubtitle } from "/components/Text/headers"

const ColoredProgressBar = styled(({ ...props }) => (
<LinearProgress {...props} />
Expand All @@ -24,7 +26,18 @@ const ChartContainer = styled.div`
margin-bottom: 1rem;
`

const PointsProgress = ({ total, title }: { total: number; title: string }) => (
interface PointsProgressProps {
amount?: number | null
required?: number | null
percentage: number
title: string
}
const PointsProgress = ({
amount,
required,
percentage,
title,
}: PointsProgressProps) => (
<>
<CardSubtitle
component="h3"
Expand All @@ -34,13 +47,18 @@ const PointsProgress = ({ total, title }: { total: number; title: string }) => (
{title}
</CardSubtitle>
<ChartContainer>
<CardSubtitle align="right">{total.toFixed(0)}%</CardSubtitle>
<CardSubtitle align="right">{percentage.toFixed(0)}%</CardSubtitle>
<ColoredProgressBar
variant="determinate"
value={total}
value={percentage}
style={{ padding: "0.5rem", flex: 1 }}
color="primary"
/>
{notEmpty(amount) && notEmpty(required) && required > 0 ? (
<CardSubtitle align="right">
{amount} out of {required} required
</CardSubtitle>
) : null}
</ChartContainer>
</>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function CourseEntry({ data }: CourseEntryProps) {
userCourseServiceProgresses={data.user_course_service_progresses?.filter(
notEmpty,
)}
exerciseCompletions={data.exercise_completions?.filter(notEmpty)}
/>
<ExerciseList
exercises={sortBy(exercisesWithCompletions, ["name"])}
Expand Down
Loading