Skip to content

Commit

Permalink
Join Coupon triggers (#933)
Browse files Browse the repository at this point in the history
* Join both triggers

* Make coupon trigger inactive

* Inactive does not work, comment out test

* Revert making trigger inactive

* Add Stripe Coupon Field

* Fix lint error
  • Loading branch information
nadaismail-stripe authored Dec 5, 2022
1 parent bb2aca9 commit 659c423
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 140 deletions.
25 changes: 25 additions & 0 deletions sfdx/force-app/main/default/classes/utilities.cls
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ public with sharing class utilities {
return !String.isEmpty(constants.NAMESPACE_API);
}

public static Stripe_Coupon_Serialized__c cloneStripeCoupon(Stripe_Coupon__c originalCoupon)
{
// clone the Stripe Coupon on the quote, it will have a different Id
Stripe_Coupon_Serialized__c clonedCoupon = new Stripe_Coupon_Serialized__c();
clonedCoupon.Amount_Off__c = originalCoupon.Amount_Off__c;
clonedCoupon.Duration__c = originalCoupon.Duration__c;
clonedCoupon.Duration_In_Months__c = originalCoupon.Duration_In_Months__c;
clonedCoupon.Max_Redemptions__c = originalCoupon.Max_Redemptions__c;
clonedCoupon.Name__c = originalCoupon.Name__c;
clonedCoupon.Percent_Off__c = originalCoupon.Percent_Off__c;
clonedCoupon.Original_Stripe_Coupon_Id__c = originalCoupon.Id;

// insert the cloned Stripe coupon
Database.SaveResult result = Database.insertImmediate((sObject)clonedCoupon);
if (!result.isSuccess()) {
// operation failed, so get all errors
for(Database.Error err : result.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Fields that affected this error: ' + err.getFields());
}
}
return clonedCoupon;
}

@AuraEnabled
public static String getPackageVersion() {
responseData rd = new responseData();
Expand Down
124 changes: 77 additions & 47 deletions sfdx/force-app/main/default/triggers/updateOrderCoupon.trigger
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,98 @@ trigger updateOrderCoupon on Order (after update) {
try {
// for all new Orders, check if the corresponding quote has coupons and duplicate/copy to the corresponding order
for (Order order : Trigger.new) {
if (order.SBQQ__Quote__c != null && order.Status == 'Activated' && Trigger.oldMap.get(order.Id).Status != 'Activated') {
if (Trigger.oldMap.get(order.Id).Status == 'Draft' && order.Status == 'Activated' && order.SBQQ__Quote__c != null) {
// get the corresponding quote for this order
Id quoteId = order.SBQQ__Quote__c;

// fetch the Stripe Coupon Quote Associations for this quote
// fetch any Stripe_Coupon_Quote_Association__c for this order
List<Stripe_Coupon_Quote_Association__c> stripeCouponQuoteAssociations = [
SELECT Id, Stripe_Coupon__c
SELECT Stripe_Coupon__c
FROM Stripe_Coupon_Quote_Association__c
WHERE Quote__c = :quoteId
];

// check if this quote has any associated coupons
if (stripeCouponQuoteAssociations.isEmpty())
{
continue;
}

// for each Stripe_Coupon_Quote_Association__c, create a corresponding Stripe_Coupon_Order_Association__c
for (Stripe_Coupon_Quote_Association__c stripeCouponQuoteAssociation: stripeCouponQuoteAssociations)
if (!stripeCouponQuoteAssociations.isEmpty())
{
List<Stripe_Coupon__c> quoteCoupons = [
SELECT Id, Amount_Off__c, Duration__c, Duration_In_Months__c, Max_Redemptions__c, Name__c, Percent_Off__c
FROM Stripe_Coupon__c
WHERE Id = :stripeCouponQuoteAssociation.Stripe_Coupon__c
];

if (quoteCoupons.isEmpty())
for (Stripe_Coupon_Quote_Association__c stripeCouponQuoteAssociation: stripeCouponQuoteAssociations)
{
throw new CouponException('no stripeCoupon found for stripeCouponQuoteAssociation: ' + stripeCouponQuoteAssociation);
List<Stripe_Coupon__c> quoteCoupons = [
SELECT Id, Amount_Off__c, Duration__c, Duration_In_Months__c, Max_Redemptions__c, Name__c, Percent_Off__c
FROM Stripe_Coupon__c
WHERE Id = :stripeCouponQuoteAssociation.Stripe_Coupon__c
];

if (quoteCoupons.isEmpty())
{
throw new CouponException('no stripeCoupon found for stripeCouponQuoteAssociation: ' + stripeCouponQuoteAssociation.Id);
}

Stripe_Coupon__c quoteCoupon = quoteCoupons.get(0);
Stripe_Coupon_Serialized__c clonedCoupon = Utilities.cloneStripeCoupon(quoteCoupon);

// create a Stripe Coupon Order Association junction object
Stripe_Coupon_Order_Association__c orderStripeCouponAssociation = new Stripe_Coupon_Order_Association__c();
orderStripeCouponAssociation.Stripe_Coupon__c = clonedCoupon.Id;
orderStripeCouponAssociation.Order__c = order.Id;

// insert this record
Database.insert((sObject)orderStripeCouponAssociation);
}

Stripe_Coupon__c quoteCoupon = quoteCoupons.get(0);
}

// fetch the related quote lines
List<SBQQ__QuoteLine__c> quoteLines = [
SELECT Id
FROM SBQQ__QuoteLine__c
WHERE SBQQ__Quote__c = :quoteId
];

// clone the Stripe Coupon on the quote, it will have a different Id
Stripe_Coupon_Serialized__c clonedCoupon = new Stripe_Coupon_Serialized__c();
clonedCoupon.Amount_Off__c = quoteCoupon.Amount_Off__c;
clonedCoupon.Duration__c = quoteCoupon.Duration__c;
clonedCoupon.Duration_In_Months__c = quoteCoupon.Duration_In_Months__c;
clonedCoupon.Max_Redemptions__c = quoteCoupon.Max_Redemptions__c;
clonedCoupon.Name__c = quoteCoupon.Name__c;
clonedCoupon.Percent_Off__c = quoteCoupon.Percent_Off__c;
clonedCoupon.Original_Stripe_Coupon_Id__c = quoteCoupon.Id;

// insert the cloned Stripe coupon
Database.SaveResult result = Database.insertImmediate((sObject)clonedCoupon);
if (!result.isSuccess()) {
// operation failed, so get all errors
for(Database.Error err : result.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Fields that affected this error: ' + err.getFields());
for (SBQQ__QuoteLine__c quoteLine : quoteLines) {
// fetch the Stripe Coupon Quote Line Associations for this quote line
List<Stripe_Coupon_Quote_Line_Association__c> stripeCouponQuoteLineAssociations = [
SELECT Id, Stripe_Coupon__c
FROM Stripe_Coupon_Quote_Line_Association__c
WHERE Quote_Line__c = :quoteLine.Id
];

// for each Stripe_Coupon_Quote_Line_Association__c, create a corresponding Stripe_Coupon_Order_Item_Association__c
if (!stripeCouponQuoteLineAssociations.isEmpty())
{
for (Stripe_Coupon_Quote_Line_Association__c stripeCouponQuoteLineAssociation: stripeCouponQuoteLineAssociations)
{
List<Stripe_Coupon__c> quoteLineCoupons = [
SELECT Amount_Off__c, Duration__c, Duration_In_Months__c, Max_Redemptions__c, Name__c, Percent_Off__c
FROM Stripe_Coupon__c
WHERE Id = :stripeCouponQuoteLineAssociation.Stripe_Coupon__c
];

if (quoteLineCoupons.isEmpty())
{
throw new CouponException('no stripeCoupon found for stripeCouponQuoteLineAssociation: ' + stripeCouponQuoteLineAssociation.Id);
}
}

// create a Stripe Coupon Order Association junction object
Stripe_Coupon_Order_Association__c orderStripeCouponAssociation = new Stripe_Coupon_Order_Association__c(
Stripe_Coupon__c = clonedCoupon.Id,
Order__c = order.Id
);

Stripe_Coupon__c quoteLineCoupon = quoteLineCoupons.get(0);
Stripe_Coupon_Serialized__c clonedCoupon = Utilities.cloneStripeCoupon(quoteLineCoupon);

// get the corresponding order line for this quote line
List<OrderItem> orderItem = [
SELECT Id
FROM OrderItem
WHERE SBQQ__QuoteLine__c = :quoteLine.Id
];

Id orderItemId = orderItem.get(0).Id;

// insert this record
Database.insertImmediate((sObject)orderStripeCouponAssociation);
// create the corresponding Stripe_Coupon_Order_Item_Association__c
Stripe_Coupon_Order_Item_Association__c orderLineStripeCouponAssociation = new Stripe_Coupon_Order_Item_Association__c();
orderLineStripeCouponAssociation.Stripe_Coupon__c = clonedCoupon.Id;
orderLineStripeCouponAssociation.Order_Item__c = orderItemId;

// insert this record
Database.insert((sObject)orderLineStripeCouponAssociation);
}
}
}
}
}
Expand Down
88 changes: 0 additions & 88 deletions sfdx/force-app/main/default/triggers/updateOrderLineCoupon.trigger

This file was deleted.

This file was deleted.

0 comments on commit 659c423

Please sign in to comment.