fix(engine): resolve lint-staged and json path typing#2481
fix(engine): resolve lint-staged and json path typing#2481shajandevz wants to merge 1 commit intoEvolutionAPI:mainfrom
Conversation
Reviewer's GuideThis pull request fixes Prisma JSON path usage for message keys, adjusts label persistence logic, adds MySQL-specific raw SQL for message read/unread calculations, simplifies the WhatsApp cache lid handling, wires ESLint to work correctly with lint-staged via the flat config env flag, and introduces an Nx project.json for the engine app. Flow diagram for updateChatUnreadMessages with MySQL and Postgres pathsflowchart TD
A[updateChatUnreadMessages remoteJid] --> B[Read DATABASE.PROVIDER from configService]
B --> C[Normalize provider to lowercase]
C --> D{dbProvider is mysql}
D -->|yes| E[Find chat by remoteJid via prismaRepository.chat.findFirst]
E --> F[Query unreadMessages count with MySQL raw SQL using JSON_EXTRACT on key.remoteJid and key.fromMe]
D -->|no| G[Find chat by remoteJid via prismaRepository.chat.findFirst]
G --> H[Query unreadMessages count with Postgres raw SQL using key->>'remoteJid' and key->>'fromMe']
F --> I[Receive unreadMessages number]
H --> I[Receive unreadMessages number]
I --> J{chat exists and chat.unreadMessages != unreadMessages}
J -->|yes| K[Update chat.unreadMessages with unreadMessages]
J -->|no| L[Skip chat update]
K --> M[Return unreadMessages]
L --> M[Return unreadMessages]
Flow diagram for saveOnWhatsappCache with simplified lid handlingflowchart TD
A[saveOnWhatsappCache data array] --> B[Iterate over each item]
B --> C[Normalize remoteJid]
C --> D[Build finalJidOptions set]
D --> E[Sort finalJidOptions]
E --> F[Create newJidOptionsString]
F --> G[Build dataPayload with remoteJid and jidOptions]
G --> H[Search existingRecord in prismaRepository.isOnWhatsapp by remoteJid]
H --> I{existingRecord found}
I -->|yes| J[Build existingJidOptionsString from existingRecord.jidOptions]
J --> K[Compare existingRecord.remoteJid and existingJidOptionsString with dataPayload]
K --> L{Data is same}
L -->|yes| M[Log verbose: data up to date, skip update]
L -->|no| N[Log verbose: updating register]
N --> O[Update existingRecord with dataPayload]
M --> P[Continue with next item]
O --> P[Continue with next item]
I -->|no| Q[Log verbose: creating register]
Q --> R[Create new record in prismaRepository.isOnWhatsapp with dataPayload]
R --> P[Continue with next item]
Flow diagram for getOnWhatsappCache lid resolution at read timeflowchart TD
A[getOnWhatsappCache remoteJids] --> B[Fetch records from prismaRepository.isOnWhatsapp]
B --> C[Iterate over each item]
C --> D[Split item.jidOptions into array]
D --> E[Compute number from item.remoteJid prefix]
E --> F[Determine lid]
F --> G{item has lid property}
G -->|yes| H[Use item.lid]
G -->|no| I{item.remoteJid includes @lid}
I -->|yes| J[Set lid to lid]
I -->|no| K[Set lid to undefined]
H --> L[Build output object with remoteJid number jidOptions and lid]
J --> L[Build output object with remoteJid number jidOptions and lid]
K --> L[Build output object with remoteJid number jidOptions and lid]
L --> M[Collect all output objects and return]
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The new JSON path usage (e.g.
key: { path: '$.id', ... }) diverges from Prisma’s typicalstring[]path typing; consider encapsulating provider-specific JSON path handling in a small helper or type-safe abstraction instead of sprinkling raw JSONPath strings throughout the code. - The
fetchMessagesmethod now returnsPromise<any>; tightening this to the concrete Prisma return type (or a well-defined interface) will help catch issues at compile time and make the call sites clearer. - The new
project.jsondeclaressourceRoot: 'apps/engine/src'and uses a$schemapath with../../node_modules, which suggests it may be intended to live underapps/engine; double-check the file’s location and paths so Nx picks it up correctly without relying on potentially incorrect relative paths.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new JSON path usage (e.g. `key: { path: '$.id', ... }`) diverges from Prisma’s typical `string[]` path typing; consider encapsulating provider-specific JSON path handling in a small helper or type-safe abstraction instead of sprinkling raw JSONPath strings throughout the code.
- The `fetchMessages` method now returns `Promise<any>`; tightening this to the concrete Prisma return type (or a well-defined interface) will help catch issues at compile time and make the call sites clearer.
- The new `project.json` declares `sourceRoot: 'apps/engine/src'` and uses a `$schema` path with `../../node_modules`, which suggests it may be intended to live under `apps/engine`; double-check the file’s location and paths so Nx picks it up correctly without relying on potentially incorrect relative paths.
## Individual Comments
### Comment 1
<location path="src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts" line_range="5050" />
<code_context>
}
- public async fetchMessages(query: Query<Message>) {
+ public async fetchMessages(query: Query<Message>): Promise<any> {
const keyFilters = query?.where?.key as ExtendedIMessageKey;
</code_context>
<issue_to_address>
**suggestion:** The `fetchMessages` return type `Promise<any>` loses useful typing information.
The method used to infer its return type from `prismaRepository.message.findMany`/`findFirst`, but explicitly returning `Promise<any>` hides the data shape and removes type safety and editor/compiler assistance for callers.
Please narrow the return type to the concrete message type (e.g. `Promise<Message[]>` or an appropriate Prisma type such as `Prisma.MessageGetPayload<...>`), matching what the method actually returns, so the service API remains type-safe without changing runtime behavior.
```suggestion
public async fetchMessages(query: Query<Message>) {
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| } | ||
|
|
||
| public async fetchMessages(query: Query<Message>) { | ||
| public async fetchMessages(query: Query<Message>): Promise<any> { |
There was a problem hiding this comment.
suggestion: The fetchMessages return type Promise<any> loses useful typing information.
The method used to infer its return type from prismaRepository.message.findMany/findFirst, but explicitly returning Promise<any> hides the data shape and removes type safety and editor/compiler assistance for callers.
Please narrow the return type to the concrete message type (e.g. Promise<Message[]> or an appropriate Prisma type such as Prisma.MessageGetPayload<...>), matching what the method actually returns, so the service API remains type-safe without changing runtime behavior.
| public async fetchMessages(query: Query<Message>): Promise<any> { | |
| public async fetchMessages(query: Query<Message>) { |
📋 Description
🔗 Related Issue
Closes #(issue_number)
🧪 Type of Change
🧪 Testing
📸 Screenshots (if applicable)
✅ Checklist
📝 Additional Notes
Summary by Sourcery
Align message JSON path queries and label persistence with database requirements, and update tooling configuration for linting and Nx integration.
Bug Fixes:
Enhancements:
Build: