Skip to content

Commit

Permalink
feat(cart_discount): Added support for totalPrice target on cart_disc…
Browse files Browse the repository at this point in the history
…ount resource
  • Loading branch information
demeyerthom committed Jan 19, 2024
1 parent 748eef5 commit ba44c99
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20240119-115429.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: Added support for totalPrice target on cart_discount resource
time: 2024-01-19T11:54:29.492858858+01:00
48 changes: 31 additions & 17 deletions commercetools/resource_cart_discount.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,33 +128,39 @@ func resourceCartDiscount() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Description: "Supports lineItems/customLineItems/multiBuyLineItems/multiBuyCustomLineItems/shipping",
Description: "Supports lineItems, customLineItems, multiBuyLineItems, multiBuyCustomLineItems, shipping or totalPrice",
Type: schema.TypeString,
Required: true,
ValidateFunc: validateTargetType,
},
"predicate": {
Description: "LineItems/CustomLineItems/MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
Type: schema.TypeString,
Optional: true,
Description: "LineItems, CustomLineItems, MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
"If set for another target the value will be ignored",
Type: schema.TypeString,
Optional: true,
},
"trigger_quantity": {
Description: "MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
Type: schema.TypeInt,
Optional: true,
Description: "MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
"If set for another target the value will be ignored",
Type: schema.TypeInt,
Optional: true,
},
"discounted_quantity": {
Description: "MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
Type: schema.TypeInt,
Optional: true,
Description: "MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
"If set for another target the value will be ignored",
Type: schema.TypeInt,
Optional: true,
},
"max_occurrence": {
Description: "MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
Type: schema.TypeInt,
Optional: true,
Description: "MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
"If set for another target the value will be ignored",
Type: schema.TypeInt,
Optional: true,
},
"selection_mode": {
Description: "MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
Description: "MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
"Can be either Cheapest or MostExpensive. " +
"If set for another target the value will be ignored",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateSelectionMode,
Expand Down Expand Up @@ -193,7 +199,8 @@ func resourceCartDiscount() *schema.Resource {
Default: false,
},
"stacking_mode": {
Description: "Specifies whether the application of this discount causes the following discounts to be ignored",
Description: "Specifies whether the application of this discount causes the following discounts to be ignored. " +
"Can be either Stacking or StopAfterThisDiscount",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateStackingMode,
Expand Down Expand Up @@ -227,9 +234,10 @@ func validateTargetType(val any, key string) (warns []string, errs []error) {
case
"lineItems",
"customLineItems",
"shipping",
"totalPrice",
"multiBuyLineItems",
"multiBuyCustomLineItems",
"shipping":
"multiBuyCustomLineItems":
return
default:
errs = append(errs, fmt.Errorf("%q not a valid value for %q", val, key))
Expand Down Expand Up @@ -662,6 +670,10 @@ func flattenCartDiscountTarget(val platform.CartDiscountTarget) []map[string]any
return []map[string]any{{
"type": "shipping",
}}
case platform.CartDiscountTotalPriceTarget:
return []map[string]any{{
"type": "totalPrice",
}}
}

panic("Unable to flatten cart discount target")
Expand Down Expand Up @@ -721,6 +733,8 @@ func expandCartDiscountTarget(d *schema.ResourceData) (platform.CartDiscountTarg

case "shipping":
return platform.CartDiscountShippingCostTarget{}, nil
case "totalPrice":
return platform.CartDiscountTotalPriceTarget{}, nil
default:
return nil, fmt.Errorf("target type %s not implemented", input["type"])
}
Expand Down
62 changes: 62 additions & 0 deletions commercetools/resource_cart_discount_totalprice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package commercetools

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccCartDiscountTotalPrice(t *testing.T) {
identifier := "totalPrice"
resourceName := "commercetools_cart_discount.totalPrice"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckCartDiscountDestroy,
Steps: []resource.TestStep{
{
Config: testAccCartDiscountTotalPriceConfig(identifier),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name.en", "absolute name"),
resource.TestCheckResourceAttr(resourceName, "value.0.type", "absolute"),
resource.TestCheckResourceAttr(resourceName, "value.0.money.0.currency_code", "USD"),
resource.TestCheckResourceAttr(resourceName, "value.0.money.0.cent_amount", "1000"),
resource.TestCheckResourceAttr(resourceName, "value.0.money.1.currency_code", "EUR"),
resource.TestCheckResourceAttr(resourceName, "value.0.money.1.cent_amount", "2000"),
resource.TestCheckResourceAttr(resourceName, "target.0.type", "totalPrice"),
),
},
},
})
}

func testAccCartDiscountTotalPriceConfig(identifier string) string {
return hclTemplate(`
resource "commercetools_cart_discount" "{{ .identifier }}" {
name = {
en = "absolute name"
}
sort_order = "0.9"
predicate = "1=1"
target {
type = "totalPrice"
}
value {
type = "absolute"
money {
currency_code = "USD"
cent_amount = 1000
}
money {
currency_code = "EUR"
cent_amount = 2000
}
}
}
`, map[string]any{
"identifier": identifier,
})
}
2 changes: 1 addition & 1 deletion commercetools/resource_discount_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func resourceDiscountCode() *schema.Resource {
Optional: true,
},
"max_applications": {
Description: "The discount code can only be applied the specified times overall" +
Description: "The discount code can only be applied the specified times overall. " +
"Note that due to an engine constraint 0 cannot be set for this field, " +
"so possible values are either larger than 0 or not set",
Type: schema.TypeInt,
Expand Down
14 changes: 7 additions & 7 deletions docs/resources/cart_discount.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ resource "commercetools_cart_discount" "my-cart-discount" {
- `is_active` (Boolean) Only active discount can be applied to the cart
- `key` (String) User-specific unique identifier for a cart discount. Must be unique across a project
- `requires_discount_code` (Boolean) States whether the discount can only be used in a connection with a [DiscountCode](https://docs.commercetools.com/api/projects/discountCodes#discountcode)
- `stacking_mode` (String) Specifies whether the application of this discount causes the following discounts to be ignored
- `stacking_mode` (String) Specifies whether the application of this discount causes the following discounts to be ignored. Can be either Stacking or StopAfterThisDiscount
- `target` (Block List, Max: 1) Empty when the value has type giftLineItem, otherwise a [CartDiscountTarget](https://docs.commercetools.com/api/projects/cartDiscounts#cartdiscounttarget) (see [below for nested schema](#nestedblock--target))
- `valid_from` (String)
- `valid_until` (String)
Expand Down Expand Up @@ -222,12 +222,12 @@ Optional:

Required:

- `type` (String) Supports lineItems/customLineItems/multiBuyLineItems/multiBuyCustomLineItems/shipping
- `type` (String) Supports lineItems, customLineItems, multiBuyLineItems, multiBuyCustomLineItems, shipping or totalPrice

Optional:

- `discounted_quantity` (Number) MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
- `max_occurrence` (Number) MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
- `predicate` (String) LineItems/CustomLineItems/MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
- `selection_mode` (String) MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
- `trigger_quantity` (Number) MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
- `discounted_quantity` (Number) MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. If set for another target the value will be ignored
- `max_occurrence` (Number) MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. If set for another target the value will be ignored
- `predicate` (String) LineItems, CustomLineItems, MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. If set for another target the value will be ignored
- `selection_mode` (String) MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. Can be either Cheapest or MostExpensive. If set for another target the value will be ignored
- `trigger_quantity` (Number) MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. If set for another target the value will be ignored
4 changes: 2 additions & 2 deletions docs/resources/discount_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ resource "commercetools_discount_code" "my_discount_code" {
- `description` (Map of String) [LocalizedString](https://docs.commercetools.com/api/types#localizedstring)
- `groups` (List of String) The groups to which this discount code belong
- `is_active` (Boolean)
- `max_applications` (Number) The discount code can only be applied maxApplications times
- `max_applications_per_customer` (Number) The discount code can only be applied maxApplicationsPerCustomer times per customer
- `max_applications` (Number) The discount code can only be applied the specified times overall. Note that due to an engine constraint 0 cannot be set for this field, so possible values are either larger than 0 or not set
- `max_applications_per_customer` (Number) The discount code can only be applied the specified times per customer. Note that due to an engine constraint 0 cannot be set for this field, so possible values are either larger than 0 or not set
- `name` (Map of String) [LocalizedString](https://docs.commercetools.com/api/types#localizedstring)
- `predicate` (String) [Cart Predicate](https://docs.commercetools.com/api/projects/predicates#cart-predicates)
- `valid_from` (String) The time from which the discount can be applied on a cart. Before that time the code is invalid
Expand Down

0 comments on commit ba44c99

Please sign in to comment.