Verifies that all the events related to ticket_creation are fired

This commit is contained in:
Eric Schultz 2021-02-02 17:48:05 -06:00 committed by Eric Schultz
parent 6c233bc2f9
commit 512672a174
8 changed files with 59 additions and 17 deletions

View file

@ -1,10 +0,0 @@
# 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 TicketListener < ApplicationListener
def self.ticket_create(tickets, charge, user=nil)
TicketMailer.followup(tickets.map{|i| i.id}, charge && charge.id).deliver_later
TicketMailer.receipt_admin(tickets.map{|i| i.id}, user && user.id).deliver_later
end
end

View file

@ -0,0 +1,12 @@
# 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 TicketMailingListener < ApplicationListener
def self.ticket_purchase_created(ticket_purchase)
tickets_ids = ticket_purchase.tickets_to_legacy_tickets.joins(:ticket).map {|i| i.ticket.id}
charge = ticket_purchase.tickets_to_legacy_tickets.joins(:ticket).first.charge
TicketMailer.followup(tickets_ids, charge && charge.id).deliver_later
TicketMailer.receipt_admin(tickets_ids, nil).deliver_later
end
end

View file

@ -5,6 +5,7 @@
class TicketPurchase < ApplicationRecord
include Model::Houidable
include Model::Jbuilder
include Model::Eventable
setup_houid :tktpur
add_builder_expansion :event, :nonprofit, :supporter
@ -52,6 +53,10 @@ class TicketPurchase < ApplicationRecord
end
end
def publish_created
Houdini.event_publisher.announce(:ticket_purchase_created, to_event('ticket_purchase.created', :event, :nonprofit, :supporter, :trx, :event_discount).attributes!)
end
private
def set_original_discount
original_discount = event_discount.nil? ? 0 : event_discount.percent

View file

@ -5,6 +5,8 @@
class TicketToLegacyTicket < ApplicationRecord
include Model::Houidable
include Model::Jbuilder
include Model::Eventable
belongs_to :ticket_purchase
belongs_to :ticket
@ -43,4 +45,14 @@ class TicketToLegacyTicket < ApplicationRecord
end
end
def publish_created
Houdini.event_publisher.announce(:ticket_created, to_event('ticket.created',
:ticket_purchase,
:ticket_level,
:supporter,
:event,
:nonprofit,
:event_discount).attributes!)
end
end

View file

@ -4,9 +4,15 @@
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
class Transaction < ApplicationRecord
include Model::Houidable
include Model::Jbuilder
include Model::Eventable
setup_houid :trx
add_builder_expansion :nonprofit, :supporter
belongs_to :supporter
has_one :nonprofit, through: :supporter
has_many :transaction_assignments
has_many :ticket_purchases, through: :transaction_assignments, source: :assignable, source_type: 'TicketPurchase'
@ -15,15 +21,19 @@ class Transaction < ApplicationRecord
def to_builder(*expand)
Jbuilder.new do |json|
init_builder(*expand) do |json|
json.(self, :id)
json.object 'transaction'
json.supporter supporter.id
json.nonprofit supporter.nonprofit.id
json.amount do
json.value_in_cents amount || 0
json.currency supporter.nonprofit.currency
json.currency nonprofit.currency
end
end
end
def publish_created
Houdini.event_publisher.announce(:transaction_created,
to_event('transaction.created', :nonprofit, :supporter).attributes!)
end
end

View file

@ -8,5 +8,5 @@ Wisper.clear if Rails.env.development?
Rails.application.config.houdini.listeners.push(NonprofitMailerListener,
CreditCardPaymentListener,
SepaPaymentListener,
TicketListener
TicketMailingListener
)

View file

@ -49,6 +49,7 @@ module InsertTickets
result = {}
trx = entities[:supporter_id].transactions.create(amount: gross_amount)
if gross_amount > 0
# Create offsite payment for tickets
if data[:kind] == 'offsite'
@ -90,10 +91,17 @@ module InsertTickets
end
end
ticket_purchase = trx.ticket_purchases.create(event: entities[:event_id])
# Generate the bid ids
data['tickets'] = generate_bid_ids(entities[:event_id].id, tl_entities)
result['tickets'] = generated_ticket_entities(data['tickets'], result, entities)
result['tickets'].each do |legacy_ticket|
legacy_ticket.quantity.times do
ticket_purchase.ticket_to_legacy_tickets.create(ticket: legacy_ticket)
end
end
# Create the activity rows for the tickets
InsertActivities.for_tickets(result['tickets'].map(&:id))
@ -101,7 +109,10 @@ module InsertTickets
ticket_ids = result['tickets'].map(&:id)
charge_id = result['charge'] ? result['charge'].id : nil
Houdini.event_publisher.announce(:ticket_create, result['tickets'], result['charge'])
ticket_purchase.ticket_to_legacy_tickets.each{|i| i.publish_created}
ticket_purchase.publish_created
trx.publish_created
result
end

View file

@ -289,7 +289,9 @@ describe InsertTickets do
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:supporter_created, anything)
expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything)
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_create, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_created, anything).once.ordered
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_purchase_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args).ordered
result = InsertTickets.create(tickets: [{ quantity: 1, ticket_level_id: ticket_level.id }], nonprofit_id: nonprofit.id, supporter_id: supporter.id, token: source_token.token, event_id: event.id, kind: 'offsite', offsite_payment: { kind: 'check', check_number: 'fake_checknumber' }, current_user: user)
expected = generate_expected_tickets(payment_id: result['payment'].id,