Mark emails as read on JMAP server (fixes Betterbird showing unread)
This commit is contained in:
@@ -208,8 +208,39 @@ export const markContactEmailsRead = async (req, res) => {
|
|||||||
const userId = req.userId;
|
const userId = req.userId;
|
||||||
const { contactId } = req.params;
|
const { contactId } = req.params;
|
||||||
|
|
||||||
|
// Get contact to find which email account it belongs to
|
||||||
|
const contact = await contactService.getContactById(contactId, userId);
|
||||||
|
if (!contact) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
error: { message: 'Kontakt nenájdený', statusCode: 404 },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get email account with credentials
|
||||||
|
const emailAccount = await emailAccountService.getEmailAccountWithCredentials(contact.emailAccountId, userId);
|
||||||
|
const jmapConfig = getJmapConfigFromAccount(emailAccount);
|
||||||
|
|
||||||
|
// Mark emails as read in database and get the updated emails
|
||||||
const result = await crmEmailService.markContactEmailsAsRead(userId, contactId);
|
const result = await crmEmailService.markContactEmailsAsRead(userId, contactId);
|
||||||
|
|
||||||
|
// Also mark emails as read on JMAP server
|
||||||
|
if (result.emails && result.emails.length > 0) {
|
||||||
|
logger.info(`Marking ${result.emails.length} emails as read on JMAP server`);
|
||||||
|
|
||||||
|
for (const email of result.emails) {
|
||||||
|
if (!email.jmapId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await markEmailAsRead(jmapConfig, userId, email.jmapId, true);
|
||||||
|
logger.debug(`✅ Marked JMAP email as read: ${email.jmapId}`);
|
||||||
|
} catch (jmapError) {
|
||||||
|
logger.error('Failed to mark JMAP email as read', { jmapId: email.jmapId, error: jmapError.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
message: `Označených ${result.count} emailov ako prečítaných`,
|
message: `Označených ${result.count} emailov ako prečítaných`,
|
||||||
|
|||||||
@@ -69,6 +69,23 @@ export const addContact = async (userId, emailAccountId, jmapConfig, email, name
|
|||||||
return newContact;
|
return newContact;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a contact by ID
|
||||||
|
*/
|
||||||
|
export const getContactById = async (contactId, userId) => {
|
||||||
|
const [contact] = await db
|
||||||
|
.select()
|
||||||
|
.from(contacts)
|
||||||
|
.where(and(eq(contacts.id, contactId), eq(contacts.userId, userId)))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!contact) {
|
||||||
|
throw new NotFoundError('Kontakt nenájdený');
|
||||||
|
}
|
||||||
|
|
||||||
|
return contact;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a contact
|
* Remove a contact
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -246,7 +246,11 @@ export const markContactEmailsAsRead = async (userId, contactId) => {
|
|||||||
|
|
||||||
console.log('✅ markContactEmailsAsRead result:', { count: result.length, contactId });
|
console.log('✅ markContactEmailsAsRead result:', { count: result.length, contactId });
|
||||||
|
|
||||||
return { success: true, count: result.length };
|
return {
|
||||||
|
success: true,
|
||||||
|
count: result.length,
|
||||||
|
emails: result, // Return the emails so controller can mark them on JMAP server
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user