refactor(task-prioritization): extract service object, fix bugs, add tests#102
Draft
Niethin69 wants to merge 2 commits into
Draft
Conversation
…tests Stacks on top of thoth-tech#94. Pulls scoring logic into TaskPrioritizationService, fixes status filter to resolve completed statuses by name (not magic ID 2), fixes N+1 with includes, handles nil edge cases in target_grade and weighting, and adds unit + API tests covering scoring, filtering, and edge cases. Refs thoth-tech#94
10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR stacks on top of #94 to address review-stage issues found in the original implementation. It refactors the scoring logic into a dedicated service object, fixes correctness bugs in the task status filter and nil handling, resolves an N+1 query, and adds unit and API test coverage.
Please review #94 first. This PR is best merged after #94 lands; until then, GitHub will show @rashi-agrawal29's commit in the diff alongside mine.
Refs #94
Type of change
Summary of changes
Refactor
TaskPrioritizationApiinto a newTaskPrioritizationServiceunderapp/services/. The API file is now ~15 lines and matches the auth → service → present convention used elsewhere in the codebase.effort_score_for(task)as the integration seam for the upcoming AI Task Prioritisation Service.Bug fixes
where.not(task_status_id: 2)was fragile (relied on seed order) and incomplete (did not coverdiscussordemonstrate, both of which mean "no further student action required"). Now resolved by name viaTaskStatus.where(name: %w[complete discuss demonstrate])..joinswith.includesfortask_definitionandproject => unit, both of which are accessed inside the result-building loop.Project.average(:target_grade)returnsnilfor a student with no enrolments; the previous.to_f.roundcoerced this to0and incorrectly returned the Pass score. Now falls through to an explicit default.task_definition.weightingis now coalesced explicitly. Removed misleadingcurrent_user&.idsinceauthenticated?guarantees non-nil.How Has This Been Tested?
Added two new test files:
test/api/task_prioritization_service_test.rb— covers deadline scoring buckets (including overdue and nil due-date), effort buckets, sort order, filtering by user / unit activity / enrolment / task status, response schema, and workload scoring.test/api/task_prioritization_api_test.rb— covers authentication (401 unauthenticated), empty response, sort order, and response schema.