mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-02 13:24:20 +01:00
56 lines
1.7 KiB
Ruby
56 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Provides helper methods for Exports resources and properties
|
|
class ExportService
|
|
class << self
|
|
# Check if the last export of the provided type is still accurate or if it must be regenerated
|
|
def last_export(type)
|
|
case type
|
|
when 'users/members'
|
|
last_export_members
|
|
when 'users/reservations'
|
|
last_export_reservations
|
|
when 'users/subscription'
|
|
last_export_subscriptions
|
|
when %r{statistics/.*}
|
|
last_export_statistics(type.split('/')[1])
|
|
else
|
|
raise TypeError "unknown export type: #{type}"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def last_export_subscriptions
|
|
Export.where(category: 'users', export_type: 'subscriptions')
|
|
.where('created_at > ?', Subscription.maximum('updated_at'))
|
|
.last
|
|
end
|
|
|
|
def last_export_reservations
|
|
Export.where(category: 'users', export_type: 'reservations')
|
|
.where('created_at > ?', Reservation.maximum('updated_at'))
|
|
.last
|
|
end
|
|
|
|
def last_export_members
|
|
last_update = [
|
|
User.members.maximum('updated_at'),
|
|
Profile.where(user_id: User.members).maximum('updated_at'),
|
|
InvoicingProfile.where(user_id: User.members).maximum('updated_at'),
|
|
StatisticProfile.where(user_id: User.members).maximum('updated_at'),
|
|
Subscription.maximum('updated_at') || DateTime.current
|
|
].max
|
|
|
|
Export.where(category: 'users', export_type: 'members')
|
|
.where('created_at > ?', last_update)
|
|
.last
|
|
end
|
|
|
|
def last_export_statistics(type)
|
|
Export.where(category: 'statistics', export_type: type, query: params[:body], key: params[:type_key])
|
|
.last
|
|
end
|
|
end
|
|
end
|