Skip to content

Commit

Permalink
fix: Added additional check for unset values on discount_code resource
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Nov 15, 2023
1 parent c0c7e0a commit d8df8d9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20231115-103727.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: Added additional check for unset values on discount_code resource
time: 2023-11-15T10:37:27.09445307+01:00
33 changes: 21 additions & 12 deletions commercetools/resource_discount_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commercetools

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -67,14 +68,20 @@ func resourceDiscountCode() *schema.Resource {
Optional: true,
},
"max_applications_per_customer": {
Description: "The discount code can only be applied maxApplicationsPerCustomer times per customer",
Type: schema.TypeInt,
Optional: true,
Description: "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",
Type: schema.TypeInt,
ValidateFunc: validation.IntAtLeast(1),
Optional: true,
},
"max_applications": {
Description: "The discount code can only be applied maxApplications times",
Type: schema.TypeInt,
Optional: true,
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,
Optional: true,
ValidateFunc: validation.IntAtLeast(1),
},
"groups": {
Description: "The groups to which this discount code belong",
Expand Down Expand Up @@ -121,8 +128,8 @@ func resourceDiscountCodeCreate(ctx context.Context, d *schema.ResourceData, m a
Code: d.Get("code").(string),
CartPredicate: cartPredicate,
IsActive: boolRef(d.Get("is_active")),
MaxApplicationsPerCustomer: intRef(d.Get("max_applications_per_customer")),
MaxApplications: intRef(d.Get("max_applications")),
MaxApplicationsPerCustomer: intNilIfEmpty(intRef(d.Get("max_applications_per_customer"))),
MaxApplications: intNilIfEmpty(intRef(d.Get("max_applications"))),
Groups: expandDiscountCodeGroups(d),
CartDiscounts: expandDiscountCodeCartDiscounts(d),
Custom: custom,
Expand Down Expand Up @@ -217,17 +224,19 @@ func resourceDiscountCodeUpdate(ctx context.Context, d *schema.ResourceData, m a
}

if d.HasChange("max_applications") {
newMaxApplications := d.Get("max_applications").(int)
maxApplications := d.Get("max_applications").(int)
input.Actions = append(
input.Actions,
&platform.DiscountCodeSetMaxApplicationsAction{MaxApplications: &newMaxApplications})
&platform.DiscountCodeSetMaxApplicationsAction{MaxApplications: intNilIfEmpty(&maxApplications)})
}

if d.HasChange("max_applications_per_customer") {
newMaxApplications := d.Get("max_applications_per_customer").(int)
maxApplicationsPerCustomer := d.Get("max_applications_per_customer").(int)
input.Actions = append(
input.Actions,
&platform.DiscountCodeSetMaxApplicationsPerCustomerAction{MaxApplicationsPerCustomer: &newMaxApplications})
&platform.DiscountCodeSetMaxApplicationsPerCustomerAction{
MaxApplicationsPerCustomer: intNilIfEmpty(&maxApplicationsPerCustomer),
})
}

if d.HasChange("cart_discounts") {
Expand Down
13 changes: 13 additions & 0 deletions commercetools/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,16 @@ func coerceTypedMoney(val platform.TypedMoney) platform.Money {

return platform.Money{}
}

// intNilIfEmpty returns a nil value if the integer is nil or empty (0) otherwise
// it returns the value
func intNilIfEmpty(val *int) *int {
if val == nil {
return nil
}

if *val == 0 {
return nil
}
return val
}
17 changes: 17 additions & 0 deletions commercetools/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commercetools
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"testing"

"github.com/labd/commercetools-go-sdk/platform"
Expand Down Expand Up @@ -75,3 +76,19 @@ func checkApiResult(err error) error {
return fmt.Errorf("unexpected result returned")
}
}

func TestIntNilIfEmpty(t *testing.T) {
testCases := []struct {
input *int
expected *int
}{
{nil, nil},
{intRef(0), nil},
{intRef(1), intRef(1)},
}

for _, tt := range testCases {
v := intNilIfEmpty(tt.input)
assert.Equal(t, tt.expected, v)
}
}

0 comments on commit d8df8d9

Please sign in to comment.