Define an endpoint to reset the discomfort between all groups #211
@ -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
|
||||
|
@ -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
|
||||
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user