feat(cardav): implement POST /accounts endpoint
Add account creation route with validation. Delegates to CardDAVSync.addAccount() which creates account and discovers addressbooks. Returns 201 with account + addressbooks array. Add _mockTestConnection() helper for testing CardDAV routes without real server connections. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,8 +7,10 @@
|
||||
import { createLogger } from '../logger.js';
|
||||
import express from 'express';
|
||||
import * as CardDAVSync from '../services/cardav-sync.js';
|
||||
import { str, collectErrors, MAX_TITLE } from '../middleware/validate.js';
|
||||
|
||||
const log = createLogger('CardDAV');
|
||||
const MAX_URL = 500;
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
@@ -26,4 +28,33 @@ router.get('/accounts', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/v1/contacts/cardav/accounts
|
||||
* Neuen CardDAV Account erstellen und Addressbooks discovern.
|
||||
* Body: { name, cardavUrl, username, password }
|
||||
* Response: { data: { account, addressbooks } }
|
||||
*/
|
||||
router.post('/accounts', async (req, res) => {
|
||||
try {
|
||||
const vName = str(req.body.name, 'Name', { max: MAX_TITLE });
|
||||
const vUrl = str(req.body.cardavUrl, 'CardDAV URL', { max: MAX_URL });
|
||||
const vUsername = str(req.body.username, 'Username', { max: MAX_TITLE });
|
||||
const vPassword = str(req.body.password, 'Password', { max: MAX_TITLE });
|
||||
const errors = collectErrors([vName, vUrl, vUsername, vPassword]);
|
||||
if (errors.length) return res.status(400).json({ error: errors.join(' '), code: 400 });
|
||||
|
||||
const result = await CardDAVSync.addAccount(
|
||||
vName.value,
|
||||
vUrl.value,
|
||||
vUsername.value,
|
||||
vPassword.value
|
||||
);
|
||||
|
||||
res.status(201).json({ data: result });
|
||||
} catch (err) {
|
||||
log.error('Error adding CardDAV account:', err);
|
||||
res.status(500).json({ error: 'Interner Fehler', code: 500 });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -212,6 +212,11 @@ function parseBirthday(value) {
|
||||
* @returns {Promise<Object>} { ok: true, addressbooks: [...] }
|
||||
*/
|
||||
async function testConnection(cardavUrl, username, password) {
|
||||
// Use mock if set (for testing)
|
||||
if (_testConnectionMock) {
|
||||
return _testConnectionMock(cardavUrl, username, password);
|
||||
}
|
||||
|
||||
try {
|
||||
const { createDAVClient } = await import('tsdav');
|
||||
const client = await createDAVClient({
|
||||
@@ -288,7 +293,16 @@ async function addAccount(name, cardavUrl, username, password) {
|
||||
|
||||
log.info(`Added CardDAV account "${name}" with ${addressbooks.length} addressbooks.`);
|
||||
|
||||
return { accountId, addressbooks: addressbookData };
|
||||
const account = {
|
||||
id: accountId,
|
||||
name,
|
||||
cardavUrl,
|
||||
username,
|
||||
createdAt: new Date().toISOString(),
|
||||
lastSync: null
|
||||
};
|
||||
|
||||
return { account, addressbooks: addressbookData };
|
||||
} catch (err) {
|
||||
log.error('Failed to add account:', err.message);
|
||||
throw err;
|
||||
@@ -869,4 +883,19 @@ export {
|
||||
|
||||
// Helpers (exported for testing)
|
||||
parseVCard,
|
||||
_mockTestConnection,
|
||||
};
|
||||
|
||||
// --------------------------------------------------------
|
||||
// Test Mocking Support
|
||||
// --------------------------------------------------------
|
||||
|
||||
let _testConnectionMock = null;
|
||||
|
||||
/**
|
||||
* ONLY FOR TESTING: Mock testConnection for unit tests
|
||||
* @param {Function|null} mockFn - Mock function or null to reset
|
||||
*/
|
||||
function _mockTestConnection(mockFn) {
|
||||
_testConnectionMock = mockFn;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user