Skip to content

feat(chat): add fetchLid endpoint for PN to LID resolution#2483

Open
MrShennawy wants to merge 2 commits intoEvolutionAPI:mainfrom
MrShennawy:feature/fetch-lid
Open

feat(chat): add fetchLid endpoint for PN to LID resolution#2483
MrShennawy wants to merge 2 commits intoEvolutionAPI:mainfrom
MrShennawy:feature/fetch-lid

Conversation

@MrShennawy
Copy link

@MrShennawy MrShennawy commented Mar 23, 2026

Description

This PR introduces a new endpoint /fetchLid that allows retrieving a user's lid using their phone number.

Motivation and Context

I am currently transitioning the system's core logic to rely primarily on lid rather than phone numbers, as lid has proven to be more consistent and reliable across various WhatsApp events.

However, in certain scenarios (such as specific incoming messages), the system might only receive the phone number. This new endpoint serves as a bridge, allowing us to fetch the corresponding lid via Baileys' signalRepository.lidMapping. This ensures that our lid-based processing remains robust and uninterrupted even when initially provided with just a phone number.

Changes Included

  • Service: Added getLid(number) in whatsapp.baileys.service.ts to query the signal repository for the LID.
  • Controller: Added fetchLid method in chat.controller.ts.
  • Router: Registered POST /fetchLid route in chat.router.ts.
  • Validation: Added fetchLidSchema in chat.schema.ts to validate the incoming number payload.

Summary by Sourcery

Add a new chat endpoint to resolve a WhatsApp LID from a phone number and expose the corresponding Baileys service method.

New Features:

  • Introduce Baileys service method to retrieve a user's LID and WhatsApp JID from a phone number.
  • Expose a POST /fetchLid chat API endpoint wired through the chat controller to return LID resolution results.
  • Add JSON schema validation for the fetchLid request payload requiring a phone number string.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Mar 23, 2026

Reviewer's Guide

Adds a new Baileys-based service method and HTTP endpoint to resolve a WhatsApp LID from a phone number, including validation and routing wiring.

Sequence diagram for the new /fetchLid PN-to-LID resolution flow

sequenceDiagram
  actor ApiClient
  participant ChatRouter
  participant DataValidator
  participant ChatController
  participant WAMonitor
  participant WaInstance
  participant BaileysStartupService
  participant BaileysClient
  participant SignalRepository
  participant LidMapping

  ApiClient->>ChatRouter: POST /fetchLid { number }
  ChatRouter->>DataValidator: validate(fetchLidSchema, NumberDto)
  DataValidator-->>ChatRouter: validated NumberDto
  ChatRouter->>ChatController: fetchLid(InstanceDto, NumberDto)
  ChatController->>WAMonitor: waInstances[instanceName]
  WAMonitor-->>ChatController: WaInstance
  ChatController->>WaInstance: getLid(number)
  WaInstance->>BaileysStartupService: getLid(number)
  BaileysStartupService->>BaileysStartupService: jid = createJid(number)
  alt signalRepository missing
    BaileysStartupService-->>WaInstance: { wuid: jid, lid: null }
  else signalRepository present
    BaileysStartupService->>BaileysClient: client
    BaileysClient->>SignalRepository: signalRepository
    SignalRepository->>LidMapping: getLIDForPN(jid)
    LidMapping-->>SignalRepository: lid or null
    SignalRepository-->>BaileysStartupService: lid or null
    BaileysStartupService-->>WaInstance: { wuid: jid, lid }
  end
  WaInstance-->>ChatController: { wuid, lid }
  ChatController-->>ChatRouter: { wuid, lid }
  ChatRouter-->>ApiClient: 200 OK { wuid, lid }
Loading

Updated class diagram for PN-to-LID resolution endpoint and services

