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

Calculate total mentor training hours for a mentor for a given provider #473

Merged
merged 1 commit into from
Apr 15, 2024
Merged
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
1 change: 1 addition & 0 deletions app/models/claims/mentor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
class Claims::Mentor < Mentor
has_many :mentor_memberships
has_many :mentor_trainings
has_many :schools, through: :mentor_memberships

default_scope { joins(:mentor_memberships).distinct }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Claims::CalculateTotalMentorTrainingHoursForProvider
include ServicePattern

def initialize(mentor:, provider:)
@mentor = mentor
@provider = provider
end

def call
mentor.mentor_trainings.where(provider:).sum(:hours_completed)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this service should also calculate hours per academic year. But for now this is enough I think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should. But we'll ignore the concept of academic years for now. We'll be addressing academic years after the private beta.

end

private

attr_reader :mentor, :provider
end
1 change: 1 addition & 0 deletions spec/models/claims/mentor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
RSpec.describe Claims::Mentor, type: :model do
context "with associations" do
it { is_expected.to have_many(:mentor_memberships) }
it { is_expected.to have_many(:mentor_trainings) }
it { is_expected.to have_many(:schools).through(:mentor_memberships) }
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "rails_helper"

RSpec.describe Claims::CalculateTotalMentorTrainingHoursForProvider do
subject(:calculate_total_training_hours) { described_class.call(mentor: create(:mentor, :provider)) }

let!(:mentor) { create(:claims_mentor) }

let!(:provider) { create(:claims_provider, :niot) }
let!(:provider_2) { create(:claims_provider, :best_practice_network) }

let!(:school) { create(:claims_school, :claims, name: "School name 1", region: regions(:inner_london), urn: "1234", local_authority_name: "blah", local_authority_code: "BLA", group: "Academy") }
let!(:claim) { create(:claim, school:, reference: "12345678") }

it_behaves_like "a service object" do
let(:params) { { mentor:, provider: } }
end

it "returns the total mentor training hours for a provider for a given mentor" do
create(:mentor_training, provider:, claim:, hours_completed: 20, mentor:)
create(:mentor_training, provider:, claim:, hours_completed: 20, mentor:)

create(:mentor_training, provider: provider_2, claim:, hours_completed: 20, mentor:)

result = described_class.call(mentor:, provider:)

expect(result).to eq(40)
end
end
Loading