Skip to content

Commit

Permalink
feat: added stores to cart discount
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Oct 22, 2024
1 parent 9373614 commit a4d7fe1
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20241022-162737.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: Added stores to cart discounts
time: 2024-10-22T16:27:37.948019802+02:00
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ vendor/

/.idea
/.env
/go.work*
37 changes: 37 additions & 0 deletions commercetools/resource_cart_discount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
100 changes: 100 additions & 0 deletions commercetools/resource_cart_discount_stores_test.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit a4d7fe1

Please sign in to comment.