Skip to content

Commit

Permalink
checkout: Add CartValidationResult to place order context if in faile…
Browse files Browse the repository at this point in the history
…d state (#319)

In the GraphQL implementation we expose the cart validation result if the ValidateCart state fails. This information is missing in the JSON API so that you only see that the cart is invalid but not which rule was triggered. This PR therefore adds an additional field to the place order context when it's in failed state due to an invalid cart.
  • Loading branch information
carstendietrich authored Jun 8, 2021
1 parent 4e78182 commit 490a5fa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

**checkout**
* Introducing Flamingo events on final states of the place order process
* API
* In case of an invalid cart during place order process we now expose the cart validation result, affected endpoints:
```
GET /api/v1/checkout/placeorder
POST /api/v1/checkout/placeorder/refresh
POST /api/v1/checkout/placeorder/refresh-blocking
```
**customer**
* Add mockery mocks for both `Customer` / `CustomerIdentityService` for easier testing
Expand Down
3 changes: 2 additions & 1 deletion cart/domain/validation/cartValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package validation

import (
"context"

"flamingo.me/flamingo-commerce/v3/cart/domain/decorator"

"flamingo.me/flamingo/v3/framework/web"
)

type (
//Result groups the validation result
// Result groups the validation result
Result struct {
HasCommonError bool
CommonErrorMessageKey string
Expand Down
33 changes: 21 additions & 12 deletions checkout/interfaces/controller/apicontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
placeorderDomain "flamingo.me/flamingo-commerce/v3/cart/domain/placeorder"
"flamingo.me/flamingo-commerce/v3/cart/domain/validation"
"flamingo.me/flamingo-commerce/v3/checkout/application"
"flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/process"

Expand Down Expand Up @@ -35,12 +36,13 @@ type (

// placeOrderContext infos
placeOrderContext struct {
Cart *cart.Cart
OrderInfos *placedOrderInfos
State string
StateData process.StateData
UUID string
FailedReason string
Cart *cart.Cart
OrderInfos *placedOrderInfos
State string
StateData process.StateData
UUID string
FailedReason string
CartValidationResult *validation.Result
}

// placedOrderInfos infos
Expand Down Expand Up @@ -181,17 +183,24 @@ func (c *APIController) getPlaceOrderContext(ctx context.Context, pctx *process.
PlacedDecoratedCart: decoratedCart,
}
}

var validationResult *validation.Result
var failedReason string
if pctx.FailedReason != nil {
failedReason = pctx.FailedReason.Reason()
if reason, ok := pctx.FailedReason.(process.CartValidationErrorReason); ok {
validationResult = &reason.ValidationResult
}
}

return placeOrderContext{
Cart: &pctx.Cart,
OrderInfos: orderInfos,
State: pctx.CurrentStateName,
StateData: pctx.CurrentStateData,
FailedReason: failedReason,
UUID: pctx.UUID,
Cart: &pctx.Cart,
OrderInfos: orderInfos,
State: pctx.CurrentStateName,
StateData: pctx.CurrentStateData,
FailedReason: failedReason,
CartValidationResult: validationResult,
UUID: pctx.UUID,
}
}

Expand Down
3 changes: 3 additions & 0 deletions docs/openapi/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,9 @@ var doc = `{
"Cart": {
"$ref": "#/definitions/cart.Cart"
},
"CartValidationResult": {
"$ref": "#/definitions/validation.Result"
},
"FailedReason": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions docs/openapi/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,9 @@
"Cart": {
"$ref": "#/definitions/cart.Cart"
},
"CartValidationResult": {
"$ref": "#/definitions/validation.Result"
},
"FailedReason": {
"type": "string"
},
Expand Down
2 changes: 2 additions & 0 deletions docs/openapi/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ definitions:
properties:
Cart:
$ref: '#/definitions/cart.Cart'
CartValidationResult:
$ref: '#/definitions/validation.Result'
FailedReason:
type: string
OrderInfos:
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger.go

Large diffs are not rendered by default.

0 comments on commit 490a5fa

Please sign in to comment.