1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-15 00:24:09 +01:00
fab-manager/app/services/export_service.rb

86 lines
3.1 KiB
Ruby
Raw Normal View History

# 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)
case type
when 'users/members'
2022-11-16 16:55:56 +01:00
last_export_members(query, key, extension)
when 'users/reservations'
2022-11-16 16:55:56 +01:00
last_export_reservations(query, key, extension)
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)
when %r{statistics/.*}
2022-11-16 16:55:56 +01:00
last_export_statistics(type, query, key, extension)
else
raise TypeError.new("unknown export type: #{type}")
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
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
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)
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') || Time.current
].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
end
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
end
end
end