diff --git a/app/controllers/affinities_controller.rb b/app/controllers/affinities_controller.rb index 097e051..f8cb9cb 100644 --- a/app/controllers/affinities_controller.rb +++ b/app/controllers/affinities_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index b87e0a0..10c45ae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/requests/affinities_spec.rb b/spec/requests/affinities_spec.rb index e458abb..a236160 100644 --- a/spec/requests/affinities_spec.rb +++ b/spec/requests/affinities_spec.rb @@ -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'