mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-20 14:54:15 +01:00
availability public api
This commit is contained in:
parent
fb26a2e358
commit
e9c1584968
@ -4,14 +4,13 @@
|
||||
# Controller used in the public calendar global
|
||||
##
|
||||
|
||||
Application.Controllers.controller "CalendarController", ["$scope", "$uibModal", "moment", "Availability", 'Slot', 'Setting', 'growl', 'dialogs', 'bookingWindowStart', 'bookingWindowEnd', '_t', 'uiCalendarConfig', 'CalendarConfig'
|
||||
($scope, $uibModal, moment, Availability, Slot, Setting, growl, dialogs, bookingWindowStart, bookingWindowEnd, _t, uiCalendarConfig, CalendarConfig) ->
|
||||
Application.Controllers.controller "CalendarController", ["$scope", "$state", "$uibModal", "moment", "Availability", 'Slot', 'Setting', 'growl', 'dialogs', 'bookingWindowStart', 'bookingWindowEnd', '_t', 'uiCalendarConfig', 'CalendarConfig'
|
||||
($scope, $state, $uibModal, moment, Availability, Slot, Setting, growl, dialogs, bookingWindowStart, bookingWindowEnd, _t, uiCalendarConfig, CalendarConfig) ->
|
||||
|
||||
|
||||
### PRIVATE STATIC CONSTANTS ###
|
||||
|
||||
|
||||
|
||||
### PUBLIC SCOPE ###
|
||||
|
||||
## bind the availabilities slots with full-Calendar events
|
||||
@ -22,11 +21,30 @@ Application.Controllers.controller "CalendarController", ["$scope", "$uibModal",
|
||||
|
||||
## fullCalendar (v2) configuration
|
||||
$scope.calendarConfig = CalendarConfig
|
||||
slotEventOverlap: false
|
||||
header:
|
||||
left: 'month agendaWeek agendaDay'
|
||||
center: 'title'
|
||||
right: 'today prev,next'
|
||||
minTime: moment.duration(moment(bookingWindowStart.setting.value).format('HH:mm:ss'))
|
||||
maxTime: moment.duration(moment(bookingWindowEnd.setting.value).format('HH:mm:ss'))
|
||||
eventClick: (event, jsEvent, view)->
|
||||
calendarEventClickCb(event, jsEvent, view)
|
||||
viewRender: (view, element) ->
|
||||
if view.type == 'agendaDay'
|
||||
uiCalendarConfig.calendars.calendar.fullCalendar('refetchEvents')
|
||||
|
||||
|
||||
### PRIVATE SCOPE ###
|
||||
|
||||
calendarEventClickCb = (event, jsEvent, view) ->
|
||||
console.log event
|
||||
## current calendar object
|
||||
calendar = uiCalendarConfig.calendars.calendar
|
||||
if event.available_type == 'machines'
|
||||
calendar.fullCalendar('gotoDate', event.start)
|
||||
calendar.fullCalendar('changeView', 'agendaDay')
|
||||
else
|
||||
if event.available_type == 'event'
|
||||
$state.go('app.public.events_show', {id: event.event_id})
|
||||
]
|
||||
|
@ -17,9 +17,25 @@ class API::AvailabilitiesController < API::ApiController
|
||||
def public
|
||||
start_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:start])
|
||||
end_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:end]).end_of_day
|
||||
@availabilities = Availability.includes(:machines,:tags,:trainings).where.not(available_type: 'event')
|
||||
.where('start_at >= ? AND end_at <= ?', start_date, end_date)
|
||||
render :index
|
||||
if in_same_day(start_date, end_date)
|
||||
@training_availabilities = Availability.includes(:tags, :trainings).where.not(available_type: 'machines')
|
||||
.where('start_at >= ? AND end_at <= ?', start_date, end_date)
|
||||
@machine_availabilities = Availability.includes(:tags, :machines).where(available_type: 'machines')
|
||||
.where('start_at >= ? AND end_at <= ?', start_date, end_date)
|
||||
@machine_slots = []
|
||||
@machine_availabilities.each do |a|
|
||||
a.machines.each do |machine|
|
||||
((a.end_at - a.start_at)/SLOT_DURATION.minutes).to_i.times do |i|
|
||||
slot = Slot.new(start_at: a.start_at + (i*SLOT_DURATION).minutes, end_at: a.start_at + (i*SLOT_DURATION).minutes + SLOT_DURATION.minutes, availability_id: a.id, availability: a, machine: machine, title: machine.name)
|
||||
@machine_slots << slot
|
||||
end
|
||||
end
|
||||
end
|
||||
@availabilities = [].concat(@training_availabilities).concat(@machine_slots)
|
||||
else
|
||||
@availabilities = Availability.includes(:tags, :machines, :trainings, :event)
|
||||
.where('start_at >= ? AND end_at <= ?', start_date, end_date)
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
@ -188,4 +204,8 @@ class API::AvailabilitiesController < API::ApiController
|
||||
def is_subscription_year(user)
|
||||
user.subscription and user.subscription.plan.interval == 'year' and user.subscription.expired_at >= Time.now
|
||||
end
|
||||
|
||||
def in_same_day(start_date, end_date)
|
||||
(end_date.to_date - start_date.to_date).to_i == 1
|
||||
end
|
||||
end
|
||||
|
@ -42,6 +42,8 @@ class Availability < ActiveRecord::Base
|
||||
def title
|
||||
if available_type == 'machines'
|
||||
machines.map(&:name).join(' - ')
|
||||
elsif available_type == 'event'
|
||||
event.name
|
||||
else
|
||||
trainings.map(&:name).join(' - ')
|
||||
end
|
||||
|
31
app/views/api/availabilities/public.json.jbuilder
Normal file
31
app/views/api/availabilities/public.json.jbuilder
Normal file
@ -0,0 +1,31 @@
|
||||
json.array!(@availabilities) do |availability|
|
||||
json.id availability.id
|
||||
json.title availability.title
|
||||
json.start availability.start_at.iso8601
|
||||
json.end availability.end_at.iso8601
|
||||
json.backgroundColor 'white'
|
||||
# availability object
|
||||
if availability.try(:available_type)
|
||||
if availability.available_type == 'event'
|
||||
json.event_id availability.event.id
|
||||
end
|
||||
if availability.available_type == 'training'
|
||||
json.training_id availability.trainings.first.id
|
||||
end
|
||||
json.available_type availability.available_type
|
||||
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
|
||||
# machine slot object
|
||||
else
|
||||
json.borderColor machines_slot_border_color(availability)
|
||||
json.tag_ids availability.availability.tag_ids
|
||||
json.tags availability.availability.tags do |t|
|
||||
json.id t.id
|
||||
json.name t.name
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user