-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #7 - add availability model and select only available participa…
…nts in matchmaking job
- Loading branch information
Showing
8 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Matchmaking | ||
class SelectsAvailableParticipants | ||
def initialize(grouping:) | ||
@grouping = grouping | ||
end | ||
|
||
def call(participant_ids) | ||
participant_ids - unavailable_participants_for_grouping(@grouping) | ||
end | ||
|
||
private | ||
|
||
def unavailable_participants_for_grouping(grouping) | ||
GroupingMemberAvailability.unavailable.where(grouping: grouping).pluck(:member_id) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class GroupingMemberAvailability < ApplicationRecord | ||
enum availability: { | ||
available: 0, | ||
unavailable: 1 | ||
} | ||
|
||
validates :grouping, :member_id, presence: true | ||
end |
13 changes: 13 additions & 0 deletions
13
db/migrate/20220120164553_create_grouping_member_availabilities.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateGroupingMemberAvailabilities < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :grouping_member_availabilities do |t| | ||
t.string :grouping | ||
t.string :member_id | ||
t.integer :availability, default: 0 | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :grouping_member_availabilities, [:grouping, :availability], name: "grouping_availability" | ||
end | ||
end |
This file contains 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
51 changes: 51 additions & 0 deletions
51
spec/lib/matchmaking/selects_available_participants_spec.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Matchmaking::SelectsAvailableParticipants, type: :matchmaking do | ||
let(:grouping) { "coffee_time" } | ||
|
||
subject { Matchmaking::SelectsAvailableParticipants.new(grouping: grouping) } | ||
|
||
it "returns empty list for no participants" do | ||
result = subject.call([]) | ||
|
||
expect(result).to eq([]) | ||
end | ||
|
||
context "when there are no unavailabilities" do | ||
it "returns all participants" do | ||
participant_ids = ["USER_1", "USER_2", "USER_3"] | ||
|
||
result = subject.call(participant_ids) | ||
|
||
expect(result).to eq(participant_ids) | ||
end | ||
end | ||
|
||
context "when there are unavailabilities for the grouping" do | ||
before do | ||
create_grouping_member_availability(grouping: grouping, member_id: "USER_3", availability: :unavailable) | ||
end | ||
|
||
it "selects only available participants" do | ||
participant_ids = ["USER_1", "USER_2", "USER_3"] | ||
|
||
result = subject.call(participant_ids) | ||
|
||
expect(result).to eq(["USER_1", "USER_2"]) | ||
end | ||
end | ||
|
||
context "when there are unavailabilities for a different grouping" do | ||
before do | ||
create_grouping_member_availability(grouping: "rotating_brunch", member_id: "USER_3", availability: :unavailable) | ||
end | ||
|
||
it "should not effect participants of an unrelated grouping" do | ||
participant_ids = ["USER_1", "USER_2", "USER_3"] | ||
|
||
result = subject.call(participant_ids) | ||
|
||
expect(result).to eq(participant_ids) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe GroupingMemberAvailability, type: :model do | ||
it "requires grouping attribute" do | ||
availability = GroupingMemberAvailability.create | ||
|
||
expect(availability.valid?).to be false | ||
expect(availability.errors[:grouping].first).to eq("can't be blank") | ||
end | ||
|
||
it "requires member_id attribute" do | ||
availability = GroupingMemberAvailability.create | ||
|
||
expect(availability.valid?).to be false | ||
expect(availability.errors[:member_id].first).to eq("can't be blank") | ||
end | ||
|
||
it "must be created with valid availability" do | ||
expect { | ||
GroupingMemberAvailability.create( | ||
grouping: "test", | ||
member_id: "USER_1", | ||
availability: :bogus | ||
) | ||
}.to raise_error(ArgumentError) | ||
end | ||
|
||
it "creates successfully" do | ||
availability = GroupingMemberAvailability.create( | ||
grouping: "test", | ||
member_id: "USER_ID_1", | ||
availability: :unavailable | ||
) | ||
|
||
expect(availability.valid?).to be true | ||
expect(availability.errors).to be_empty | ||
end | ||
end |
This file contains 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