fix(whatsapp): guard messageUpdate.create against missing parent message #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This PR addresses a critical issue in the messages.update handler that
caused PrismaClientValidationError and led to crash loops.
The Bug:
The messages.update handler in whatsapp.baileys.service.ts was
attempting to call prismaRepository.messageUpdate.create without
ensuring the existence of a parent Message record. When an update event
arrived for a message that was not stored in the Message table (e.g.,
ephemeral messages, old messages not synced, or deleted messages), the
messageId required for the MessageUpdate relation would be missing. This
resulted in a PrismaClientValidationError with the message "Argument
Message is missing," causing the application to crash. The previous logs
showed a misleading artifact Invalid this.cache.delete() invocation, but
the core issue was the missing Prisma relation.
The Fix:
messageUpdate.create: Implemented a conditional check forthe presence of message.messageId before attempting to create a
MessageUpdate record.
logs a warning ([WIDGET-WORKS] Skipping messageUpdate.create for key
...: Parent message not found.) and skips the database write,
preventing the crash.
for clarity and adherence to project conventions.
Impact:
the messages.update handler.
updates, even if the parent message is not found in the database.
Additional Notes:
already wrapped in a try/catch block, providing a safety net for
potential issues in cache deletion.