fix: use correct Prisma JSON path filter in getLastMessage#2515
fix: use correct Prisma JSON path filter in getLastMessage#2515octo-patch wants to merge 1 commit intoEvolutionAPI:mainfrom
Conversation
…lutionAPI#2495) The `getLastMessage` function was using an invalid Prisma where-clause syntax `{ key: { remoteJid: number } }` for a JSON/JSONB field. Prisma requires `{ key: { path: ['remoteJid'], equals: value } }` to filter on a JSON field path. This caused a PrismaClientValidationError whenever `archiveChat` or `markChatUnread` called `getLastMessage` with a chat number instead of providing lastMessage directly. Co-Authored-By: Octopus <liyuan851277048@icloud.com>
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts the Prisma JSON filter used in getLastMessage so that WhatsApp archive/unread operations correctly resolve the last message without throwing a PrismaClientValidationError. Sequence diagram for getLastMessage using correct Prisma JSON path filtersequenceDiagram
participant Client
participant ArchiveController
participant BaileysStartupService
participant PrismaRepository
participant Database
Client->>ArchiveController: POST /chat/archiveChat/{instance}
ArchiveController->>BaileysStartupService: getLastMessage(number)
BaileysStartupService->>BaileysStartupService: build where = { key: { path: ['remoteJid'], equals: number }, instanceId: this.instanceId }
BaileysStartupService->>PrismaRepository: message.findMany(where)
PrismaRepository->>Database: SELECT FROM message WHERE key->remoteJid = number AND instanceId = this.instanceId
Database-->>PrismaRepository: messages
PrismaRepository-->>BaileysStartupService: messages
BaileysStartupService-->>ArchiveController: lastMessage
ArchiveController-->>Client: 200 OK (chat archived)
Updated class diagram for BaileysStartupService getLastMessage queryclassDiagram
class BaileysStartupService {
- prismaRepository PrismaRepository
- instanceId string
+ getLastMessage(number string) Promise~Message[]~
}
class PrismaRepository {
+ message MessageDelegate
}
class MessageDelegate {
+ findMany(where any) Promise~Message[]~
}
class Message {
+ id string
+ key Json
+ instanceId string
+ content string
}
BaileysStartupService --> PrismaRepository : uses
PrismaRepository --> MessageDelegate : exposes
MessageDelegate --> Message : returns
class GetLastMessageWhereFilter {
+ key JsonPathFilter
+ instanceId string
}
class JsonPathFilter {
+ path string[]
+ equals string
}
BaileysStartupService ..> GetLastMessageWhereFilter : builds
GetLastMessageWhereFilter --> JsonPathFilter : contains
File-Level Changes
Assessment against linked issues
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 left some high level feedback:
- The change from
instanceId: this.instance.idtoinstanceId: this.instanceIdalters behavior beyond the Prisma JSON filter fix; please confirm this is intentional and consistent with howinstanceIdis handled in other methods of this service. - Instead of typing
whereasany, consider using the appropriate Prisma-generated type (e.g.,Prisma.MessageWhereInput) so the compiler can help catch invalid JSON filter shapes in the future.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The change from `instanceId: this.instance.id` to `instanceId: this.instanceId` alters behavior beyond the Prisma JSON filter fix; please confirm this is intentional and consistent with how `instanceId` is handled in other methods of this service.
- Instead of typing `where` as `any`, consider using the appropriate Prisma-generated type (e.g., `Prisma.MessageWhereInput`) so the compiler can help catch invalid JSON filter shapes in the future.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Thanks for the review! On the On the |
Fixes #2495
Problem
POST /chat/archiveChat/{instance}(andmarkChatUnread) always returns HTTP 500 with aPrismaClientValidationErrorwhen called with achatstring (nolastMessageprovided).The
getLastMessagehelper was building the Prismawhereclause as:The
keycolumn is aJson/JsonBfield. Prisma does not accept nested object syntax ({ remoteJid: value }) to filter inside a JSON column; it requires the explicit path form:Using the wrong form caused Prisma to throw
PrismaClientValidationError: Unknown argument 'remoteJid'.Solution
Changed the
whereclause ingetLastMessageto use the correct Prisma JSON path filter:This matches the pattern already used elsewhere in the codebase (e.g. lines 5060-5061).
Testing
The fix aligns with the established Prisma JSON filtering pattern used throughout the file.
archiveChatwith{"chat": "<jid>", "archive": true}will now correctly resolve the last message instead of throwing a validation error.Summary by Sourcery
Bug Fixes: