From a4d7fe1c15b4b31f2ce3871103b4069113824fff Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 22 Oct 2024 16:28:15 +0200 Subject: [PATCH] feat: added stores to cart discount --- .../unreleased/Added-20241022-162737.yaml | 3 + .gitignore | 1 + commercetools/resource_cart_discount.go | 37 +++++++ .../resource_cart_discount_stores_test.go | 100 ++++++++++++++++++ go.mod | 7 +- go.sum | 4 +- 6 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 .changes/unreleased/Added-20241022-162737.yaml create mode 100644 commercetools/resource_cart_discount_stores_test.go diff --git a/.changes/unreleased/Added-20241022-162737.yaml b/.changes/unreleased/Added-20241022-162737.yaml new file mode 100644 index 00000000..33764d23 --- /dev/null +++ b/.changes/unreleased/Added-20241022-162737.yaml @@ -0,0 +1,3 @@ +kind: Added +body: Added stores to cart discounts +time: 2024-10-22T16:27:37.948019802+02:00 diff --git a/.gitignore b/.gitignore index 4e2fd302..90b3abc9 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ vendor/ /.idea /.env +/go.work* diff --git a/commercetools/resource_cart_discount.go b/commercetools/resource_cart_discount.go index 911b8c92..f518f1f7 100644 --- a/commercetools/resource_cart_discount.go +++ b/commercetools/resource_cart_discount.go @@ -168,6 +168,16 @@ func resourceCartDiscount() *schema.Resource { }, }, }, + "stores": { + Description: "If a value exists, the Cart Discount applies on Carts having a Store matching any " + + "Store defined for this field. If empty, the Cart Discount applies on all Carts, irrespective of " + + "a Store. Use store keys as references", + Type: schema.TypeSet, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + }, "sort_order": { Description: "The string must contain a number between 0 and 1. All matching cart discounts are " + "applied to a cart in the order defined by this field. A discount with greater sort order is " + @@ -301,6 +311,7 @@ func resourceCartDiscountCreate(ctx context.Context, d *schema.ResourceData, m a SortOrder: d.Get("sort_order").(string), IsActive: boolRef(d.Get("is_active")), RequiresDiscountCode: ctutils.BoolRef(d.Get("requires_discount_code").(bool)), + Stores: expandStores(d.Get("stores").(*schema.Set)), Custom: custom, StackingMode: &stackingMode, } @@ -377,6 +388,7 @@ func resourceCartDiscountRead(ctx context.Context, d *schema.ResourceData, m any _ = d.Set("requires_discount_code", cartDiscount.RequiresDiscountCode) _ = d.Set("stacking_mode", cartDiscount.StackingMode) _ = d.Set("custom", flattenCustomFields(cartDiscount.Custom)) + _ = d.Set("stores", flattenStores(cartDiscount.Stores)) return nil } @@ -514,6 +526,13 @@ func resourceCartDiscountUpdate(ctx context.Context, d *schema.ResourceData, m a } } + if d.HasChange("stores") { + stores := expandStores(d.Get("stores").(*schema.Set)) + input.Actions = append( + input.Actions, + &platform.CartDiscountSetStoresAction{Stores: stores}) + } + err := retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError { _, err := client.CartDiscounts().WithId(d.Id()).Post(input).Execute(ctx) return utils.ProcessRemoteError(err) @@ -762,3 +781,21 @@ func expandSelectionMode(selectionMode string) (platform.SelectionMode, error) { return "", fmt.Errorf("selection mode %s not implemented", selectionMode) } } + +func flattenStores(storeKeyReferences []platform.StoreKeyReference) []string { + var storeKeys []string + for _, store := range storeKeyReferences { + storeKeys = append(storeKeys, store.Key) + } + + return storeKeys +} + +func expandStores(storeKeys *schema.Set) []platform.StoreResourceIdentifier { + storeResourceIdentifiers := make([]platform.StoreResourceIdentifier, 0, storeKeys.Len()) + for _, key := range storeKeys.List() { + var keyVal = key.(string) + storeResourceIdentifiers = append(storeResourceIdentifiers, platform.StoreResourceIdentifier{Key: &keyVal}) + } + return storeResourceIdentifiers +} diff --git a/commercetools/resource_cart_discount_stores_test.go b/commercetools/resource_cart_discount_stores_test.go new file mode 100644 index 00000000..0e74010b --- /dev/null +++ b/commercetools/resource_cart_discount_stores_test.go @@ -0,0 +1,100 @@ +package commercetools + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccCartDiscountStores(t *testing.T) { + identifier := "stores" + resourceName := "commercetools_cart_discount.stores" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviders, + CheckDestroy: testAccCheckCartDiscountDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCartDiscountWithoutStores(identifier), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "stores.#", "0"), + ), + }, + { + Config: testAccCartDiscountWithStores(identifier), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckTypeSetElemAttr(resourceName, "stores.*", "my-store"), + ), + }, + { + Config: testAccCartDiscountWithoutStores(identifier), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "stores.#", "0"), + ), + }, + }, + }) +} + +func testAccCartDiscountWithoutStores(identifier string) string { + return hclTemplate(` + resource "commercetools_cart_discount" "{{ .identifier }}" { + name = { + en = "fixed name" + } + sort_order = "0.9" + predicate = "1=1" + + target { + type = "shipping" + } + + value { + type = "fixed" + money { + currency_code = "USD" + cent_amount = 1000 + } + } + } + `, map[string]any{ + "identifier": identifier, + }) +} + +func testAccCartDiscountWithStores(identifier string) string { + return hclTemplate(` + resource "commercetools_store" "my-store-{{ .identifier }}" { + key = "my-store" + name = { + en-US = "My store" + } + countries = ["NL", "BE"] + languages = ["nl-NL"] + } + + resource "commercetools_cart_discount" "{{ .identifier }}" { + name = { + en = "fixed name" + } + stores = [commercetools_store.my-store-{{ .identifier }}.key] + sort_order = "0.9" + predicate = "1=1" + + target { + type = "shipping" + } + + value { + type = "fixed" + money { + currency_code = "USD" + cent_amount = 1000 + } + } + } + `, map[string]any{ + "identifier": identifier, + }) +} diff --git a/go.mod b/go.mod index c908168f..b1b0fb74 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,8 @@ module github.com/labd/terraform-provider-commercetools -go 1.21 -toolchain go1.22.5 +go 1.22.0 + +toolchain go1.22.8 //replace github.com/labd/commercetools-go-sdk v1.5.1 => ../commercetools-go-sdk @@ -15,7 +16,7 @@ require ( github.com/hashicorp/terraform-plugin-go v0.24.0 github.com/hashicorp/terraform-plugin-mux v0.16.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 - github.com/labd/commercetools-go-sdk v1.6.0 + github.com/labd/commercetools-go-sdk v1.7.0 github.com/mitchellh/mapstructure v1.5.0 github.com/stretchr/testify v1.9.0 golang.org/x/oauth2 v0.23.0 diff --git a/go.sum b/go.sum index 9fd1d958..6bd45ee1 100644 --- a/go.sum +++ b/go.sum @@ -141,8 +141,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/labd/commercetools-go-sdk v1.6.0 h1:f+hXCSea6WSsANzPliUZes/qbIBBMyhZF1WhBcnLneM= -github.com/labd/commercetools-go-sdk v1.6.0/go.mod h1:3K76EpprufmZhqmcEZ+lPAKeK7tDUzEq81gT9pzrvyQ= +github.com/labd/commercetools-go-sdk v1.7.0 h1:kJv0rAYZ3CqOCuHB/LluZSPoPrnl55f/x7FNwURhXv0= +github.com/labd/commercetools-go-sdk v1.7.0/go.mod h1:B0nR272yhimVmiR6WS2nTctE7dSq6mehE1hre431wtk= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=