Add debug logging for markContactEmailsAsRead and remove password change restriction

This commit is contained in:
richardtekula
2025-11-20 08:00:14 +01:00
parent 51714c8edd
commit 178b18baa5
20 changed files with 152 additions and 394 deletions

View File

@@ -4,6 +4,7 @@ import * as emailAccountService from '../services/email-account.service.js';
import { markEmailAsRead, sendEmail, getJmapConfig, getJmapConfigFromAccount, syncEmailsFromSender, searchEmailsJMAP as searchEmailsJMAPService } from '../services/jmap.service.js';
import { formatErrorResponse } from '../utils/errors.js';
import { getUserById } from '../services/auth.service.js';
import { logger } from '../utils/logger.js';
/**
* Get all emails for authenticated user
@@ -91,7 +92,7 @@ export const getUnreadCount = async (req, res) => {
},
});
} catch (error) {
console.error('ERROR in getUnreadCount:', error);
logger.error('ERROR in getUnreadCount', { error: error.message });
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
@@ -153,7 +154,7 @@ export const syncEmails = async (req, res) => {
totalSynced += total;
totalNew += saved;
} catch (syncError) {
console.error(`Failed to sync emails for contact ${contact.email}`, syncError);
logger.error('Failed to sync emails for contact', { contactEmail: contact.email, error: syncError.message });
}
}
@@ -198,6 +199,29 @@ export const markAsRead = async (req, res) => {
}
};
/**
* Mark all emails from contact as read
* POST /api/emails/contact/:contactId/read
*/
export const markContactEmailsRead = async (req, res) => {
try {
const userId = req.userId;
const { contactId } = req.params;
const result = await crmEmailService.markContactEmailsAsRead(userId, contactId);
res.status(200).json({
success: true,
message: `Označených ${result.count} emailov ako prečítaných`,
data: result,
});
} catch (error) {
logger.error('ERROR in markContactEmailsRead', { error: error.message });
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
};
/**
* Mark entire thread as read
* POST /api/emails/thread/:threadId/read
@@ -224,7 +248,7 @@ export const markThreadRead = async (req, res) => {
try {
await markEmailAsRead(jmapConfig, userId, email.jmapId, true);
} catch (jmapError) {
console.error(`Failed to mark JMAP email ${email.jmapId} as read`, jmapError);
logger.error('Failed to mark JMAP email as read', { jmapId: email.jmapId, error: jmapError.message });
}
}
}
@@ -327,15 +351,15 @@ export const searchEmailsJMAP = async (req, res) => {
const userId = req.userId;
const { query = '', limit = 50, offset = 0, accountId } = req.query;
console.log('🔍 searchEmailsJMAP called:', { userId, query, limit, offset, accountId });
logger.debug('searchEmailsJMAP called', { userId, query, limit, offset, accountId });
// Get email account (or primary if not specified)
let emailAccount;
if (accountId) {
console.log('📧 Using provided accountId:', accountId);
logger.debug('Using provided accountId', { accountId });
emailAccount = await emailAccountService.getEmailAccountWithCredentials(accountId, userId);
} else {
console.log('📧 No accountId provided, using primary account');
logger.debug('No accountId provided, using primary account');
const primaryAccount = await emailAccountService.getPrimaryEmailAccount(userId);
if (!primaryAccount) {
return res.status(400).json({
@@ -347,7 +371,7 @@ export const searchEmailsJMAP = async (req, res) => {
});
}
emailAccount = await emailAccountService.getEmailAccountWithCredentials(primaryAccount.id, userId);
console.log('📧 Using primary account:', primaryAccount.id);
logger.debug('Using primary account', { accountId: primaryAccount.id });
}
const jmapConfig = getJmapConfigFromAccount(emailAccount);
@@ -366,7 +390,7 @@ export const searchEmailsJMAP = async (req, res) => {
data: results,
});
} catch (error) {
console.error('ERROR in searchEmailsJMAP:', error);
logger.error('ERROR in searchEmailsJMAP', { error: error.message });
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}