From d092a1c5ead51eb04c16a6d2ad7df0d12243ba80 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 6 Sep 2017 15:01:41 +0200 Subject: [PATCH] admin can lock and unlock availbilities --- .../controllers/admin/calendar.coffee.erb | 23 ++++++++ .../javascripts/services/availability.coffee | 3 + app/assets/stylesheets/app.layout.scss | 4 ++ .../admin/calendar/calendar.html.erb | 58 ++++++++++++------- .../api/availabilities_controller.rb | 15 ++++- app/policies/availability_policy.rb | 2 +- .../api/availabilities/index.json.jbuilder | 3 +- .../api/availabilities/show.json.jbuilder | 1 + config/locales/app.admin.fr.yml | 10 ++++ config/routes.rb | 1 + ...20170906100906_add_lock_to_availability.rb | 5 ++ db/schema.rb | 3 +- 12 files changed, 104 insertions(+), 24 deletions(-) create mode 100644 db/migrate/20170906100906_add_lock_to_availability.rb diff --git a/app/assets/javascripts/controllers/admin/calendar.coffee.erb b/app/assets/javascripts/controllers/admin/calendar.coffee.erb index 33f9394ad..fe56df5b2 100644 --- a/app/assets/javascripts/controllers/admin/calendar.coffee.erb +++ b/app/assets/javascripts/controllers/admin/calendar.coffee.erb @@ -132,6 +132,29 @@ Application.Controllers.controller "AdminCalendarController", ["$scope", "$state + ## + # Mark the selected slot as unavailable for new reservations or allow reservations again on it + ## + $scope.toggleLockReservations = -> + locked = $scope.availability.lock + # open a confirmation dialog + dialogs.confirm + resolve: + object: -> + title: _t('admin_calendar.confirmation_required') + msg: if locked then _t("admin_calendar.do_you_really_want_to_allow_reservations") else _t("admin_calendar.do_you_really_want_to_block_this_slot_html") + , -> + # the admin has confirmed, lock/unlock the slot + Availability.lock {id: $scope.availability.id}, {lock: !locked} + , (data) -> # success + $scope.availability = data + growl.success(if locked then _t('admin_calendar.unlocking_success') else _t('admin_calendar.locking_success') ) + uiCalendarConfig.calendars.calendar.fullCalendar 'refetchEvents' + , (error) -> # failed + growl.error(if locked then _t('admin_calendar.unlocking_failed') else _t('admin_calendar.locking_failed')) + + + ### PRIVATE SCOPE ### ## diff --git a/app/assets/javascripts/services/availability.coffee b/app/assets/javascripts/services/availability.coffee index 4bc448b13..c837977c1 100644 --- a/app/assets/javascripts/services/availability.coffee +++ b/app/assets/javascripts/services/availability.coffee @@ -24,4 +24,7 @@ Application.Services.factory 'Availability', ["$resource", ($resource)-> isArray: true update: method: 'PUT' + lock: + method: 'PUT' + url: '/api/availabilities/:id/lock' ] diff --git a/app/assets/stylesheets/app.layout.scss b/app/assets/stylesheets/app.layout.scss index bd693d343..3ad8383b8 100644 --- a/app/assets/stylesheets/app.layout.scss +++ b/app/assets/stylesheets/app.layout.scss @@ -360,6 +360,10 @@ body.container{ } +.reservations-locked { + background-color: #f5f5f5; +} + .reservation-canceled { color: #606060; border-radius: 0.2em; diff --git a/app/assets/templates/admin/calendar/calendar.html.erb b/app/assets/templates/admin/calendar/calendar.html.erb index 2e7c16dd3..e8909d26e 100644 --- a/app/assets/templates/admin/calendar/calendar.html.erb +++ b/app/assets/templates/admin/calendar/calendar.html.erb @@ -42,11 +42,12 @@ -
+ +

{{ 'admin_calendar.ongoing_reservations' }}

-
+
  • {{r.user.name}} @@ -55,25 +56,42 @@
-
{{ 'admin_calendar.no_reservations' }}
+
{{ 'admin_calendar.no_reservations' }}
+
{{ 'admin_calendar.reservations_locked' }}
+
+
+ +
+
+

{{ 'admin_calendar.machines' }}

+
+
+
    +
  • + {{m.name}} + +
  • +
+
+
+ +
+
+

{{ 'admin_calendar.actions' }}

+
+
+
-
-
-
-

{{ 'admin_calendar.machines' }}

-
-
-
    -
  • - {{m.name}} - -
  • -
-
-
-
- - + \ No newline at end of file diff --git a/app/controllers/api/availabilities_controller.rb b/app/controllers/api/availabilities_controller.rb index af00bfcea..10f7a6cd5 100644 --- a/app/controllers/api/availabilities_controller.rb +++ b/app/controllers/api/availabilities_controller.rb @@ -2,7 +2,7 @@ class API::AvailabilitiesController < API::ApiController include FablabConfiguration before_action :authenticate_user!, except: [:public] - before_action :set_availability, only: [:show, :update, :destroy, :reservations] + before_action :set_availability, only: [:show, :update, :destroy, :reservations, :lock] before_action :define_max_visibility, only: [:machine, :trainings, :spaces] respond_to :json @@ -235,6 +235,15 @@ class API::AvailabilitiesController < API::ApiController end end + def lock + authorize @availability + if @availability.update_attributes(lock: lock_params) + render :show, status: :ok, location: @availability + else + render json: @availability.errors, status: :unprocessable_entity + end + end + private def set_availability @availability = Availability.find(params[:id]) @@ -245,6 +254,10 @@ class API::AvailabilitiesController < API::ApiController :machines_attributes => [:id, :_destroy]) end + def lock_params + params.require(:lock) + end + def is_reserved_availability(availability, user_id) reserved_slots = [] availability.slots.each do |s| diff --git a/app/policies/availability_policy.rb b/app/policies/availability_policy.rb index 85f8eefe2..d298e641b 100644 --- a/app/policies/availability_policy.rb +++ b/app/policies/availability_policy.rb @@ -1,5 +1,5 @@ class AvailabilityPolicy < ApplicationPolicy - %w(index? show? create? update? destroy? reservations? export?).each do |action| + %w(index? show? create? update? destroy? reservations? export? lock?).each do |action| define_method action do user.is_admin? end diff --git a/app/views/api/availabilities/index.json.jbuilder b/app/views/api/availabilities/index.json.jbuilder index 606f92dee..8749f6ca8 100644 --- a/app/views/api/availabilities/index.json.jbuilder +++ b/app/views/api/availabilities/index.json.jbuilder @@ -6,11 +6,12 @@ json.array!(@availabilities) do |availability| json.available_type availability.available_type json.machine_ids availability.machine_ids json.training_ids availability.training_ids - json.backgroundColor 'white' + json.backgroundColor !availability.lock ? 'white' : '#f5f5f5' json.borderColor availability_border_color(availability) json.tag_ids availability.tag_ids json.tags availability.tags do |t| json.id t.id json.name t.name end + json.lock availability.lock end diff --git a/app/views/api/availabilities/show.json.jbuilder b/app/views/api/availabilities/show.json.jbuilder index 5c97a67b4..68706604f 100644 --- a/app/views/api/availabilities/show.json.jbuilder +++ b/app/views/api/availabilities/show.json.jbuilder @@ -11,3 +11,4 @@ json.tags @availability.tags do |t| json.id t.id json.name t.name end +json.lock @availability.lock diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index aa9ebc866..f43de6319 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -48,6 +48,16 @@ fr: unable_to_delete_the_slot_START-END_because_it_s_already_reserved_by_a_member: "Le créneau {{START}} - {{END}} n'a pu être supprimé car il est déjà réservé par un membre" # angular interpolation you_should_select_at_least_a_machine: "Vous devriez sélectionne au moins une machine pour ce créneau." export_is_running_you_ll_be_notified_when_its_ready: "L'export est en cours. Vous serez notifié lorsqu'il sera prêt." + actions: "Actions" + block_reservations: "Bloquer les réservations" + do_you_really_want_to_block_this_slot_html: "Êtes vous sur de vouloir bloquer les nouvelles réservations sur ce créneau ? Il deviendra alors invisible pour les utilisateurs.
Attention : cela n'annulera pas les réservations existantes." + locking_success: "Le créneau a bien été verrouillé, il n'apparaitra plus dans le calendrier utilisateur" + locking_failed: "Une erreur est survenue. Le verrouillage du créneau a échoué" + allow_reservations: "Autoriser les réservations" + do_you_really_want_to_allow_reservations: "Êtes vous sur de vouloir autoriser de nouveau la prise de réservations sur ce créneau ? Il deviendra alors visible pour les utilisateurs." + unlocking_success: "Le créneau a bien été déverrouillé, il apparaîtra de nouveau dans le calendrier utilisateur" + unlocking_failed: "Une erreur est survenue. Le déverrouillage du créneau a échoué" + reservations_locked: "Réservations bloquées" project_elements: # gestion des éléments constituant les projets diff --git a/config/routes.rb b/config/routes.rb index 262e496c1..0b4596592 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -85,6 +85,7 @@ Rails.application.routes.draw do get 'reservations', on: :member get 'public', on: :collection get '/export_index', action: 'export_availabilities', on: :collection + put ':id/lock', action: 'lock', on: :collection end resources :groups, only: [:index, :create, :update, :destroy] diff --git a/db/migrate/20170906100906_add_lock_to_availability.rb b/db/migrate/20170906100906_add_lock_to_availability.rb new file mode 100644 index 000000000..8a27aad1e --- /dev/null +++ b/db/migrate/20170906100906_add_lock_to_availability.rb @@ -0,0 +1,5 @@ +class AddLockToAvailability < ActiveRecord::Migration + def change + add_column :availabilities, :lock, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index a76ff5499..c6095c45a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170227114634) do +ActiveRecord::Schema.define(version: 20170906100906) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -79,6 +79,7 @@ ActiveRecord::Schema.define(version: 20170227114634) do t.datetime "updated_at" t.integer "nb_total_places" t.boolean "destroying", default: false + t.boolean "lock", default: false end create_table "availability_tags", force: :cascade do |t|