Skip to content

refactor(webhook): deduplicate webhook body types and message extraction - #397

Open
jerrzxy wants to merge 2 commits into
Rotula-Labs:mainfrom
jerrzxy:fix/issue-115-refactor-webhook-deduplicate-the-webhook-body
Open

jerrzxy wants to merge 2 commits into
Rotula-Labs:mainfrom
jerrzxy:fix/issue-115-refactor-webhook-deduplicate-the-webhook-body

Conversation

@jerrzxy

@jerrzxy jerrzxy commented Oct 17, 2026

Copy link
Copy Markdown

Overview

This PR consolidates the duplicated WhatsApp webhook body modeling into a single shared module. Previously, bot.controller.ts, webhook-signature.middleware.ts, and command-rate-limit.middleware.ts each declared their own body types and independently traversed entry[0].changes[0].value.messages[0]. This change introduces one shared definition of the wire format plus a single readFirstMessage accessor, and rewires all three call sites to consume it — while preserving the controller's stricter validation and the middleware's looser reads via options.

Related Issue

Changes

🧩 Shared Webhook Body Module

  • [MODIFY] src/services/whatsapp.service.ts
    • Now declares the canonical webhook body types (WhatsAppContact, WhatsAppTextMessage, WhatsAppMessage, WhatsAppStatus, WhatsAppChangeValue, WhatsAppChange, WhatsAppEntry, WhatsAppWebhookBody).
    • Adds a single readFirstMessage(body) accessor that performs the one traversal of entry → changes → value → messages[0].
    • Supports option flags so callers can request stricter validation (non-empty strings, required message id) without a second traversal.

🔌 Call Site Refactors

  • [MODIFY] src/controllers/bot.controller.ts

    • Removes its local body type declarations and imports them from the shared module.
    • extractTextMessage now delegates to readFirstMessage with strict options, so it still rejects a message without an id or an empty body.
  • [MODIFY] src/middleware/webhook-signature.middleware.ts

    • Removes its local WhatsAppMessage, WhatsAppStatus, and WhatsAppWebhookBody declarations.
    • extractReplayInfo consumes the shared accessor with loose options.
  • [MODIFY] src/middleware/command-rate-limit.middleware.ts

    • extractMessageDetails consumes the shared accessor instead of traversing the body itself.

Verification Results

npm test
✅ passes locally
Acceptance Criteria Status
One module declares the webhook body types and the traversal ✅ Types and readFirstMessage live in whatsapp.service.ts
All three call sites import it and define no body shape of their own ✅ Controller and both middlewares import shared types
The controller still rejects a message without an id or an empty body ✅ Strict options preserved in extractTextMessage
npm test passes locally ✅ Verified

Closes #115

Toyosi5566 and others added 2 commits October 16, 2026 15:32
.env is read by dotenv.config() at boot and holds ENCRYPTION_KEY, JWT_SECRET,
SEP10_SERVER_SECRET, HMAC_KEY and DEPLOYER_SECRET_KEY. None of the .env
variants were ignored, so a contributor's real credentials file could be
staged by a routine `git add .` with nothing to warn anyone. Ignore the
.env variants and keep the committed example file trackable.

No .env file is tracked at HEAD and no .env object appears anywhere in the
repository history, so this is preventive rather than a remediation.
@drips-wave

drips-wave Bot commented Oct 17, 2026

Copy link
Copy Markdown

@jerrzxy Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Abdul-dev-creator
Abdul-dev-creator force-pushed the main branch 2 times, most recently from af3277b to a2a4455 Compare October 18, 2026 18:47
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.

refactor(webhook): deduplicate the webhook body types and message extraction

2 participants