option for more emails,fix jmap service,add table email accounts

This commit is contained in:
richardtekula
2025-11-19 13:15:45 +01:00
parent 97f437c1c4
commit 1e7c1eab90
18 changed files with 1991 additions and 1299 deletions

View File

@@ -0,0 +1,174 @@
import * as emailAccountService from '../services/email-account.service.js';
import {
logEmailLink,
} from '../services/audit.service.js';
import { formatErrorResponse } from '../utils/errors.js';
/**
* Get all email accounts for logged-in user
* GET /api/email-accounts
*/
export const getEmailAccounts = async (req, res) => {
try {
const userId = req.userId;
const accounts = await emailAccountService.getUserEmailAccounts(userId);
res.status(200).json({
success: true,
data: accounts,
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
};
/**
* Get a specific email account
* GET /api/email-accounts/:id
*/
export const getEmailAccount = async (req, res) => {
try {
const userId = req.userId;
const { id } = req.params;
const account = await emailAccountService.getEmailAccountById(id, userId);
res.status(200).json({
success: true,
data: account,
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
};
/**
* Create a new email account
* POST /api/email-accounts
*/
export const createEmailAccount = async (req, res) => {
const { email, emailPassword } = req.body;
const userId = req.userId;
const ipAddress = req.ip || req.connection.remoteAddress;
const userAgent = req.headers['user-agent'];
try {
const account = await emailAccountService.createEmailAccount(
userId,
email,
emailPassword
);
// Log email account creation
await logEmailLink(userId, email, ipAddress, userAgent);
res.status(201).json({
success: true,
data: account,
message: 'Email účet úspešne pripojený',
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
};
/**
* Update email account password
* PATCH /api/email-accounts/:id/password
*/
export const updateEmailAccountPassword = async (req, res) => {
try {
const userId = req.userId;
const { id } = req.params;
const { emailPassword } = req.body;
const result = await emailAccountService.updateEmailAccountPassword(
id,
userId,
emailPassword
);
res.status(200).json({
success: true,
data: result,
message: 'Heslo k emailovému účtu bolo aktualizované',
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
};
/**
* Toggle email account active status
* PATCH /api/email-accounts/:id/status
*/
export const toggleEmailAccountStatus = async (req, res) => {
try {
const userId = req.userId;
const { id } = req.params;
const { isActive } = req.body;
const result = await emailAccountService.toggleEmailAccountStatus(
id,
userId,
isActive
);
res.status(200).json({
success: true,
data: result,
message: `Email účet ${isActive ? 'aktivovaný' : 'deaktivovaný'}`,
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
};
/**
* Set email account as primary
* POST /api/email-accounts/:id/set-primary
*/
export const setPrimaryEmailAccount = async (req, res) => {
try {
const userId = req.userId;
const { id } = req.params;
const result = await emailAccountService.setPrimaryEmailAccount(id, userId);
res.status(200).json({
success: true,
data: result,
message: 'Primárny email účet bol nastavený',
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
};
/**
* Delete email account
* DELETE /api/email-accounts/:id
*/
export const deleteEmailAccount = async (req, res) => {
try {
const userId = req.userId;
const { id } = req.params;
const result = await emailAccountService.deleteEmailAccount(id, userId);
res.status(200).json({
success: true,
data: result,
message: result.message,
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
};