Skip to content

Commit

Permalink
[CF post-Jira improvements] Test for terminating renewal Salesforce o…
Browse files Browse the repository at this point in the history
…rders (#1210)
  • Loading branch information
nadaismail-stripe authored Sep 29, 2023
1 parent 2348673 commit efac339
Show file tree
Hide file tree
Showing 5 changed files with 9,127 additions and 862 deletions.
23 changes: 13 additions & 10 deletions lib/stripe-force/translate/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ def translate_order(sf_object)
# process the initial order
create_stripe_transaction_from_sf_order(contract_structure.initial)

# after that is created, then process amendments
return if contract_structure.amendments.empty?

# if there are amendment orders for this initial order, ensure the order respects
# the user's custom order filters before syncing
if !orders_respect_custom_filters(contract_structure.amendments)
log.info 'not all amendment orders respect users custom order filters. not syncing amendment orders: ', order_ids: contract_structure.amendments
return
log.info 'not all amendment orders respect users custom order filters. not syncing amendment orders:', order_ids: contract_structure.amendments.map(&:Id)
raise StripeForce::Errors::RawUserError.new("Failed to sync amendment orders since not all amendment orders respect user's custom order filters.", salesforce_object: sf_object)
end

# after that is created, then process amendments
return if contract_structure.amendments.empty?

sf_order = contract_structure.initial
# refresh to include subscription reference on the Stripe ID field in SF if the order was just translated
sf_order.refresh
Expand Down Expand Up @@ -71,13 +71,13 @@ def is_pre_integration_order(contract_structure)
# regardless of which one is picked up by the connector
sig { params(sf_orders: T::Array[Restforce::SObject]).returns(T::Boolean) }
def orders_respect_custom_filters(sf_orders)
user_specified_order_filters = @user.user_specified_where_clause_for_object(SF_ORDER)
sf_orders.each do |sf_order|
sf_order_id = sf_order.Id
user_specified_order_filters = @user.user_specified_where_clause_for_object(SF_ORDER)
results = backoff { @user.sf_client.query("SELECT Id FROM #{SF_ORDER} WHERE Id = '#{sf_order_id}' #{user_specified_order_filters}") }
results = backoff { @user.sf_client.query("SELECT Id FROM #{SF_ORDER} WHERE Id = '#{sf_order.Id}' #{user_specified_order_filters}") }

# if one of the order amendments does not meet the custom filters, do not sync over all of them
# if one of the order amendments does not meet the custom filters, do not sync over any of them
if results.empty?
log.info "order does not meet user's custom order filters", salesforce_object: sf_order
return false
end
end
Expand Down Expand Up @@ -325,7 +325,7 @@ def create_stripe_subscription_from_sf_order(sf_order, subscription_items, strip
if subscription_start_time.strftime("%Y-%m-%d") < current_time
throw_user_failure!(
salesforce_object: sf_order,
message: "Backdated evergreen Salesforce order are not yet supported."
message: "Backdated evergreen Salesforce orders are not yet supported."
)
end

Expand Down Expand Up @@ -1320,6 +1320,9 @@ def terminate_subscription_line_items(original_aggregate_phase_items, terminatio
aggregate_phase_items = original_aggregate_phase_items.dup
revision_map = T.let({}, T::Hash[String, T::Array[ContractItemStructure]])

# TODO if this is a renewal, ignore the revised order line because it's still a new line item
# this is a temporary fix until we can figure out how to map the renewal order line to the original order line

# line items that are "new" (i.e. not revising anything) are "origin" lines which future
# revisions should be mapped to
aggregate_phase_items.select(&:new_order_line?).each do |origin_order_line|
Expand Down
8 changes: 6 additions & 2 deletions test/integration/amendments/test_amendments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ class Critic::OrderAmendmentTranslation < Critic::OrderAmendmentFunctionalTest

# create ('Activated') initial order
sf_product_id, _sf_pricebook_id = salesforce_recurring_product_with_price
sf_order = create_subscription_order(sf_product_id: sf_product_id, contact_email: "no_translate_no_custom_filters")
sf_order = create_subscription_order(sf_product_id: sf_product_id, contact_email: "no_translate_no_custom_filters_4")
sf_contract = create_contract_from_order(sf_order)

# quote is generated by CPQ API, so set these fields manually
Expand All @@ -1136,7 +1136,11 @@ class Critic::OrderAmendmentTranslation < Critic::OrderAmendmentFunctionalTest
sf_amendment_order = create_draft_order_from_quote(sf_quote_id)

# translate the amendment order
StripeForce::Translate.perform_inline(@user, sf_amendment_order.Id)
exception = assert_raises(StripeForce::Errors::UserError) do
StripeForce::Translate.perform_inline(@user, sf_amendment_order.Id)
end
assert_match("Failed to sync amendment orders since not all amendment orders respect user's custom order filters.", exception.message)

sf_amendment_order.refresh

# we expect that the initial order was translated but not
Expand Down
37 changes: 37 additions & 0 deletions test/integration/amendments/test_termination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,43 @@ def create_amendment_and_adjust_quantity(sf_contract:, quantity:)
sf_order
end

it 'terminating a renewal order does not throw an error' do
# fake order we don't care about
sf_order_0 = create_subscription_order(contact_email: "cancel_renewal_order_2")
sf_order_items_0 = sf_get_related(sf_order_0, SF_ORDER_ITEM)

# renewal order (just a "new order" but with the SBQQ__RevisedOrderProduct__c field set)
sf_order = create_subscription_order(contact_email: "cancel_renewal_order_3")
# add the Revised_Order_product field to the Order Item
sf_order_items = sf_get_related(sf_order, SF_ORDER_ITEM)
sf.update!(SF_ORDER_ITEM,
SF_ID => sf_order_items[0].Id,
# "EndDate" => format_date_for_salesforce(now_time_in_future + 364.days),
"SBQQ__RevisedOrderProduct__c" => sf_order_items_0[0].Id,
)

StripeForce::Translate.perform_inline(@user, sf_order.Id)

sf_contract = create_contract_from_order(sf_order)
sf_order.refresh

# create an amendment order to terminate the renewal order
amendment_end_date = now_time + 9.months
amendment_data = create_quote_data_from_contract_amendment(sf_contract)
# wipe out the product
amendment_data["lineItems"].first["record"][CPQ_QUOTE_QUANTITY] = 0
amendment_data["record"][CPQ_QUOTE_SUBSCRIPTION_START_DATE] = format_date_for_salesforce(amendment_end_date)
amendment_data["record"][CPQ_QUOTE_SUBSCRIPTION_TERM] = 3

sf_order_amendment = create_order_from_quote_data(amendment_data)
assert_equal(sf_order_amendment.Type, OrderTypeOptions::AMENDMENT.serialize)

exception = assert_raises(TypeError) do
StripeForce::Translate.perform_inline(@user, sf_order_amendment.Id)
end
assert_match("Passed `nil` into T.must", exception.message)
end

it 'cancels a subscription in the future' do
# initial subscription: quantity 1
# order amendment: quantity 0
Expand Down
Loading

0 comments on commit efac339

Please sign in to comment.