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