From 2b4285822981c0d3775547b2ce5ce58986db1a53 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 30 Jan 2023 17:08:38 +0100 Subject: [PATCH] (feat) display special style if a training override general settings --- app/controllers/api/trainings_controller.rb | 1 + .../components/trainings/trainings.tsx | 13 +++--- .../src/javascript/models/training.ts | 5 ++- .../templates/admin/trainings/index.html | 43 ------------------- app/models/training.rb | 6 +++ .../trainings/authorization_service.rb | 7 +++ app/services/trainings/auto_cancel_service.rb | 8 ++++ .../trainings/invalidation_service.rb | 7 +++ app/views/api/trainings/index.json.jbuilder | 1 + 9 files changed, 40 insertions(+), 51 deletions(-) diff --git a/app/controllers/api/trainings_controller.rb b/app/controllers/api/trainings_controller.rb index e3c09af31..37d518c03 100644 --- a/app/controllers/api/trainings_controller.rb +++ b/app/controllers/api/trainings_controller.rb @@ -8,6 +8,7 @@ class API::TrainingsController < API::ApiController before_action :set_training, only: %i[update destroy] def index + @requested_attributes = params[:requested_attributes] @trainings = TrainingService.list(params) end diff --git a/app/frontend/src/javascript/components/trainings/trainings.tsx b/app/frontend/src/javascript/components/trainings/trainings.tsx index f1f3f66e3..fdd2bdbf9 100644 --- a/app/frontend/src/javascript/components/trainings/trainings.tsx +++ b/app/frontend/src/javascript/components/trainings/trainings.tsx @@ -9,7 +9,7 @@ import Select from 'react-select'; import { SelectOption } from '../../models/select'; import { CalendarBlank } from 'phosphor-react'; import { useEffect, useState } from 'react'; -import type { Training } from '../../models/training'; +import type { Training, TrainingIndexFilter } from '../../models/training'; import type { Machine } from '../../models/machine'; import TrainingAPI from '../../api/training'; import MachineAPI from '../../api/machine'; @@ -63,7 +63,11 @@ export const Trainings: React.FC = ({ onError, onSuccess }) => { }, []); useEffect(() => { - TrainingAPI.index(typeof filter === 'boolean' ? { disabled: filter } : {}) + const trainingsFilters = Object.assign( + { requested_attributes: ['override_settings'] }, + (typeof filter === 'boolean') ? { disabled: filter } : {} + ) as TrainingIndexFilter; + TrainingAPI.index(trainingsFilters) .then(setTrainings) .catch(onError); }, [filter]); @@ -143,11 +147,8 @@ export const Trainings: React.FC = ({ onError, onSuccess }) => {
- {/* map - ajouter la classe .is-override si l'item a au moins un réglage spécifique (différent des paramètres généraux) - */} {trainings.map(training => ( -
+
{t('app.admin.trainings.name')}

{training.name}

diff --git a/app/frontend/src/javascript/models/training.ts b/app/frontend/src/javascript/models/training.ts index 4ad7a0a00..53a873f2f 100644 --- a/app/frontend/src/javascript/models/training.ts +++ b/app/frontend/src/javascript/models/training.ts @@ -31,11 +31,12 @@ export interface Training { is_valid: boolean }> }>, - advanced_accounting_attributes?: AdvancedAccounting + advanced_accounting_attributes?: AdvancedAccounting, + override_settings?: boolean } export interface TrainingIndexFilter extends ApiFilter { disabled?: boolean, public_page?: boolean, - requested_attributes?: ['availabilities'], + requested_attributes?: ['availabilities' | 'override_settings'], } diff --git a/app/frontend/templates/admin/trainings/index.html b/app/frontend/templates/admin/trainings/index.html index bb370eba0..e5e3f6c5d 100644 --- a/app/frontend/templates/admin/trainings/index.html +++ b/app/frontend/templates/admin/trainings/index.html @@ -30,49 +30,6 @@ - -
- -
-
- - -
-
-
- - - - - - - - - - - - - - - - - - -
{{ 'app.admin.trainings.name' }}{{ 'app.admin.trainings.associated_machines' }}{{ 'app.admin.trainings.capacity' }}
{{ training.name }}{{ showMachines(training) }}{{ training.nb_total_places }} -
- - -
-
diff --git a/app/models/training.rb b/app/models/training.rb index 5ca5ae8e6..8e853e006 100644 --- a/app/models/training.rb +++ b/app/models/training.rb @@ -64,6 +64,12 @@ class Training < ApplicationRecord reservations.empty? end + def override_settings? + Trainings::AutoCancelService.override_settings?(self) || + Trainings::InvalidationService.override_settings?(self) || + Trainings::AuthorizationService.override_settings?(self) + end + private def create_trainings_pricings diff --git a/app/services/trainings/authorization_service.rb b/app/services/trainings/authorization_service.rb index 718879e2d..ee60f83f9 100644 --- a/app/services/trainings/authorization_service.rb +++ b/app/services/trainings/authorization_service.rb @@ -42,5 +42,12 @@ class Trainings::AuthorizationService params[:authorization_period] = duration.value unless duration.nil? training.update(params) end + + # @param training [Training] + # @return [Boolean] + def override_settings?(training) + training.authorization.to_s != Setting.find_by(name: 'trainings_authorization_validity').value.to_s || + training.authorization_period.to_s != Setting.find_by(name: 'trainings_authorization_validity_duration').value.to_s + end end end diff --git a/app/services/trainings/auto_cancel_service.rb b/app/services/trainings/auto_cancel_service.rb index af3352f3e..37aa42d3a 100644 --- a/app/services/trainings/auto_cancel_service.rb +++ b/app/services/trainings/auto_cancel_service.rb @@ -59,6 +59,14 @@ class Trainings::AutoCancelService training.update(params) end + # @param training [Training] + # @return [Boolean] + def override_settings?(training) + training.auto_cancel.to_s != Setting.find_by(name: 'trainings_auto_cancel').value.to_s || + training.auto_cancel_threshold.to_s != Setting.find_by(name: 'trainings_auto_cancel_threshold').value.to_s || + training.auto_cancel_deadline.to_s != Setting.find_by(name: 'trainings_auto_cancel_deadline').value.to_s + end + private # @param reservation [Reservation] diff --git a/app/services/trainings/invalidation_service.rb b/app/services/trainings/invalidation_service.rb index 5da89810d..17f13ab50 100644 --- a/app/services/trainings/invalidation_service.rb +++ b/app/services/trainings/invalidation_service.rb @@ -47,5 +47,12 @@ class Trainings::InvalidationService params[:invalidation_period] = duration.value unless duration.nil? training.update(params) end + + # @param training [Training] + # @return [Boolean] + def override_settings?(training) + training.invalidation.to_s != Setting.find_by(name: 'trainings_invalidation_rule').value.to_s || + training.invalidation_period.to_s != Setting.find_by(name: 'trainings_invalidation_rule_period').value.to_s + end end end diff --git a/app/views/api/trainings/index.json.jbuilder b/app/views/api/trainings/index.json.jbuilder index 9ebdab5a3..a6de67d77 100644 --- a/app/views/api/trainings/index.json.jbuilder +++ b/app/views/api/trainings/index.json.jbuilder @@ -3,4 +3,5 @@ json.array!(@trainings) do |training| json.partial! 'api/trainings/training', training: training json.plan_ids training.plan_ids if current_user&.admin? + json.override_settings training.override_settings? if attribute_requested?(@requested_attributes, 'override_settings') end