1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/controllers/api/exports_controller.rb

60 lines
1.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# API Controller for resources of type Export
# Export are used to download data tables in offline files
class API::ExportsController < API::ApiController
before_action :authenticate_user!
before_action :set_export, only: [:download]
def download
authorize @export
if FileTest.exist?(@export.file)
send_file File.join(Rails.root, @export.file),
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
disposition: 'attachment'
else
render text: I18n.t('errors.messages.export_not_found'), status: :not_found
end
end
def status
authorize Export
export = Export.where(category: params[:category], export_type: params[:type], query: params[:query], key: params[:key])
if params[:category] == 'users'
case params[:type]
when 'subscriptions'
export = export.where('created_at > ?', Subscription.maximum('updated_at'))
when 'reservations'
export = export.where('created_at > ?', Reservation.maximum('updated_at'))
when 'members'
export = export.where('created_at > ?', User.with_role(:member).maximum('updated_at'))
else
raise ArgumentError, "Unknown export users/#{params[:type]}"
2017-03-02 12:34:28 +01:00
end
elsif params[:category] == 'availabilities'
2017-03-02 12:34:28 +01:00
case params[:type]
when 'index'
export = export.where('created_at > ?', Availability.maximum('updated_at'))
else
raise ArgumentError, "Unknown type availabilities/#{params[:type]}"
end
end
export = export.last
if export.nil? || !FileTest.exist?(export.file)
render json: { exists: false, id: nil }, status: :ok
else
render json: { exists: true, id: export.id }, status: :ok
end
end
private
def set_export
@export = Export.find(params[:id])
end
end