Add comprehensive debug logging for markContactEmailsAsRead to diagnose contactId mismatch

This commit is contained in:
richardtekula
2025-11-20 08:03:35 +01:00
parent 178b18baa5
commit ed8875c00e

View File

@@ -148,6 +148,19 @@ export const markThreadAsRead = async (userId, threadId) => {
export const markContactEmailsAsRead = async (userId, contactId) => {
console.log('🟦 markContactEmailsAsRead called:', { userId, contactId });
// Get the contact info first
const [contact] = await db
.select()
.from(contacts)
.where(eq(contacts.id, contactId))
.limit(1);
console.log('👤 Contact info:', {
id: contact?.id,
email: contact?.email,
name: contact?.name,
});
// First, check what emails exist for this contact (including already read ones)
const allContactEmails = await db
.select({
@@ -160,13 +173,42 @@ export const markContactEmailsAsRead = async (userId, contactId) => {
.from(emails)
.where(and(eq(emails.userId, userId), eq(emails.contactId, contactId)));
console.log('📧 All emails for this contact:', {
console.log('📧 All emails for this contact (by contactId):', {
total: allContactEmails.length,
unread: allContactEmails.filter(e => !e.isRead).length,
read: allContactEmails.filter(e => e.isRead).length,
sampleEmails: allContactEmails.slice(0, 3),
});
// Check if there are emails from this sender but with NULL or different contactId
if (contact) {
const emailsFromSender = await db
.select({
id: emails.id,
contactId: emails.contactId,
isRead: emails.isRead,
from: emails.from,
subject: emails.subject,
})
.from(emails)
.where(and(
eq(emails.userId, userId),
or(
eq(emails.from, contact.email),
like(emails.from, `%<${contact.email}>%`)
)
))
.limit(10);
console.log('📨 Emails from this sender email (any contactId):', {
total: emailsFromSender.length,
withContactId: emailsFromSender.filter(e => e.contactId === contactId).length,
withNullContactId: emailsFromSender.filter(e => e.contactId === null).length,
withDifferentContactId: emailsFromSender.filter(e => e.contactId && e.contactId !== contactId).length,
sampleEmails: emailsFromSender.slice(0, 3),
});
}
const result = await db
.update(emails)
.set({ isRead: true, updatedAt: new Date() })