mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-21 15:54:22 +01:00
refactored events controller
This commit is contained in:
parent
d2c4773ac9
commit
da25e16c50
@ -1,12 +1,13 @@
|
|||||||
# Changelog Fab Manager
|
# Changelog Fab Manager
|
||||||
|
|
||||||
|
- Removed ability to disable invoicing for an user
|
||||||
|
- Fixed a missing translation in plan form
|
||||||
- Fix a bug: error handling on password recovery
|
- Fix a bug: error handling on password recovery
|
||||||
- Fix a bug: error handling on machine attachment upload
|
- Fix a bug: error handling on machine attachment upload
|
||||||
- Fix a bug: first day of week is ignored in statistics custom filter
|
- Fix a bug: first day of week is ignored in statistics custom filter
|
||||||
- Fix a bug: rails DSB locale is invalid
|
- Fix a bug: rails DSB locale is invalid
|
||||||
- Fix a bug: unable to delete an admin who has changed a setting
|
- Fix a bug: unable to delete an admin who has changed a setting
|
||||||
- Fix a bug: unable to create/edit a plan of 12 months or 52 weeks
|
- Fix a bug: unable to create/edit a plan of 12 months or 52 weeks
|
||||||
- Removed ability to disable invoicing for an user
|
|
||||||
- Refactored frontend invoices translations
|
- Refactored frontend invoices translations
|
||||||
- Updated RailRoady 1.4.0 to 1.5.3
|
- Updated RailRoady 1.4.0 to 1.5.3
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# API Controller for resources of type Event
|
||||||
class API::EventsController < API::ApiController
|
class API::EventsController < API::ApiController
|
||||||
before_action :set_event, only: [:show, :update, :destroy]
|
before_action :set_event, only: %i[show update destroy]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@events = policy_scope(Event)
|
@events = policy_scope(Event)
|
||||||
@ -11,16 +14,16 @@ class API::EventsController < API::ApiController
|
|||||||
@events = @events.joins(:event_themes).where('event_themes.id = :theme', theme: params[:theme_id]) if params[:theme_id]
|
@events = @events.joins(:event_themes).where('event_themes.id = :theme', theme: params[:theme_id]) if params[:theme_id]
|
||||||
@events = @events.where('age_range_id = :age_range', age_range: params[:age_range_id]) if params[:age_range_id]
|
@events = @events.where('age_range_id = :age_range', age_range: params[:age_range_id]) if params[:age_range_id]
|
||||||
|
|
||||||
if current_user and current_user.admin?
|
if current_user&.admin?
|
||||||
case params[:scope]
|
@events = case params[:scope]
|
||||||
when 'future'
|
when 'future'
|
||||||
@events = @events.where('availabilities.start_at >= ?', Time.now).order('availabilities.start_at DESC')
|
@events.where('availabilities.start_at >= ?', Time.now).order('availabilities.start_at DESC')
|
||||||
when 'future_asc'
|
when 'future_asc'
|
||||||
@events = @events.where('availabilities.start_at >= ?', Time.now).order('availabilities.start_at ASC')
|
@events.where('availabilities.start_at >= ?', Time.now).order('availabilities.start_at ASC')
|
||||||
when 'passed'
|
when 'passed'
|
||||||
@events = @events.where('availabilities.start_at < ?', Time.now).order('availabilities.start_at DESC')
|
@events.where('availabilities.start_at < ?', Time.now).order('availabilities.start_at DESC')
|
||||||
else
|
else
|
||||||
@events = @events.order('availabilities.start_at DESC')
|
@events.order('availabilities.start_at DESC')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -38,8 +41,7 @@ class API::EventsController < API::ApiController
|
|||||||
.limit(limit)
|
.limit(limit)
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show; end
|
||||||
end
|
|
||||||
|
|
||||||
def create
|
def create
|
||||||
authorize Event
|
authorize Event
|
||||||
@ -61,7 +63,8 @@ class API::EventsController < API::ApiController
|
|||||||
end
|
end
|
||||||
rescue ActiveRecord::RecordNotDestroyed => e
|
rescue ActiveRecord::RecordNotDestroyed => e
|
||||||
if e.record.class.name == 'EventPriceCategory'
|
if e.record.class.name == 'EventPriceCategory'
|
||||||
render json: {error: ["#{e.record.price_category.name}: #{t('events.error_deleting_reserved_price')}"]}, status: :unprocessable_entity
|
render json: { error: ["#{e.record.price_category.name}: #{t('events.error_deleting_reserved_price')}"] },
|
||||||
|
status: :unprocessable_entity
|
||||||
else
|
else
|
||||||
render json: { error: [t('events.other_error')] }, status: :unprocessable_entity
|
render json: { error: [t('events.other_error')] }, status: :unprocessable_entity
|
||||||
end
|
end
|
||||||
@ -79,6 +82,7 @@ class API::EventsController < API::ApiController
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Use callbacks to share common setup or constraints between actions.
|
# Use callbacks to share common setup or constraints between actions.
|
||||||
def set_event
|
def set_event
|
||||||
@event = Event.find(params[:id])
|
@event = Event.find(params[:id])
|
||||||
@ -88,39 +92,12 @@ class API::EventsController < API::ApiController
|
|||||||
def event_params
|
def event_params
|
||||||
# handle general properties
|
# handle general properties
|
||||||
event_preparams = params.required(:event).permit(:title, :description, :start_date, :start_time, :end_date, :end_time,
|
event_preparams = params.required(:event).permit(:title, :description, :start_date, :start_time, :end_date, :end_time,
|
||||||
:amount, :nb_total_places, :availability_id,
|
:amount, :nb_total_places, :availability_id, :all_day, :recurrence,
|
||||||
:all_day, :recurrence, :recurrence_end_at, :category_id, :event_theme_ids,
|
:recurrence_end_at, :category_id, :event_theme_ids, :age_range_id,
|
||||||
:age_range_id, event_theme_ids: [],
|
event_theme_ids: [],
|
||||||
event_image_attributes: [:attachment],
|
event_image_attributes: [:attachment],
|
||||||
event_files_attributes: [:id, :attachment, :_destroy],
|
event_files_attributes: %i[id attachment_destroy],
|
||||||
event_price_categories_attributes: [:id, :price_category_id, :amount, :_destroy]
|
event_price_categories_attributes: %i[id price_category_id amount_destroy])
|
||||||
)
|
EventService.process_params(event_preparams)
|
||||||
# handle dates & times (whole-day events or not, maybe during many days)
|
|
||||||
start_date = Time.zone.parse(event_preparams[:start_date])
|
|
||||||
end_date = Time.zone.parse(event_preparams[:end_date])
|
|
||||||
start_time = Time.parse(event_preparams[:start_time]) if event_preparams[:start_time]
|
|
||||||
end_time = Time.parse(event_preparams[:end_time]) if event_preparams[:end_time]
|
|
||||||
if event_preparams[:all_day] == 'true'
|
|
||||||
start_at = DateTime.new(start_date.year, start_date.month, start_date.day, 0, 0, 0, start_date.zone)
|
|
||||||
end_at = DateTime.new(end_date.year, end_date.month, end_date.day, 23, 59, 59, end_date.zone)
|
|
||||||
else
|
|
||||||
start_at = DateTime.new(start_date.year, start_date.month, start_date.day, start_time.hour, start_time.min, start_time.sec, start_date.zone)
|
|
||||||
end_at = DateTime.new(end_date.year, end_date.month, end_date.day, end_time.hour, end_time.min, end_time.sec, end_date.zone)
|
|
||||||
end
|
|
||||||
event_preparams.merge!(availability_attributes: {id: event_preparams[:availability_id], start_at: start_at, end_at: end_at, available_type: 'event'})
|
|
||||||
.except!(:start_date, :end_date, :start_time, :end_time, :all_day)
|
|
||||||
# convert main price to centimes
|
|
||||||
event_preparams.merge!(amount: (event_preparams[:amount].to_f * 100 if event_preparams[:amount].present?))
|
|
||||||
# delete non-complete "other" prices and convert them to centimes
|
|
||||||
unless event_preparams[:event_price_categories_attributes].nil?
|
|
||||||
event_preparams[:event_price_categories_attributes].delete_if { |price_cat| price_cat[:price_category_id].empty? or price_cat[:amount].empty? }
|
|
||||||
event_preparams[:event_price_categories_attributes].each do |price_cat|
|
|
||||||
price_cat[:amount] = price_cat[:amount].to_f * 100
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# return the resulting params object
|
|
||||||
event_preparams
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
end
|
|
||||||
|
44
app/services/event_service.rb
Normal file
44
app/services/event_service.rb
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Provides helper methods for Events resources and properties
|
||||||
|
class EventService
|
||||||
|
def self.process_params(params)
|
||||||
|
# handle dates & times (whole-day events or not, maybe during many days)
|
||||||
|
range = EventService.date_range({date: params[:start_date], time: params[:start_time] },
|
||||||
|
{ date: params[:end_date], time: params[:end_time] },
|
||||||
|
params[:all_day] == 'true')
|
||||||
|
params.merge!(availability_attributes: { id: params[:availability_id],
|
||||||
|
start_at: range[:start_at],
|
||||||
|
end_at: range[:end_at],
|
||||||
|
available_type: 'event' })
|
||||||
|
.except!(:start_date, :end_date, :start_time, :end_time, :all_day)
|
||||||
|
# convert main price to centimes
|
||||||
|
params[:amount] = (params[:amount].to_f * 100 if params[:amount].present?)
|
||||||
|
# delete non-complete "other" prices and convert them to centimes
|
||||||
|
unless params[:event_price_categories_attributes].nil?
|
||||||
|
params[:event_price_categories_attributes].delete_if do |price_cat|
|
||||||
|
price_cat[:price_category_id].empty? || price_cat[:amount].empty?
|
||||||
|
end
|
||||||
|
params[:event_price_categories_attributes].each do |price_cat|
|
||||||
|
price_cat[:amount] = price_cat[:amount].to_f * 100
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# return the resulting params object
|
||||||
|
params
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.date_range(starting, ending, all_day)
|
||||||
|
start_date = Time.zone.parse(starting[:date])
|
||||||
|
end_date = Time.zone.parse(ending[:date])
|
||||||
|
start_time = Time.parse(starting[:time]) if starting[:time]
|
||||||
|
end_time = Time.parse(ending[:time]) if ending[:time]
|
||||||
|
if all_day == 'true'
|
||||||
|
start_at = DateTime.new(start_date.year, start_date.month, start_date.day, 0, 0, 0, start_date.zone)
|
||||||
|
end_at = DateTime.new(end_date.year, end_date.month, end_date.day, 23, 59, 59, end_date.zone)
|
||||||
|
else
|
||||||
|
start_at = DateTime.new(start_date.year, start_date.month, start_date.day, start_time.hour, start_time.min, start_time.sec, start_date.zone)
|
||||||
|
end_at = DateTime.new(end_date.year, end_date.month, end_date.day, end_time.hour, end_time.min, end_time.sec, end_date.zone)
|
||||||
|
end
|
||||||
|
{ start_at: start_at, end_at: end_at }
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user