Skip to content

Commit

Permalink
Issue #7 - add availability model and select only available participa…
Browse files Browse the repository at this point in the history
…nts in matchmaking job
  • Loading branch information
lsparlin committed Jan 20, 2022
1 parent 9d13b23 commit 1e24cc9
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/jobs/establish_matches_for_grouping_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def perform(grouping:)

matches = @matches_participants.call(
grouping: grouping,
participant_ids: @loads_slack_channel_members.call(channel: channel.id)
participant_ids: members_for_channel(channel, selected_by: Matchmaking::SelectsAvailableParticipants.new(grouping: grouping))
)
matches.each do |match|
@notifies_grouping_members.call(
Expand All @@ -30,6 +30,12 @@ def perform(grouping:)

private

def members_for_channel(channel, selected_by:)
participant_ids = @loads_slack_channel_members.call(channel: channel.id)

selected_by.call(participant_ids)
end

def channel_for_grouping(grouping)
grouping_sym = grouping.intern

Expand Down
17 changes: 17 additions & 0 deletions app/lib/matchmaking/selects_available_participants.rb
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
8 changes: 8 additions & 0 deletions app/models/grouping_member_availability.rb
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 db/migrate/20220120164553_create_grouping_member_availabilities.rb
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
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_10_21_003356) do
ActiveRecord::Schema.define(version: 2022_01_20_164553) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "grouping_member_availabilities", force: :cascade do |t|
t.string "grouping"
t.string "member_id"
t.integer "availability", default: 0
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["grouping", "availability"], name: "grouping_availability"
end

create_table "historical_matches", force: :cascade do |t|
t.string "members", default: [], array: true
t.string "grouping"
Expand Down
51 changes: 51 additions & 0 deletions spec/lib/matchmaking/selects_available_participants_spec.rb
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
38 changes: 38 additions & 0 deletions spec/models/grouping_member_availability_spec.rb
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
8 changes: 8 additions & 0 deletions spec/support/database_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module DatabaseHelpers
def create_historical_match(grouping:, members:)
HistoricalMatch.create(grouping: grouping, members: members, matched_on: Date.today)
end

def create_grouping_member_availability(grouping:, member_id:, availability:)
GroupingMemberAvailability.create(
grouping: grouping,
member_id: member_id,
availability: availability
)
end
end

0 comments on commit 1e24cc9

Please sign in to comment.