Skip to content

Commit

Permalink
[CF RUN Issue] Raise error if quote / quote line associations do not …
Browse files Browse the repository at this point in the history
…match Order / Order Item coupon count (#1236)
  • Loading branch information
nadaismail-stripe authored Nov 9, 2023
1 parent 3b37370 commit 8472d9a
Show file tree
Hide file tree
Showing 13 changed files with 23,260 additions and 12,203 deletions.
29 changes: 28 additions & 1 deletion lib/stripe-force/translate/coupon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,18 @@ def get_salesforce_stripe_coupons_associated_to_sf_object(sf_client:, sf_object:
lookup_field = 'Order_Item__c'
else
# this should never happen since coupons can only be tied to an order or order item
raise Integrations::Errors::ImpossibleState.new("unsupported sf object type for coupons: #{source_sf_record_type}")
raise Integrations::Errors::ImpossibleState.new("Unsupported sf object type for coupons: #{source_sf_record_type}")
end

# check if there are any coupon associations to this order or order item
coupon_data = backoff { sf_client.query("Select #{SF_ID} from #{prefixed_stripe_field(ORDER_SF_STRIPE_COUPON)} where #{prefixed_stripe_field(lookup_field)} = '#{sf_object.Id}'") }

associations = get_quote_associations_for_sf_object(sf_client: sf_client, sf_object: sf_object)
if associations.size != coupon_data.size
log.info 'found a different number of coupons associations for sf object', salesforce_object: sf_object, sf_association_count: associations.size, sf_order_coupon_count: coupon_data.size
raise Integrations::Errors::ImpossibleInternalError.new("The sf object has a different number of quote / quote line associations than Order coupons.")
end

# there could be multiple coupons associated with a single order or order line
coupons = coupon_data.map do |data|
backoff { sf_client.find(prefixed_stripe_field(ORDER_SF_STRIPE_COUPON), data.Id) }
Expand All @@ -127,4 +133,25 @@ def get_salesforce_stripe_coupons_associated_to_sf_object(sf_client:, sf_object:
coupons
end
end

def get_quote_associations_for_sf_object(sf_client:, sf_object:)
source_sf_record_type = sf_object.sobject_type
if source_sf_record_type == SF_ORDER
lookup_field = 'Quote__c'
lookup_object = QUOTE_SF_STRIPE_COUPON_ASSOCIATION
lookup_field_id = sf_object[CPQ_QUOTE]
elsif source_sf_record_type == SF_ORDER_ITEM
lookup_field = 'Quote_Line__c'
lookup_object = QUOTE_LINE_SF_STRIPE_COUPON_ASSOCIATION
lookup_field_id = sf_object[CPQ_QUOTE_LINE]
else
# this should never happen since coupons can only be tied to an order or order item
raise Integrations::Errors::ImpossibleState.new("Unsupported sf object type for coupons: #{source_sf_record_type}")
end

# check if there are any coupon associations to this order or order item
qla_data = backoff { sf_client.query("Select #{SF_ID} from #{prefixed_stripe_field(lookup_object)} where #{prefixed_stripe_field(lookup_field)} = '#{lookup_field_id}'") }

qla_data
end
end
2 changes: 1 addition & 1 deletion test/integration/translate/test_coupon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Critic::CouponTranslation < Critic::VCRTest
sf_product_id, _sf_pricebook_id = salesforce_recurring_product_with_price

# create a CPQ quote
sf_quote_id = create_salesforce_quote(sf_account_id: sf_account_id, contact_email: "order_and_order_line_when_quote_ordered", additional_quote_fields: {
sf_quote_id = create_salesforce_quote(sf_account_id: sf_account_id, contact_email: "order_and_order_line_when_quote_ordered_1", additional_quote_fields: {
CPQ_QUOTE_SUBSCRIPTION_START_DATE => now_time_formatted_for_salesforce,
CPQ_QUOTE_SUBSCRIPTION_TERM => TEST_DEFAULT_CONTRACT_TERM,
})
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8472d9a

Please sign in to comment.