initialize git, basic setup for crm
This commit is contained in:
147
src/controllers/contact.controller.js
Normal file
147
src/controllers/contact.controller.js
Normal file
@@ -0,0 +1,147 @@
|
||||
import * as contactService from '../services/contact.service.js';
|
||||
import { discoverContactsFromJMAP, getJmapConfig } from '../services/jmap.service.js';
|
||||
import { formatErrorResponse } from '../utils/errors.js';
|
||||
import { getUserById } from '../services/auth.service.js';
|
||||
|
||||
/**
|
||||
* Get all contacts for authenticated user
|
||||
* GET /api/contacts
|
||||
*/
|
||||
export const getContacts = async (req, res) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const contacts = await contactService.getUserContacts(userId);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
count: contacts.length,
|
||||
data: contacts,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Discover potential contacts from JMAP (email senders)
|
||||
* GET /api/contacts/discover?search=query&limit=50
|
||||
*/
|
||||
export const discoverContacts = async (req, res) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { search = '', limit = 50 } = req.query;
|
||||
|
||||
// Get user to access JMAP config
|
||||
const user = await getUserById(userId);
|
||||
|
||||
// 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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const jmapConfig = getJmapConfig(user);
|
||||
|
||||
const potentialContacts = await discoverContactsFromJMAP(
|
||||
jmapConfig,
|
||||
userId,
|
||||
search,
|
||||
parseInt(limit)
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
count: potentialContacts.length,
|
||||
data: potentialContacts,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a new contact
|
||||
* POST /api/contacts
|
||||
*/
|
||||
export const addContact = async (req, res) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { email, name = '', notes = '' } = req.body;
|
||||
|
||||
if (!email) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: {
|
||||
message: 'Email je povinný',
|
||||
statusCode: 400,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Get user to access JMAP config
|
||||
const user = await getUserById(userId);
|
||||
const jmapConfig = getJmapConfig(user);
|
||||
|
||||
const contact = await contactService.addContact(userId, jmapConfig, email, name, notes);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: contact,
|
||||
message: 'Kontakt pridaný a emaily synchronizované',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove a contact
|
||||
* DELETE /api/contacts/:contactId
|
||||
*/
|
||||
export const removeContact = async (req, res) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { contactId } = req.params;
|
||||
|
||||
const result = await contactService.removeContact(userId, contactId);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: result.message,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Update a contact
|
||||
* PATCH /api/contacts/:contactId
|
||||
*/
|
||||
export const updateContact = async (req, res) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { contactId } = req.params;
|
||||
const { name, notes } = req.body;
|
||||
|
||||
const updated = await contactService.updateContact(userId, contactId, { name, notes });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: updated,
|
||||
message: 'Kontakt aktualizovaný',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user