Skip to content

feat: VTEX client and add_to_cart action support - #149

Merged
paulobernardoaf merged 4 commits into
mainfrom
feat/vtex-add-to-cart
Apr 10, 2026
Merged

paulobernardoaf merged 4 commits into
mainfrom
feat/vtex-add-to-cart

Conversation

@paulobernardoaf

Copy link
Copy Markdown
Contributor

What

Adds an add_to_cart WebSocket message type that lets browser clients add or update items in a VTEX shopping cart. The implementation introduces a new pkg/vtex package containing an HTTP client for the VTEX Checkout API, wired into the existing WebSocket handler pipeline.

New package — pkg/vtex

  • IClient interface with a single method AddOrUpdateCartItem(ctx, vtexAccount, orderFormID, itemID, seller) for testability
  • Client implementation that fetches the current order form (GET), then either adds the item or increments its quantity (POST)
  • gomock-generated mock (MockIClient) for handler-level tests

WebSocket integration

  • New add_to_cart case in ParsePayload dispatch
  • AddToCart handler validates the payload synchronously, then runs the VTEX API call in a goroutine (non-blocking for the read loop)
  • Sends cart_updated on success or cart_error on failure back to the client

Hardening

  • 30 s HTTP client timeout on the VTEX client to prevent goroutine leaks from slow/unresponsive upstream
  • context.WithTimeout (30 s) wrapping the goroutine's VTEX call, enabling cancellation and bounding goroutine lifetime
  • SSRF protection: vtexAccount and orderFormID are validated against ^[a-zA-Z0-9][a-zA-Z0-9-]*$ before URL interpolation, blocking fragment (#), slash, dot, and query-string injection attacks

Files changed

File Change
pkg/vtex/client.go New — VTEX Checkout API client
pkg/vtex/client_mock.go New — gomock mock
pkg/vtex/client_test.go New — httptest-based tests (add, update, errors, SSRF validation)
pkg/websocket/application.go Added VTEXClient vtex.IClient to App and NewApp
pkg/websocket/client.go AddToCart handler + ParsePayload case
pkg/websocket/client_test.go 8 new test cases (happy path, not registered, feature disabled, missing fields, VTEX error, ParsePayload integration)
api/main.go Wiring: creates vtex.NewClient(), passes to NewApp

Why

Enables the webchat widget to add products to a VTEX shopping cart directly from the conversation, supporting e-commerce use cases where a chatbot recommends products and the user can add them without leaving the chat.

@paulobernardoaf
paulobernardoaf merged commit 38a06dc into main Apr 10, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants