Class: OutletPurchase::OpportunityUnsyncHandler

Inherits:
Object
  • Object
show all
Defined in:
app/subscribers/outlet_purchase/opportunity_unsync_handler.rb

Overview

Subscribes to Events::OutletPurchaseRejected. The inverse of
OpportunitySyncHandler: an attribution that had been
accepted is withdrawn, so everything it wrote to the opportunity comes back
off.

  • recomputes the channel from whatever is still accepted, because
    stamp_purchase_outlet refuses to overwrite and a stale value would block
    the next accepted row from recording the real one
  • restores the state this purchase corrected, if it corrected one and nothing
    else now justifies the win
  • recomputes value, since the revenue is no longer ours to count

Synchronous for the same reason as its counterpart — the reviewer lands back
on a page showing the state.

The ad conversion is deliberately NOT retracted. That needs Google's
conversion adjustment API and a decision about whether withdrawing a signal
that already moved bidding is better than leaving it. See the ledger.

Wired in config/initializers/event_store.rb.

Instance Method Summary collapse

Instance Method Details

#call(event) ⇒ void

This method returns an undefined value.

Parameters:

  • event (RubyEventStore::Event)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/subscribers/outlet_purchase/opportunity_unsync_handler.rb', line 26

def call(event)
  purchase = CustomerOutletPurchase.find_by(id: event.data[:customer_outlet_purchase_id])
  return unless purchase

  # Same race as its counterpart, mirrored: a concurrent verification can move
  # the row back to `pending` between the rejection's commit and this handler,
  # and unwinding then would strip a live attribution.
  purchase.with_lock do
    purchase.reload
    next unless purchase.rejected? && purchase.opportunity

    purchase.restamp_purchase_outlet
    purchase.undo_written_off_correction
    purchase.opportunity.calculate_value
  end
rescue ActiveRecord::RecordNotFound
  nil
end