fix(send-media): use upsert to prevent P2002 duplicate messageId error
MessageHandler can insert the echo of a sent message before the HTTP handler does — replace prisma.message.create() with upsert() so both paths converge safely instead of crashing with a unique constraint error. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -167,8 +167,10 @@ export function buildMediaRoutes(manager: WhatsAppConnectionManager): Router {
|
||||
})
|
||||
|
||||
const messageType = mediaType.toUpperCase() as any
|
||||
const msg = await prisma.message.create({
|
||||
data: {
|
||||
// Usa upsert para evitar P2002 — o MessageHandler pode ter inserido antes
|
||||
const msg = await prisma.message.upsert({
|
||||
where: { instanceId_messageId: { instanceId, messageId } },
|
||||
create: {
|
||||
tenantId,
|
||||
instanceId,
|
||||
chatId: chat.id,
|
||||
@@ -183,6 +185,11 @@ export function buildMediaRoutes(manager: WhatsAppConnectionManager): Router {
|
||||
status: 'SENT',
|
||||
timestamp: new Date(),
|
||||
},
|
||||
update: {
|
||||
mediaUrl,
|
||||
mimeType: mimetype,
|
||||
status: 'SENT',
|
||||
},
|
||||
})
|
||||
|
||||
logger.info({ instanceId, jid, mediaType, size }, 'Mídia enviada')
|
||||
|
||||
Reference in New Issue
Block a user