1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/services/prepaid_pack_service.rb

33 lines
1.1 KiB
Ruby
Raw Normal View History

2021-06-21 17:39:48 +02:00
# frozen_string_literal: true
# Provides methods for PrepaidPack
class PrepaidPackService
2021-06-25 17:24:34 +02:00
class << self
def list(filters)
packs = PrepaidPack.where(nil)
2021-06-21 17:39:48 +02:00
2021-06-25 17:24:34 +02:00
packs = packs.where(group_id: filters[:group_id]) if filters[:group_id].present?
packs = packs.where(priceable_id: filters[:priceable_id]) if filters[:priceable_id].present?
packs = packs.where(priceable_type: filters[:priceable_type]) if filters[:priceable_type].present?
2021-06-21 17:39:48 +02:00
2021-06-25 17:24:34 +02:00
if filters[:disabled].present?
state = filters[:disabled] == 'false' ? [nil, false] : true
packs = packs.where(disabled: state)
end
packs
end
2021-06-29 11:14:36 +02:00
# return the not expired packs for the given item bought by the given user
def user_packs(user, priceable)
StatisticProfilePrepaidPack
.includes(:prepaid_pack)
.references(:prepaid_packs)
.where('statistic_profile_id = ?', user.statistic_profile.id)
.where('expires_at > ?', DateTime.current)
.where('prepaid_packs.priceable_id = ?', priceable.id)
.where('prepaid_packs.priceable_type = ?', priceable.class.name)
2021-06-25 17:24:34 +02:00
end
2021-06-21 17:39:48 +02:00
end
end