Refactor: code quality improvements

- Extract admin.service.js from admin.controller.js (proper layering)
- Remove console.log statements from todo.controller.js
- Fix inconsistent error handling in auth.controller.js (return next)
- Remove logger.debug calls from contact.controller.js
- Add transaction management to contact.service.js addContact()

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
richardtekula
2025-12-05 07:25:49 +01:00
parent ad93b3b2a9
commit 81f75d285e
6 changed files with 303 additions and 263 deletions

View File

@@ -1,7 +1,6 @@
import * as contactService from '../services/contact.service.js';
import { discoverContactsFromJMAP, getJmapConfigFromAccount } from '../services/jmap.service.js';
import * as emailAccountService from '../services/email-account.service.js';
import { logger } from '../utils/logger.js';
/**
* Get all contacts for an email account
@@ -47,18 +46,12 @@ export const discoverContacts = async (req, res, next) => {
const userId = req.userId;
const { accountId, search = '', limit = 50 } = req.query;
logger.debug('discoverContacts called', { userId, accountId, search, limit });
// Get email account (or primary if not specified)
let emailAccount;
if (accountId) {
logger.debug('Getting email account by ID', { accountId });
emailAccount = await emailAccountService.getEmailAccountWithCredentials(accountId, userId);
logger.debug('Email account retrieved', { id: emailAccount.id, email: emailAccount.email });
} else {
logger.debug('No accountId provided, getting primary account', { userId });
const primaryAccount = await emailAccountService.getPrimaryEmailAccount(userId);
logger.debug('Primary account', primaryAccount ? { id: primaryAccount.id, email: primaryAccount.email } : { found: false });
if (!primaryAccount) {
return res.status(400).json({
success: false,
@@ -69,16 +62,9 @@ export const discoverContacts = async (req, res, next) => {
});
}
emailAccount = await emailAccountService.getEmailAccountWithCredentials(primaryAccount.id, userId);
logger.debug('Email account retrieved from primary', { id: emailAccount.id, email: emailAccount.email });
}
const jmapConfig = getJmapConfigFromAccount(emailAccount);
logger.debug('JMAP Config created', {
server: jmapConfig.server,
username: jmapConfig.username,
accountId: jmapConfig.accountId,
hasPassword: !!jmapConfig.password
});
const potentialContacts = await discoverContactsFromJMAP(
jmapConfig,
@@ -93,7 +79,6 @@ export const discoverContacts = async (req, res, next) => {
data: potentialContacts,
});
} catch (error) {
logger.error('ERROR in discoverContacts', { error: error.message, stack: error.stack });
return next(error);
}
};
@@ -106,11 +91,8 @@ export const discoverContacts = async (req, res, next) => {
export const addContact = async (req, res, next) => {
try {
const userId = req.userId;
logger.debug('Full req.body', { body: req.body });
const { email, name = '', notes = '', accountId } = req.body;
logger.debug('addContact called', { userId, email, name, accountId });
if (!email) {
return res.status(400).json({
success: false,
@@ -124,10 +106,8 @@ export const addContact = async (req, res, next) => {
// Get email account (or primary if not specified)
let emailAccount;
if (accountId) {
logger.debug('Using provided accountId', { accountId });
emailAccount = await emailAccountService.getEmailAccountWithCredentials(accountId, userId);
} else {
logger.debug('No accountId provided, using primary account');
const primaryAccount = await emailAccountService.getPrimaryEmailAccount(userId);
if (!primaryAccount) {
return res.status(400).json({
@@ -139,7 +119,6 @@ export const addContact = async (req, res, next) => {
});
}
emailAccount = await emailAccountService.getEmailAccountWithCredentials(primaryAccount.id, userId);
logger.debug('Using primary account', { accountId: primaryAccount.id });
}
const jmapConfig = getJmapConfigFromAccount(emailAccount);