Skip to content

Instantly share code, notes, and snippets.

@TheBrotherFromASouthernMother
Last active July 3, 2024 22:29
Show Gist options
  • Save TheBrotherFromASouthernMother/29abe54ce62a82a15562fb749361d774 to your computer and use it in GitHub Desktop.
Save TheBrotherFromASouthernMother/29abe54ce62a82a15562fb749361d774 to your computer and use it in GitHub Desktop.
# Fetches only the charges that were affected by the bug, and who do not have the use_npsp_recurring_model feature enabled
affected_charges = Charge.where(id: affected_charges_ids).where.not(nonprofit_id: affected_npo_ids_with_npsp_recurring_enabled)
# Filters to select only the charges whose nonprofits also have a salesforce config
charges_with_salesforce_config = affected_charges.select { |charge| charge.nonprofit.salesforce_config }
# Fetches line items for affected charges
line_items = charges_with_salesforce_config.map do |charge|
line_items = charge.order.line_items
non_nil_line_items = line_items.filter { |li| !li.nil? }
end
# finds the external sync records for the lines that have already been synced
flattened_line_items = line_items.flatten
external_syncs = flattened_line_items.map { |li| li.salesforce_external_syncs }
flattened_external_syncs = external_syncs.flatten
synced_external_syncs = flattened_external_syncs.select { |sync| sync.sf_id.present? }
## Updates the opportunities amounts in Salesforce based on the line items
sf_response = synced_external_syncs.map do |sync|
salesforce_client = sync.nonprofit.salesforce_config.client
line_item = sync.line_item
sf_response = salesforce_client.update(
"Opportunity",
id: sync.sf_id,
Amount: line_item.amount + line_item.covered_transaction_fee_amount
)
sf_response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment