Skip to content

refactor(task-prioritization): extract service object, fix bugs, add tests#102

Draft
Niethin69 wants to merge 2 commits into
thoth-tech:Feature/Task-Priorfrom
Niethin69:refactor/task-prioritization-niethin
Draft

refactor(task-prioritization): extract service object, fix bugs, add tests#102
Niethin69 wants to merge 2 commits into
thoth-tech:Feature/Task-Priorfrom
Niethin69:refactor/task-prioritization-niethin

Conversation

@Niethin69
Copy link
Copy Markdown

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Summary of changes

Refactor

  • Extracted scoring logic from TaskPrioritizationApi into a new TaskPrioritizationService under app/services/. The API file is now ~15 lines and matches the auth → service → present convention used elsewhere in the codebase.
  • Promoted magic numbers (deadline buckets, effort buckets, scoring weights, task pressure thresholds, target grade scores) to named constants at the top of the service.
  • Exposed effort_score_for(task) as the integration seam for the upcoming AI Task Prioritisation Service.

Bug fixes

  • Status filter: the original where.not(task_status_id: 2) was fragile (relied on seed order) and incomplete (did not cover discuss or demonstrate, both of which mean "no further student action required"). Now resolved by name via TaskStatus.where(name: %w[complete discuss demonstrate]).
  • N+1 queries: replaced .joins with .includes for task_definition and project => unit, both of which are accessed inside the result-building loop.
  • Nil handling: Project.average(:target_grade) returns nil for a student with no enrolments; the previous .to_f.round coerced this to 0 and incorrectly returned the Pass score. Now falls through to an explicit default. task_definition.weighting is now coalesced explicitly. Removed misleading current_user&.id since authenticated? 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.

rashi-agrawal29 and others added 2 commits April 29, 2026 17:55
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants