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

@@ -128,7 +128,7 @@ export const getAllUsers = async (req, res) => {
* Získanie konkrétneho usera (admin only)
* GET /api/admin/users/:userId
*/
export const getUserById = async (req, res) => {
export const getUser = async (req, res) => {
const { userId } = req.params;
try {

View File

@@ -2,6 +2,7 @@ import * as contactService from '../services/contact.service.js';
import { discoverContactsFromJMAP, getJmapConfigFromAccount } from '../services/jmap.service.js';
import { formatErrorResponse } from '../utils/errors.js';
import * as emailAccountService from '../services/email-account.service.js';
import { logger } from '../utils/logger.js';
/**
* Get all contacts for authenticated user
@@ -34,18 +35,18 @@ export const discoverContacts = async (req, res) => {
const userId = req.userId;
const { accountId, search = '', limit = 50 } = req.query;
console.log('🔍 discoverContacts called:', { userId, accountId, search, limit });
logger.debug('discoverContacts called', { userId, accountId, search, limit });
// Get email account (or primary if not specified)
let emailAccount;
if (accountId) {
console.log('📧 Getting email account by ID:', accountId);
logger.debug('Getting email account by ID', { accountId });
emailAccount = await emailAccountService.getEmailAccountWithCredentials(accountId, userId);
console.log('Email account retrieved:', { id: emailAccount.id, email: emailAccount.email });
logger.debug('Email account retrieved', { id: emailAccount.id, email: emailAccount.email });
} else {
console.log('📧 No accountId provided, getting primary account for user:', userId);
logger.debug('No accountId provided, getting primary account', { userId });
const primaryAccount = await emailAccountService.getPrimaryEmailAccount(userId);
console.log('🔑 Primary account:', primaryAccount ? { id: primaryAccount.id, email: primaryAccount.email } : 'NOT FOUND');
logger.debug('Primary account', primaryAccount ? { id: primaryAccount.id, email: primaryAccount.email } : { found: false });
if (!primaryAccount) {
return res.status(400).json({
success: false,
@@ -56,11 +57,11 @@ export const discoverContacts = async (req, res) => {
});
}
emailAccount = await emailAccountService.getEmailAccountWithCredentials(primaryAccount.id, userId);
console.log('Email account retrieved from primary:', { id: emailAccount.id, email: emailAccount.email });
logger.debug('Email account retrieved from primary', { id: emailAccount.id, email: emailAccount.email });
}
const jmapConfig = getJmapConfigFromAccount(emailAccount);
console.log('🔧 JMAP Config created:', {
logger.debug('JMAP Config created', {
server: jmapConfig.server,
username: jmapConfig.username,
accountId: jmapConfig.accountId,
@@ -80,8 +81,7 @@ export const discoverContacts = async (req, res) => {
data: potentialContacts,
});
} catch (error) {
console.error('ERROR in discoverContacts:', error);
console.error('Error stack:', error.stack);
logger.error('ERROR in discoverContacts', { error: error.message, stack: error.stack });
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
@@ -95,10 +95,10 @@ export const discoverContacts = async (req, res) => {
export const addContact = async (req, res) => {
try {
const userId = req.userId;
console.log('📦 Full req.body:', JSON.stringify(req.body, null, 2));
logger.debug('Full req.body', { body: req.body });
const { email, name = '', notes = '', accountId } = req.body;
console.log(' addContact called:', { userId, email, name, accountId });
logger.debug('addContact called', { userId, email, name, accountId });
if (!email) {
return res.status(400).json({
@@ -113,10 +113,10 @@ export const addContact = async (req, res) => {
// 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({
@@ -128,7 +128,7 @@ export const addContact = 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);

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);
}