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

@@ -1,16 +1,18 @@
import * as contactService from '../services/contact.service.js';
import { discoverContactsFromJMAP, getJmapConfig } from '../services/jmap.service.js';
import { discoverContactsFromJMAP, getJmapConfigFromAccount } from '../services/jmap.service.js';
import { formatErrorResponse } from '../utils/errors.js';
import { getUserById } from '../services/auth.service.js';
import * as emailAccountService from '../services/email-account.service.js';
/**
* Get all contacts for authenticated user
* GET /api/contacts
* GET /api/contacts?accountId=xxx (optional)
*/
export const getContacts = async (req, res) => {
try {
const userId = req.userId;
const contacts = await contactService.getUserContacts(userId);
const { accountId } = req.query;
const contacts = await contactService.getUserContacts(userId, accountId || null);
res.status(200).json({
success: true,
@@ -25,28 +27,45 @@ export const getContacts = async (req, res) => {
/**
* Discover potential contacts from JMAP (email senders)
* GET /api/contacts/discover?search=query&limit=50
* GET /api/contacts/discover?accountId=xxx&search=query&limit=50
*/
export const discoverContacts = async (req, res) => {
try {
const userId = req.userId;
const { search = '', limit = 50 } = req.query;
const { accountId, search = '', limit = 50 } = req.query;
// Get user to access JMAP config
const user = await getUserById(userId);
console.log('🔍 discoverContacts called:', { userId, accountId, search, limit });
// Check if user has JMAP email configured
if (!user.email || !user.emailPassword || !user.jmapAccountId) {
return res.status(400).json({
success: false,
error: {
message: 'Najprv musíš pripojiť email účet v Profile',
statusCode: 400,
},
});
// Get email account (or primary if not specified)
let emailAccount;
if (accountId) {
console.log('📧 Getting email account by ID:', accountId);
emailAccount = await emailAccountService.getEmailAccountWithCredentials(accountId, userId);
console.log('✅ Email account retrieved:', { id: emailAccount.id, email: emailAccount.email });
} else {
console.log('📧 No accountId provided, getting primary account for user:', userId);
const primaryAccount = await emailAccountService.getPrimaryEmailAccount(userId);
console.log('🔑 Primary account:', primaryAccount ? { id: primaryAccount.id, email: primaryAccount.email } : 'NOT FOUND');
if (!primaryAccount) {
return res.status(400).json({
success: false,
error: {
message: 'Najprv musíš pripojiť email účet v Profile',
statusCode: 400,
},
});
}
emailAccount = await emailAccountService.getEmailAccountWithCredentials(primaryAccount.id, userId);
console.log('✅ Email account retrieved from primary:', { id: emailAccount.id, email: emailAccount.email });
}
const jmapConfig = getJmapConfig(user);
const jmapConfig = getJmapConfigFromAccount(emailAccount);
console.log('🔧 JMAP Config created:', {
server: jmapConfig.server,
username: jmapConfig.username,
accountId: jmapConfig.accountId,
hasPassword: !!jmapConfig.password
});
const potentialContacts = await discoverContactsFromJMAP(
jmapConfig,
@@ -61,6 +80,8 @@ export const discoverContacts = async (req, res) => {
data: potentialContacts,
});
} catch (error) {
console.error('❌ ERROR in discoverContacts:', error);
console.error('Error stack:', error.stack);
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
}
@@ -69,11 +90,15 @@ export const discoverContacts = async (req, res) => {
/**
* Add a new contact
* POST /api/contacts
* Body: { email, name, notes, accountId }
*/
export const addContact = async (req, res) => {
try {
const userId = req.userId;
const { email, name = '', notes = '' } = req.body;
console.log('📦 Full req.body:', JSON.stringify(req.body, null, 2));
const { email, name = '', notes = '', accountId } = req.body;
console.log(' addContact called:', { userId, email, name, accountId });
if (!email) {
return res.status(400).json({
@@ -85,11 +110,37 @@ export const addContact = async (req, res) => {
});
}
// Get user to access JMAP config
const user = await getUserById(userId);
const jmapConfig = getJmapConfig(user);
// Get email account (or primary if not specified)
let emailAccount;
if (accountId) {
console.log('📧 Using provided accountId:', accountId);
emailAccount = await emailAccountService.getEmailAccountWithCredentials(accountId, userId);
} else {
console.log('📧 No accountId provided, using primary account');
const primaryAccount = await emailAccountService.getPrimaryEmailAccount(userId);
if (!primaryAccount) {
return res.status(400).json({
success: false,
error: {
message: 'Najprv musíš pripojiť email účet v Profile',
statusCode: 400,
},
});
}
emailAccount = await emailAccountService.getEmailAccountWithCredentials(primaryAccount.id, userId);
console.log('📧 Using primary account:', primaryAccount.id);
}
const contact = await contactService.addContact(userId, jmapConfig, email, name, notes);
const jmapConfig = getJmapConfigFromAccount(emailAccount);
const contact = await contactService.addContact(
userId,
emailAccount.id,
jmapConfig,
email,
name,
notes
);
res.status(201).json({
success: true,