Skip to content

Conversation

@ValdecirMysian
Copy link

@ValdecirMysian ValdecirMysian commented Jan 28, 2026

This PR adds support for orderMessage (catalog orders) in the Chatwoot integration. Previously, catalog orders would appear as empty messages. This change ensures that the product title, price, and ID are correctly formatted and sent as text to Chatwoot.

Summary by Sourcery

Add support for WhatsApp Business catalog order messages in the Chatwoot integration and prevent duplicate order events from being sent.

New Features:

  • Format WhatsApp catalog orderMessage payloads into readable text messages with product, quantity, total price, and order ID for Chatwoot.

Bug Fixes:

  • Ensure catalog orders no longer appear as empty messages in Chatwoot by correctly extracting their content.
  • Avoid sending duplicate catalog order messages by caching and deduplicating order IDs with a short TTL.

Added support for orderMessage in getTypeMessage method and updated message formatting for orders.
Refactor order message handling and improve formatting.
Implement cache for deduplication of order messages to avoid processing duplicates.
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 28, 2026

Reviewer's Guide

Adds WhatsApp Business catalog orderMessage support to the Chatwoot integration, including message-type detection, human-readable text formatting, and a small in-memory cache to deduplicate repeated order notifications.

Class diagram for ChatwootService orderMessage support

classDiagram
  class ChatwootService {
    -provider : any
    -processedOrderIds : Map<string, number>
    -ORDER_CACHE_TTL_MS : number
    +getTypeMessage(msg : any) any
    +getMessageContent(types : any) string
  }
  ChatwootService --> WAMonitoringService : uses
  ChatwootService --> ConfigService : uses
Loading

Flow diagram for orderMessage handling

flowchart TD
  A[Receive types map] --> B{typeKey is orderMessage}
  B -- No --> Z[Return existing result]
  B -- Yes --> C[Extract orderMessage]
  C --> D[Clean cache older than ORDER_CACHE_TTL_MS]
  D --> E{orderId already cached}
  E -- Yes --> Y[Return undefined]
  E -- No --> F[Cache orderId timestamp]
  F --> G[Derive rawPrice from totalAmount1000]
  G --> H[Format localized price]
  H --> I[Assemble catalog order text]
  I --> X[Return formatted response]
Loading

File-Level Changes

Change Details Files
Handle WhatsApp catalog orderMessage as a first-class message type and render a formatted text summary for Chatwoot.
  • Extend type extraction to surface msg.orderMessage in getTypeMessage
  • Add orderMessage-specific handling in getMessageContent that formats order details (title, quantity, total price, order ID) into a user-friendly text block
  • Normalize totalAmount1000 from multiple possible numeric representations (Long, {low, high} object, plain number) and format it as a localized currency string
  • Provide sensible fallbacks for missing orderTitle, itemCount, or orderId fields
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Introduce a simple in-memory cache to prevent duplicate catalog order messages from being sent to Chatwoot.
  • Add processedOrderIds Map and ORDER_CACHE_TTL_MS constant to ChatwootService
  • On processing orderMessage, evict expired cache entries based on TTL and skip messages whose orderId was already processed within the TTL window
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Preserve existing message content handling while keeping the externalAdReplyBody cleanup and location message formatting logic intact.
  • Retain and slightly refactor getTypeMessage and getMessageContent structure while inserting orderMessage logic and keeping the externalAdReplyBody stripping behavior unchanged
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The hard-coded orderMessage text (including emojis and PT-BR strings) in getMessageContent makes this path locale-specific; consider reusing any existing localization/templating mechanism so catalog orders can be formatted per workspace/language configuration rather than fixed Portuguese.
  • The deduplication cache for orderMessage is keyed only by orderId; if the same ID can appear across different senders/tenants, consider including sender or conversation identifiers in the key to avoid suppressing legitimate messages in other contexts.
  • The orderMessage cache cleanup only runs when processing a new orderMessage, so stale entries can accumulate if no further orders arrive; consider extracting this into a periodic cleanup or a helper called from a broader code path to keep the map bounded over time.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The hard-coded orderMessage text (including emojis and PT-BR strings) in `getMessageContent` makes this path locale-specific; consider reusing any existing localization/templating mechanism so catalog orders can be formatted per workspace/language configuration rather than fixed Portuguese.
- The deduplication cache for `orderMessage` is keyed only by `orderId`; if the same ID can appear across different senders/tenants, consider including sender or conversation identifiers in the key to avoid suppressing legitimate messages in other contexts.
- The orderMessage cache cleanup only runs when processing a new orderMessage, so stale entries can accumulate if no further orders arrive; consider extracting this into a periodic cleanup or a helper called from a broader code path to keep the map bounded over time.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Filtered null/undefined values from replyToIds before sending and constructed messageData object to include valid content_attributes.
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.

1 participant