2022-10-03 14:22:15 +02:00
|
|
|
# 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
|
2022-11-16 16:55:56 +01:00
|
|
|
def last_export(type, query = nil, key = nil, extension = nil)
|
2022-10-03 14:22:15 +02:00
|
|
|
case type
|
|
|
|
when 'users/members'
|
2022-11-16 16:55:56 +01:00
|
|
|
last_export_members(query, key, extension)
|
2022-10-03 14:22:15 +02:00
|
|
|
when 'users/reservations'
|
2022-11-16 16:55:56 +01:00
|
|
|
last_export_reservations(query, key, extension)
|
2023-10-02 15:14:23 +02:00
|
|
|
when 'users/subscriptions'
|
2022-11-16 16:55:56 +01:00
|
|
|
last_export_subscriptions(query, key, extension)
|
|
|
|
when 'availabilities/index'
|
|
|
|
last_export_availabilities(query, key, extension)
|
|
|
|
when %r{accounting/.*}
|
|
|
|
last_export_accounting(type, query, key, extension)
|
2022-10-12 14:19:59 +02:00
|
|
|
when %r{statistics/.*}
|
2022-11-16 16:55:56 +01:00
|
|
|
last_export_statistics(type, query, key, extension)
|
2022-10-03 14:22:15 +02:00
|
|
|
else
|
2023-10-02 15:14:23 +02:00
|
|
|
raise TypeError.new("unknown export type: #{type}")
|
2022-10-03 14:22:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-11-16 16:55:56 +01:00
|
|
|
def query_last_export(category, export_type, query = nil, key = nil, extension = nil)
|
|
|
|
export = Export.where(category: category, export_type: export_type)
|
2022-12-01 13:03:19 +01:00
|
|
|
export = export.where(query: query) unless query.nil?
|
|
|
|
export = export.where(key: key) unless key.nil?
|
|
|
|
export = export.where(extension: extension) unless extension.nil?
|
2022-11-16 16:55:56 +01:00
|
|
|
export
|
2022-10-03 14:22:15 +02:00
|
|
|
end
|
|
|
|
|
2022-11-16 16:55:56 +01:00
|
|
|
def last_export_subscriptions(query, key, extension)
|
|
|
|
query_last_export('users', 'subscriptions', query, key, extension)
|
|
|
|
.where('created_at > ?', Subscription.maximum('updated_at'))
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.first
|
2022-10-03 14:22:15 +02:00
|
|
|
end
|
|
|
|
|
2022-11-16 16:55:56 +01:00
|
|
|
def last_export_reservations(query, key, extension)
|
|
|
|
query_last_export('users', 'reservations', query, key, extension)
|
|
|
|
.where('created_at > ?', Reservation.maximum('updated_at'))
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_export_members(query, key, extension)
|
2022-10-03 14:22:15 +02:00
|
|
|
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'),
|
2023-02-14 13:10:58 +01:00
|
|
|
Subscription.maximum('updated_at') || Time.current
|
2022-10-03 14:22:15 +02:00
|
|
|
].max
|
|
|
|
|
2022-11-16 16:55:56 +01:00
|
|
|
query_last_export('users', 'members', query, key, extension)
|
|
|
|
.where('created_at > ?', last_update)
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_export_availabilities(query, key, extension)
|
|
|
|
query_last_export('availabilities', 'index', query, key, extension)
|
|
|
|
.where('created_at > ?', [Availability.maximum('updated_at'), Reservation.maximum('updated_at')].max)
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_export_accounting(type, query, key, extension)
|
|
|
|
query_last_export('accounting', type.split('/')[1], query, key, extension)
|
|
|
|
.where('created_at > ?', Invoice.maximum('updated_at'))
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.first
|
2022-10-03 14:22:15 +02:00
|
|
|
end
|
2022-10-12 14:19:59 +02:00
|
|
|
|
2022-11-16 16:55:56 +01:00
|
|
|
def last_export_statistics(type, query, key, extension)
|
|
|
|
query_last_export('statistics', type.split('/')[1], query, key, extension)
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.first
|
2022-10-12 14:19:59 +02:00
|
|
|
end
|
2022-10-03 14:22:15 +02:00
|
|
|
end
|
|
|
|
end
|