Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cart: dispatch events before and after cart merge for e.g. logging #351

Merged
merged 3 commits into from
Dec 1, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions cart/application/eventHandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ type (
cartReceiverService *CartReceiverService
cartCache CartCache
webIdentityService *auth.WebIdentityService
eventRouter flamingo.EventRouter
}

// PreCartMergeEvent is dispatched after getting the (current) guest cart and the customer cart before merging
PreCartMergeEvent struct {
GuestCart *cartDomain.Cart
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the carts passed by reference for a specific reason? would change that to value objects to avoid accidentally mutating the structs in the event listener.

CustomerCart *cartDomain.Cart
}

// PostCartMergeEvent is dispatched after merging the guest cart and the customer cart
PostCartMergeEvent struct {
MergedCart *cartDomain.Cart
}
)

Expand All @@ -29,6 +41,7 @@ func (e *EventReceiver) Inject(
cartService *CartService,
cartReceiverService *CartReceiverService,
webIdentityService *auth.WebIdentityService,
eventRouter flamingo.EventRouter,
optionals *struct {
CartCache CartCache `inject:",optional"`
},
Expand All @@ -37,6 +50,7 @@ func (e *EventReceiver) Inject(
e.cartService = cartService
e.cartReceiverService = cartReceiverService
e.webIdentityService = webIdentityService
e.eventRouter = eventRouter
if optionals != nil {
e.cartCache = optionals.CartCache
}
Expand Down Expand Up @@ -79,6 +93,9 @@ func (e *EventReceiver) Notify(ctx context.Context, event flamingo.Event) {
if err != nil {
e.logger.WithContext(ctx).Error("WebLoginEvent - DeleteSavedSessionGuestCartID Error", err)
}

e.eventRouter.Dispatch(ctx, &PreCartMergeEvent{GuestCart: guestCart, CustomerCart: customerCart})

for _, d := range guestCart.Deliveries {
e.logger.WithContext(ctx).Info(fmt.Sprintf("Merging delivery with code %v of guestCart with ID %v into customerCart with ID %v", d.DeliveryInfo.Code, guestCart.ID, customerCart.ID))
err := e.cartService.UpdateDeliveryInfo(ctx, session, d.DeliveryInfo.Code, cartDomain.CreateDeliveryInfoUpdateCommand(d.DeliveryInfo))
Expand Down Expand Up @@ -115,15 +132,15 @@ func (e *EventReceiver) Notify(ctx context.Context, event flamingo.Event) {
}
if guestCart.HasAppliedCouponCode() {
for _, code := range guestCart.AppliedCouponCodes {
_, err := e.cartService.ApplyVoucher(ctx, session, code.Code)
customerCart, err = e.cartService.ApplyVoucher(ctx, session, code.Code)
if err != nil {
e.logger.WithContext(ctx).Error("WebLoginEvent - customerCart ApplyVoucher has error", code.Code, err)
}
}
}
if guestCart.HasAppliedGiftCards() {
for _, code := range guestCart.AppliedGiftCards {
_, err := e.cartService.ApplyGiftCard(ctx, session, code.Code)
customerCart, err = e.cartService.ApplyGiftCard(ctx, session, code.Code)
if err != nil {
e.logger.WithContext(ctx).Error("WebLoginEvent - customerCart ApplyGiftCard has error", code.Code, err)
}
Expand All @@ -140,6 +157,8 @@ func (e *EventReceiver) Notify(ctx context.Context, event flamingo.Event) {
}
}
}

e.eventRouter.Dispatch(ctx, &PostCartMergeEvent{MergedCart: customerCart})
})
// Handle Event to Invalidate the Cart Cache
case *cartDomain.InvalidateCartEvent:
Expand Down