Merge pull request #10 from ArnasDon/fix/webhook-messages-schema

fix: webhook message insert uses wrong schema field names
This commit is contained in:
Arnas Donauskas 2026-04-16 21:04:26 +03:00 committed by GitHub
commit b8923c6056
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -184,13 +184,13 @@ async function handleStatusUpdate(status: {
timestamp: string timestamp: string
recipient_id: string recipient_id: string
}) { }) {
// The messages table schema uses `message_id` (not whatsapp_message_id)
// and has no `updated_at` column. Meta's status values (sent/delivered/
// read/failed) already match our CHECK constraint.
const { error } = await supabaseAdmin() const { error } = await supabaseAdmin()
.from('messages') .from('messages')
.update({ .update({ status: status.status })
status: status.status, .eq('message_id', status.id)
updated_at: new Date().toISOString(),
})
.eq('whatsapp_message_id', status.id)
if (error) { if (error) {
console.error('Error updating message status:', error) console.error('Error updating message status:', error)
@ -227,18 +227,23 @@ async function processMessage(
) )
if (!conversation) return if (!conversation) return
// Insert message // Insert message — field names MUST match the messages table schema
// (see supabase/migrations/001_initial_schema.sql):
// conversation_id, sender_type, content_type, content_text,
// media_url, template_name, message_id, status, created_at
// `mediaType` is intentionally unused — the schema has no media_type
// column; the MIME type is only used to construct the proxy URL during
// parseMessageContent. Silence the unused-var warning:
void mediaType
const { error: msgError } = await supabaseAdmin().from('messages').insert({ const { error: msgError } = await supabaseAdmin().from('messages').insert({
conversation_id: conversation.id, conversation_id: conversation.id,
user_id: userId, sender_type: 'customer',
whatsapp_message_id: message.id, content_type: message.type,
direction: 'inbound',
message_type: message.type,
content_text: contentText, content_text: contentText,
media_url: mediaUrl, media_url: mediaUrl,
media_type: mediaType, message_id: message.id,
status: 'received', status: 'delivered',
timestamp: new Date(parseInt(message.timestamp) * 1000).toISOString(), created_at: new Date(parseInt(message.timestamp) * 1000).toISOString(),
}) })
if (msgError) { if (msgError) {