Skip to content

Commit

Permalink
Update supermentor role when changing mentored tracks (#6609)
Browse files Browse the repository at this point in the history
* Update mentored tracks in command

* Update supermentor role when updating mentored tracks
  • Loading branch information
ErikSchierboom committed Dec 19, 2023
1 parent ad4aeb1 commit 0653a73
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/commands/mentor/update_mentored_tracks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Mentor::UpdateMentoredTracks
include Mandate

initialize_with :mentor, :tracks

def call
mentor.update!(mentored_tracks: tracks)
User::UpdateSupermentorRole.defer(mentor)
end
end
3 changes: 1 addition & 2 deletions app/controllers/api/mentoring/tracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def mentored

def update
tracks = Track.where(slug: params[:track_slugs])

current_user.update!(mentored_tracks: tracks)
Mentor::UpdateMentoredTracks.(current_user, tracks)

render json: {
tracks: SerializeTracksForMentoring.(current_user.mentored_tracks, current_user)
Expand Down
52 changes: 52 additions & 0 deletions test/commands/mentor/update_mentored_tracks_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "test_helper"

class Mentor::UpdateMentoredTracksTest < ActiveSupport::TestCase
test "adds new mentored tracks" do
mentor = create :user
track = create :track

assert_empty mentor.mentored_tracks

Mentor::UpdateMentoredTracks.(mentor, [track])

assert_equal [track], mentor.mentored_tracks
end

test "keeps existing tracks" do
mentor = create :user
track = create :track
updated_at = Time.current - 2.weeks
track_mentorship = create(:user_track_mentorship, track:, user: mentor, updated_at:)

assert_equal [track], mentor.mentored_tracks

Mentor::UpdateMentoredTracks.(mentor, [track])

assert_equal [track], mentor.mentored_tracks
assert_equal updated_at, track_mentorship.updated_at
end

test "removes tracks no longer being mentored" do
mentor = create :user
track = create :track
updated_at = Time.current - 2.weeks
track_mentorship = create(:user_track_mentorship, track:, user: mentor, updated_at:)

assert_equal [track], mentor.mentored_tracks

Mentor::UpdateMentoredTracks.(mentor, [])

assert_empty mentor.mentored_tracks
assert_raises ActiveRecord::RecordNotFound do
track_mentorship.reload
end
end

test "updates supermentor role" do
mentor = create :user

User::UpdateSupermentorRole.expects(:defer).with(mentor)

Mentor::UpdateMentoredTracks.(mentor, [])
end
end

0 comments on commit 0653a73

Please sign in to comment.