Merge pull request 'Define an endpoint to return the default affinities of a group' (#209) from affinity-reset into main
All checks were successful
Check usage of free licenses / check-licenses (push) Successful in 36s
Run unit tests / unit_tests (push) Successful in 1m15s
Build Nginx-based docker image / build-static-assets (push) Successful in 12m1s

Reviewed-on: #209
This commit is contained in:
bustikiller 2025-01-12 21:34:04 +00:00
commit 65a265b900
3 changed files with 33 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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'