Code quality improvements from code review

- Add admin-only authorization for company and projects CRUD operations
- Create requireAccountId middleware to eliminate code duplication
- Standardize error handling (use next(error) consistently)
- Change error messages to Slovak language

🤖 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 11:03:32 +01:00
parent 03b7a215bb
commit 6f4a31e9de
19 changed files with 186 additions and 191 deletions

View File

@@ -12,17 +12,7 @@ import { logger } from '../utils/logger.js';
export const getEmails = async (req, res, next) => {
try {
const userId = req.userId;
const { accountId } = req.query;
if (!accountId) {
return res.status(400).json({
success: false,
error: {
message: 'accountId je povinný parameter',
statusCode: 400,
},
});
}
const { accountId } = req;
// Verify user has access to this email account
await emailAccountService.getEmailAccountById(accountId, userId);
@@ -35,7 +25,7 @@ export const getEmails = async (req, res, next) => {
data: emails,
});
} catch (error) {
return next(error);
next(error);
}
};
@@ -47,17 +37,7 @@ export const getThread = async (req, res, next) => {
try {
const userId = req.userId;
const { threadId } = req.params;
const { accountId } = req.query;
if (!accountId) {
return res.status(400).json({
success: false,
error: {
message: 'accountId je povinný parameter',
statusCode: 400,
},
});
}
const { accountId } = req;
// Verify user has access to this email account
await emailAccountService.getEmailAccountById(accountId, userId);
@@ -70,7 +50,7 @@ export const getThread = async (req, res, next) => {
data: thread,
});
} catch (error) {
return next(error);
next(error);
}
};
@@ -81,17 +61,8 @@ export const getThread = async (req, res, next) => {
export const searchEmails = async (req, res, next) => {
try {
const userId = req.userId;
const { q, accountId } = req.query;
if (!accountId) {
return res.status(400).json({
success: false,
error: {
message: 'accountId je povinný parameter',
statusCode: 400,
},
});
}
const { q } = req.query;
const { accountId } = req;
// Verify user has access to this email account
await emailAccountService.getEmailAccountById(accountId, userId);
@@ -104,7 +75,7 @@ export const searchEmails = async (req, res, next) => {
data: results,
});
} catch (error) {
return next(error);
next(error);
}
};
@@ -135,7 +106,7 @@ export const getUnreadCount = async (req, res, next) => {
});
} catch (error) {
logger.error('ERROR in getUnreadCount', { error: error.message });
return next(error);
next(error);
}
};
@@ -198,7 +169,7 @@ export const syncEmails = async (req, res, next) => {
}
}
return res.status(200).json({
res.status(200).json({
success: true,
message: 'Emaily synchronizované',
data: {
@@ -208,7 +179,7 @@ export const syncEmails = async (req, res, next) => {
},
});
} catch (error) {
return next(error);
next(error);
}
};
@@ -220,7 +191,8 @@ export const markAsRead = async (req, res, next) => {
try {
const userId = req.userId;
const { jmapId } = req.params;
const { isRead, accountId } = req.body;
const { isRead } = req.body;
const accountId = req.accountId || req.body.accountId;
if (!accountId) {
return res.status(400).json({
@@ -243,7 +215,7 @@ export const markAsRead = async (req, res, next) => {
message: `Email označený ako ${isRead ? 'prečítaný' : 'neprečítaný'}`,
});
} catch (error) {
return next(error);
next(error);
}
};
@@ -255,17 +227,7 @@ export const markContactEmailsRead = async (req, res, next) => {
try {
const userId = req.userId;
const { contactId } = req.params;
const { accountId } = req.query;
if (!accountId) {
return res.status(400).json({
success: false,
error: {
message: 'accountId je povinný parameter',
statusCode: 400,
},
});
}
const { accountId } = req;
// Verify user has access to this email account
const emailAccount = await emailAccountService.getEmailAccountWithCredentials(accountId, userId);
@@ -287,9 +249,8 @@ export const markContactEmailsRead = async (req, res, next) => {
}
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 });
logger.error('Nepodarilo sa označiť JMAP email ako prečítaný', { jmapId: email.jmapId, error: jmapError.message });
}
}
}
@@ -301,7 +262,7 @@ export const markContactEmailsRead = async (req, res, next) => {
});
} catch (error) {
logger.error('ERROR in markContactEmailsRead', { error: error.message });
return next(error);
next(error);
}
};
@@ -313,17 +274,7 @@ export const markThreadRead = async (req, res, next) => {
try {
const userId = req.userId;
const { threadId } = req.params;
const { accountId } = req.query;
if (!accountId) {
return res.status(400).json({
success: false,
error: {
message: 'accountId je povinný parameter',
statusCode: 400,
},
});
}
const { accountId } = req;
// Verify user has access to this email account
const emailAccount = await emailAccountService.getEmailAccountWithCredentials(accountId, userId);
@@ -340,7 +291,7 @@ export const markThreadRead = async (req, res, next) => {
try {
await markEmailAsRead(jmapConfig, userId, email.jmapId, true);
} catch (jmapError) {
logger.error('Failed to mark JMAP email as read', { jmapId: email.jmapId, error: jmapError.message });
logger.error('Nepodarilo sa označiť JMAP email ako prečítaný', { jmapId: email.jmapId, error: jmapError.message });
}
}
@@ -353,7 +304,7 @@ export const markThreadRead = async (req, res, next) => {
count,
});
} catch (error) {
return next(error);
next(error);
}
};
@@ -405,7 +356,7 @@ export const replyToEmail = async (req, res, next) => {
data: result,
});
} catch (error) {
return next(error);
next(error);
}
};
@@ -417,7 +368,7 @@ export const getContactEmails = async (req, res, next) => {
try {
const userId = req.userId;
const { contactId } = req.params;
const { accountId } = req.query;
const accountId = req.accountId || req.query.accountId;
if (!accountId) {
return res.status(400).json({
@@ -440,7 +391,7 @@ export const getContactEmails = async (req, res, next) => {
data: emails,
});
} catch (error) {
return next(error);
next(error);
}
};
@@ -494,6 +445,6 @@ export const searchEmailsJMAP = async (req, res, next) => {
});
} catch (error) {
logger.error('ERROR in searchEmailsJMAP', { error: error.message });
return next(error);
next(error);
}
};