feat(cardav): implement POST /accounts/:id/test endpoint

Add connection test route. Loads account from DB and delegates to
CardDAVSync.testConnection() to verify credentials and discover
addressbooks without saving.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Ulas Kalayci
2026-05-04 17:10:25 +02:00
parent 38fa84c3d4
commit dd5ac8812c
2 changed files with 64 additions and 0 deletions
+27
View File
@@ -6,6 +6,7 @@
import { createLogger } from '../logger.js';
import express from 'express';
import * as db from '../db.js';
import * as CardDAVSync from '../services/cardav-sync.js';
import { str, collectErrors, MAX_TITLE } from '../middleware/validate.js';
@@ -76,4 +77,30 @@ router.delete('/accounts/:id', async (req, res) => {
}
});
/**
* POST /api/v1/contacts/cardav/accounts/:id/test
* Connection testen (ohne Account zu speichern).
* Response: { data: { ok, addressbooks } }
*/
router.post('/accounts/:id/test', async (req, res) => {
try {
const id = parseInt(req.params.id, 10);
if (!id || id < 1) return res.status(400).json({ error: 'Invalid ID', code: 400 });
const account = db.get().prepare('SELECT * FROM carddav_accounts WHERE id = ?').get(id);
if (!account) return res.status(404).json({ error: 'Account nicht gefunden', code: 404 });
const result = await CardDAVSync.testConnection(
account.carddav_url,
account.username,
account.password
);
res.json({ data: result });
} catch (err) {
log.error('Error testing CardDAV connection:', err);
res.status(500).json({ error: 'Interner Fehler', code: 500 });
}
});
export default router;