garageScript/c0d3-app

Filter exercises in the backend

Opened this issue · 2 comments

Problem

All the exercises are sent to the frontend and only a portion is rendered for the user. This does not only impact the performance on mobile devices, but also waste the user bandwidth.

Steps to reproduce

  1. Sign in with admin user and go to /exercises/js0
  2. Go to /curriculum/js0/mentor and remove an exercise
  3. Go to /exercises/js0 and inspect the getExercises query request
  4. Notice how it has all the exercises even the removed one

Expected behavior

Backend should only send exercises that are not removed and are for the current lesson. If the user is taking the exercises of JS1, it should send the exercises for JS1

Actual behavior

Backend sends all the exercises to the client (All lessons exercises & removed exercises)

I looked into this issue and it looks like each exercise has a removedAt flag (of type timestamp) in the database.

We could keep this functionality as-is or have the remove exercise function outright delete the exercise from the database (and eliminate the removedAt column).

const currentExercises = exercises
    .filter(
      exercise =>
        exercise.module.lesson.slug === slug &&
        exercise.author.id === sessionUser?.id &&
        !exercise.removedAt
    )
    .map(exercise => ({
      id: exercise.id,
      moduleName: exercise.module.name,
      problem: exercise.description,
      answer: exercise.answer,
      explanation: exercise.explanation || ''
    }))
    
    return ...

The functions are under /graphql/resolvers/exerciseCrud.ts