classDiagram
  class ChatRouter {
    +post_fetchLid()
  }

  class ChatController {
    +fetchLid(instanceDto: InstanceDto, numberDto: NumberDto) Promise
  }

  class InstanceDto {
    +instanceName: string
  }

  class NumberDto {
    +number: string
  }

  class WAMonitor {
    +waInstances: Map
  }

  class WaInstance {
    +getLid(number: string) Promise
  }

  class BaileysStartupService {
    +getLid(number: string) Promise
    +getStatus(number: string) Promise
  }

  class BaileysClient {
    +signalRepository: SignalRepository
  }

  class SignalRepository {
    +lidMapping: LidMapping
  }

  class LidMapping {
    +getLIDForPN(jid: string) Promise
  }

  class FetchLidSchema {
  }

  ChatRouter --> ChatController : uses
  ChatRouter --> FetchLidSchema : validates_with
  ChatController --> InstanceDto : parameter
  ChatController --> NumberDto : parameter
  ChatController --> WAMonitor : uses
  WAMonitor --> WaInstance : waInstances
  WaInstance --> BaileysStartupService : delegates_getLid
  BaileysStartupService --> BaileysClient : uses
  BaileysClient --> SignalRepository : has
  SignalRepository --> LidMapping : has
  FetchLidSchema --> NumberDto : validates_number
Loading

File-Level Changes

Change Details Files
Expose Baileys LID resolution via a new service method used by the chat controller and router.
  • Add getLid(number) to BaileysStartupService to build a JID from the phone number, query signalRepository.lidMapping.getLIDForPN, and return a { wuid, lid } pair with null on failure or missing repository.
  • Add fetchLid(instanceName, data) method to ChatController that delegates to the Baileys instance getLid call.
  • Register POST /fetchLid route in ChatRouter that validates the body with fetchLidSchema, maps it into NumberDto, and invokes chatController.fetchLid.
  • Introduce fetchLidSchema JSON schema requiring a string number field for request validation.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
src/api/controllers/chat.controller.ts
src/api/routes/chat.router.ts
src/validate/chat.schema.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 found 1 issue, and left some high level feedback:

  • In getLid, the catch block silently swallows all errors; consider at least logging or narrowing the caught error types so unexpected failures are visible in monitoring.
  • The getLid method currently returns an untyped object { wuid, lid }; defining and reusing a dedicated return type/interface would make the contract clearer and reduce the risk of shape drift across callers.
  • For fetchLidSchema, consider reusing or aligning with the existing validation used for NumberDto (e.g., format/pattern, non-empty constraints) so phone number validation is consistent across endpoints.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `getLid`, the `catch` block silently swallows all errors; consider at least logging or narrowing the caught error types so unexpected failures are visible in monitoring.
- The `getLid` method currently returns an untyped object `{ wuid, lid }`; defining and reusing a dedicated return type/interface would make the contract clearer and reduce the risk of shape drift across callers.
- For `fetchLidSchema`, consider reusing or aligning with the existing validation used for `NumberDto` (e.g., format/pattern, non-empty constraints) so phone number validation is consistent across endpoints.

## Individual Comments

### Comment 1
<location path="src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts" line_range="2057-2066" />
<code_context>
     }
   }

+  public async getLid(number: string) {
+    const jid = createJid(number);
+
+    if (!this.client?.signalRepository) {
+      return { wuid: jid, lid: null };
+    }
+
+    try {
+      const lid = await this.client.signalRepository.lidMapping.getLIDForPN(jid);
+      return { wuid: jid, lid: lid || null };
+    } catch {
+      return { wuid: jid, lid: null };
+    }
</code_context>
<issue_to_address>
**issue (bug_risk):** Swallowing all errors and returning null LID may hide real failures.

This `try { ... } catch { return { wuid: jid, lid: null }; }` makes all failures in `getLIDForPN` (e.g., connectivity or internal errors) look identical to “no LID mapping”. If callers need to distinguish “no mapping” from “lookup failed”, narrow the catch to specific expected errors from `lidMapping`, or at least log unexpected errors before returning `null` so operational issues are visible.
</issue_to_address>

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.

@MrShennawy
Copy link
Author

MrShennawy commented Mar 23, 2026

I have addressed the first point by adding error logging in the catch block to ensure failures aren't swallowed silently (Commit: fix(pr-feedback): log error when failing to fetch LID for WUID).

Regarding the second point (defining a dedicated return type) and the third point (stricter validation in fetchLidSchema), I decided to keep them as is to maintain consistency with the rest of the codebase. Currently, similar methods and schemas in our project don't use dedicated return interfaces or strict pattern validations. I wanted to match the team's existing patterns rather than introducing a new standard in this specific PR.

Let me know if you'd still prefer me to update them!"

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