2021-02-02 21:04:53 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
|
|
|
class TicketPurchase < ApplicationRecord
|
2021-02-10 23:48:18 +00:00
|
|
|
include Model::TrxAssignable
|
2021-02-02 21:04:53 +00:00
|
|
|
setup_houid :tktpur
|
|
|
|
|
|
|
|
before_create :set_original_discount
|
|
|
|
|
|
|
|
belongs_to :event_discount
|
|
|
|
belongs_to :event
|
|
|
|
|
|
|
|
has_many :ticket_to_legacy_tickets
|
|
|
|
|
|
|
|
validates :event, presence: true
|
|
|
|
|
2021-04-07 21:43:58 +00:00
|
|
|
def to_id
|
|
|
|
::Jbuilder.new do |json|
|
|
|
|
json.id id
|
|
|
|
json.object 'ticket_purchase'
|
|
|
|
json.type 'trx_assignment'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-02 21:04:53 +00:00
|
|
|
def to_builder(*expand)
|
|
|
|
init_builder(*expand) do |json|
|
2021-04-07 21:43:58 +00:00
|
|
|
json.type 'trx_assignment'
|
|
|
|
|
2021-02-02 21:04:53 +00:00
|
|
|
json.original_discount do
|
|
|
|
json.percent original_discount
|
|
|
|
end if original_discount
|
|
|
|
|
|
|
|
json.amount do
|
2021-02-26 20:23:29 +00:00
|
|
|
json.cents amount
|
2021-02-02 21:04:53 +00:00
|
|
|
json.currency nonprofit.currency
|
|
|
|
end
|
|
|
|
|
2021-04-07 21:43:58 +00:00
|
|
|
json.add_builder_expansion :event, :event_discount, :nonprofit, :supporter
|
|
|
|
json.add_builder_expansion :ticket_to_legacy_tickets, enum_type: :expandable, json_attribute: 'tickets'
|
|
|
|
json.add_builder_expansion :trx, json_attribute: :transaction
|
|
|
|
|
|
|
|
# if expand.include? :tickets
|
|
|
|
# json.tickets ticket_to_legacy_tickets do |i|
|
|
|
|
# i.to_builder.attributes!
|
|
|
|
# end
|
|
|
|
# else
|
|
|
|
# json.tickets ticket_to_legacy_tickets.pluck(:id)
|
|
|
|
# end
|
2021-02-02 21:04:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-02 23:48:05 +00:00
|
|
|
def publish_created
|
2021-04-07 21:43:58 +00:00
|
|
|
Houdini.event_publisher.announce(:ticket_purchase_created, to_event('ticket_purchase.created', :event, :nonprofit, :supporter, :trx, :event_discount, :ticket_to_legacy_tickets).attributes!)
|
2021-02-02 23:48:05 +00:00
|
|
|
end
|
|
|
|
|
2021-02-02 21:04:53 +00:00
|
|
|
private
|
|
|
|
def set_original_discount
|
|
|
|
original_discount = event_discount.nil? ? 0 : event_discount.percent
|
|
|
|
end
|
|
|
|
end
|