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
|
2019-07-30 11:43:51 +02:00
|
|
|
mime_type = if @export.extension == 'xlsx'
|
|
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
|
|
elsif @export.extension == 'csv'
|
|
|
|
'text/csv'
|
|
|
|
else
|
|
|
|
'application/octet-stream'
|
|
|
|
end
|
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),
|
2019-07-30 11:43:51 +02:00
|
|
|
type: mime_type,
|
2019-01-16 16:28:25 +01:00
|
|
|
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-07-31 15:47:02 +02:00
|
|
|
exports = Export.where(
|
|
|
|
category: params[:category],
|
|
|
|
export_type: params[:type],
|
|
|
|
query: params[:query],
|
|
|
|
key: params[:key],
|
|
|
|
extension: params[:extension]
|
|
|
|
)
|
2019-07-30 11:43:51 +02:00
|
|
|
export = retrieve_last_export(exports, params[:category], params[:type])
|
2016-07-28 10:28:43 +02:00
|
|
|
|
2019-07-30 11:43:51 +02:00
|
|
|
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 retrieve_last_export(export, category, type)
|
|
|
|
case category
|
|
|
|
when 'users'
|
|
|
|
case 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'
|
2020-01-14 14:33:00 +01:00
|
|
|
export = export.where('created_at > ?', User.members.maximum('updated_at'))
|
2019-01-16 16:28:25 +01:00
|
|
|
else
|
2019-07-30 11:43:51 +02:00
|
|
|
raise ArgumentError, "Unknown export users/#{type}"
|
2017-03-02 12:34:28 +01:00
|
|
|
end
|
2019-07-30 11:43:51 +02:00
|
|
|
when 'availabilities'
|
|
|
|
case type
|
2019-01-16 16:28:25 +01:00
|
|
|
when 'index'
|
2019-05-07 12:24:51 +02:00
|
|
|
export = export.where('created_at > ?', [Availability.maximum('updated_at'), Reservation.maximum('updated_at')].max)
|
2019-01-16 16:28:25 +01:00
|
|
|
else
|
2019-07-30 11:43:51 +02:00
|
|
|
raise ArgumentError, "Unknown type availabilities/#{type}"
|
|
|
|
end
|
|
|
|
when 'accounting'
|
|
|
|
case type
|
2019-09-16 14:39:47 +02:00
|
|
|
when 'acd'
|
2019-07-30 11:43:51 +02:00
|
|
|
export = export.where('created_at > ?', Invoice.maximum('updated_at'))
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Unknown type accounting/#{type}"
|
2016-07-28 10:28:43 +02:00
|
|
|
end
|
2016-07-27 15:42:29 +02:00
|
|
|
else
|
2019-07-30 11:43:51 +02:00
|
|
|
raise ArgumentError, "Unknown category #{category}"
|
2016-07-27 15:42:29 +02:00
|
|
|
end
|
2019-07-30 11:43:51 +02:00
|
|
|
export.last
|
2016-07-27 15:42:29 +02:00
|
|
|
end
|
|
|
|
|
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
|