Define an endpoint to reset the discomfort between all groups #211

Merged
bustikiller merged 1 commits from full-affinity-reset into main 2025-01-13 20:57:43 +00:00
3 changed files with 27 additions and 1 deletions

View File

@ -3,7 +3,7 @@
# frozen_string_literal: true
class AffinitiesController < ApplicationController
before_action :set_group
before_action :set_group, except: :reset
def index
overridden = @group.affinities.each_with_object({}) do |affinity, acc|
@ -39,6 +39,20 @@ class AffinitiesController < ApplicationController
end
end
def reset
affinities = Group.pluck(:id).combination(2).map do |(group_a_id, group_b_id)|
{
group_a_id:,
group_b_id:,
discomfort: Tables::DiscomfortCalculator.cohesion_discomfort(id_a: group_a_id, id_b: group_b_id).to_f
}
end
GroupAffinity.upsert_all(affinities)
render json: {}, status: :ok
end
private
def for_each_group

View File

@ -24,6 +24,7 @@ Rails.application.routes.draw do
end
resources :groups, only: %i[index create update destroy] do
post 'affinities/reset', to: 'affinities#reset', on: :collection
resources :affinities, only: %i[index] do
put :bulk_update, on: :collection
get :default, on: :collection

View File

@ -5,6 +5,17 @@
require 'swagger_helper'
RSpec.describe 'affinities' do
path '/{slug}/groups/affinities/reset' do
parameter Swagger::Schema::SLUG
post('reset affinities') do
tags 'Affinities'
description 'Reset all affinities to default values based on the distance between groups in the hierarchy.'
response_empty200
end
end
path '/{slug}/groups/{group_id}/affinities' do
parameter Swagger::Schema::SLUG
parameter name: 'group_id', in: :path, type: :string, format: :uuid, description: 'group_id'