From f997657cd3ac1c170ea8767d46178b331744887f Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 12 Jan 2025 20:41:01 +0100 Subject: [PATCH] Define an endpoint to return the default affinities of a group --- app/controllers/affinities_controller.rb | 21 +++++++++++++++++---- config/routes.rb | 1 + spec/requests/affinities_spec.rb | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/controllers/affinities_controller.rb b/app/controllers/affinities_controller.rb index f8b7052..097e051 100644 --- a/app/controllers/affinities_controller.rb +++ b/app/controllers/affinities_controller.rb @@ -9,10 +9,10 @@ class AffinitiesController < ApplicationController overridden = @group.affinities.each_with_object({}) do |affinity, acc| acc[affinity.another_group(@group).id] = affinity.discomfort end - Group.where.not(id: @group.id) - .pluck(:id) - .index_with { |group_id| GroupAffinity::MAX_DISCOMFORT - (overridden[group_id] || GroupAffinity::NEUTRAL) } - .then { |affinities| render json: affinities } + + for_each_group do |group_id| + overridden[group_id] || GroupAffinity::NEUTRAL + end end def bulk_update @@ -33,8 +33,21 @@ class AffinitiesController < ApplicationController render json: { error: 'Invalid group ID or discomfort provided.' }, status: :bad_request end + def default + for_each_group do |group_id| + Tables::DiscomfortCalculator.cohesion_discomfort(id_a: @group.id, id_b: group_id).to_f + end + end + private + def for_each_group + Group.where.not(id: @group.id) + .pluck(:id) + .index_with { |group_id| GroupAffinity::MAX_DISCOMFORT - yield(group_id) } + .then { |affinities| render json: affinities } + end + def set_group @group = Group.find(params[:group_id]) end diff --git a/config/routes.rb b/config/routes.rb index 9e2b004..b87e0a0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,6 +26,7 @@ Rails.application.routes.draw do resources :groups, only: %i[index create update destroy] do resources :affinities, only: %i[index] do put :bulk_update, on: :collection + get :default, on: :collection end end diff --git a/spec/requests/affinities_spec.rb b/spec/requests/affinities_spec.rb index c964018..e458abb 100644 --- a/spec/requests/affinities_spec.rb +++ b/spec/requests/affinities_spec.rb @@ -20,6 +20,21 @@ RSpec.describe 'affinities' do end end + path '/{slug}/groups/{group_id}/affinities/default' do + parameter Swagger::Schema::SLUG + parameter name: 'group_id', in: :path, type: :string, format: :uuid, description: 'group_id' + + get('calculate default affinity') do + tags 'Affinities' + produces 'application/json' + + response(200, 'successful') do + schema type: :object, additionalProperties: { type: :integer, minimum: 0, maximum: 2 } + xit + end + end + end + path '/{slug}/groups/{group_id}/affinities/bulk_update' do parameter Swagger::Schema::SLUG parameter name: 'group_id', in: :path, type: :string, format: :uuid, description: 'group_id' -- 2.47.1