Skip to content

Commit

Permalink
Track modified donations based on transaction updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanb777 committed Oct 31, 2024
1 parent 8f34cba commit fd55c5e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
11 changes: 4 additions & 7 deletions app/models/identity_tijuana/donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class Donation < ReadWrite

def self.import(donation_id, sync_id)
donation = Donation.find(donation_id)
donation.import(sync_id)
donation.import(sync_id, donation.transactions)
end

def import(_sync_id)
def import(_sync_id, transactions)
member = Member.find_by_external_id(:tijuana, user_id)
if member.present?
if member.ghosting_started?
Expand Down Expand Up @@ -68,13 +68,10 @@ def import(_sync_id)
raise
end
end
refund_transactions = transactions.filter_map { |t|
t.refund_of_id && t.successful ? [t.refund_of_id, t] : nil
}.to_h

transactions.each do |transaction|
next unless transaction.successful

refund_transaction = refund_transactions[transaction.id]
donation_hash = {
# member_action_id: nil,
member_id: member.id,
Expand All @@ -83,7 +80,7 @@ def import(_sync_id)
external_id: transaction.id,
# nonce: nil,
medium: payment_method,
refunded_at: refund_transaction ? refund_transaction.created_at : nil,
refunded_at: transaction.refund_of_id ? transaction.created_at : nil,
created_at: transaction.created_at,
updated_at: DateTime.now,
}
Expand Down
8 changes: 7 additions & 1 deletion app/models/identity_tijuana/transaction.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
module IdentityTijuana
class Transaction < ReadWrite
self.table_name = 'transactions'
belongs_to :donation, optional: true
belongs_to :donation, optional: false

scope :updated_transactions_all, ->(last_updated_at, last_id, exclude_from) {
where('updated_at > ? OR (updated_at = ? AND id > ?)',
last_updated_at, last_updated_at, last_id)
.where(updated_at: ...exclude_from)
}
end
end
50 changes: 34 additions & 16 deletions lib/identity_tijuana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,38 +173,56 @@ def self.fetch_donation_updates(sync_id)

def self.fetch_donation_updates_impl(sync_id)
started_at = DateTime.now
last_updated_at = get_redis_date('tijuana:donations:last_updated_at')
last_id = (Sidekiq.redis { |r| r.get 'tijuana:donations:last_id' } || 0).to_i
last_updated_at = get_redis_date('tijuana:transactions:last_updated_at')
last_id = (Sidekiq.redis { |r| r.get 'tijuana:transactions:last_id' } || 0).to_i
users_dependent_data_cutoff = get_redis_date('tijuana:users:dependent_data_cutoff')
updated_donations = IdentityTijuana::Donation.updated_donations(last_updated_at, last_id, users_dependent_data_cutoff)
updated_donations_all = IdentityTijuana::Donation.updated_donations_all(last_updated_at, last_id, users_dependent_data_cutoff)
updated_donations.each do |donation|
IdentityTijuana::Donation.import(donation.id, sync_id)

updated_transactions = IdentityTijuana::Transaction
.updated_transactions_all(
last_updated_at,
last_id,
users_dependent_data_cutoff
)
.includes(:donation)
.order(:updated_at, :id)
.limit(Settings.tijuana.pull_batch_amount || 100)

updated_transactions_all = IdentityTijuana::Transaction
.updated_transactions_all(
last_updated_at,
last_id,
users_dependent_data_cutoff
).count()

donations_with_transactions = updated_transactions.group_by(&:donation)

donations_with_transactions.each do |donation, transactions|
donation.import(sync_id, transactions)
end

unless updated_donations.empty?
set_redis_date('tijuana:donations:last_updated_at', updated_donations.last.updated_at)
Sidekiq.redis { |r| r.set 'tijuana:donations:last_id', updated_donations.last.id }
unless updated_transactions.empty?
set_redis_date('tijuana:transactions:last_updated_at', updated_transactions.last.updated_at)
Sidekiq.redis { |r| r.set 'tijuana:transactions:last_id', updated_transactions.last.id }
end

execution_time_seconds = ((DateTime.now - started_at) * 24 * 60 * 60).to_i
yield(
updated_donations.size,
updated_donations.pluck(:id),
updated_transactions.size,
updated_transactions.pluck(:id),
{
scope: 'tijuana:donations:last_updated_at',
scope: 'tijuana:transactions:last_id',
scope_limit: Settings.tijuana.pull_batch_amount,
from: last_updated_at,
to: updated_donations.empty? ? nil : updated_donations.last.updated_at,
from: last_id,
to: updated_transactions.empty? ? nil : updated_transactions.last.id,
started_at: started_at,
completed_at: DateTime.now,
execution_time_seconds: execution_time_seconds,
remaining_behind: updated_donations_all.count
remaining_behind: updated_transactions_all
},
false
)

updated_donations.count < updated_donations_all.count
updated_transactions.count < updated_transactions_all
end

def self.fetch_tagging_updates(sync_id)
Expand Down

0 comments on commit fd55c5e

Please sign in to comment.