From a159a57e9cac4fd3dcdf7a46bc6893737e8670c0 Mon Sep 17 00:00:00 2001 From: Ulas Kalayci Date: Mon, 4 May 2026 07:53:39 +0200 Subject: [PATCH] feat(caldav): add caldav-sync service base structure with helpers Co-Authored-By: Claude Opus 4.7 --- server/services/caldav-sync.js | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 server/services/caldav-sync.js diff --git a/server/services/caldav-sync.js b/server/services/caldav-sync.js new file mode 100644 index 0000000..381d821 --- /dev/null +++ b/server/services/caldav-sync.js @@ -0,0 +1,62 @@ +/** + * Modul: Generic CalDAV Sync + * Zweck: Multi-Account CalDAV synchronization with calendar selection + * Abhängigkeiten: tsdav, server/db.js, server/services/ics-parser.js + */ + +import { createLogger } from '../logger.js'; +const log = createLogger('CalDAV'); + +import * as db from '../db.js'; + +// Reused functions from apple-calendar.js +import { + parseICS, + buildICS, + escapeICS, + unescapeICS, + formatICSDate, + tzLocalToUTC, + applyDuration +} from './ics-parser.js'; + +// -------------------------------------------------------- +// Helper Functions +// -------------------------------------------------------- + +function normalizeCalColor(c) { + if (!c) return null; + if (/^#[0-9a-fA-F]{8}$/.test(c)) return c.slice(0, 7); // strip alpha + if (/^#[0-9a-fA-F]{6}$/.test(c)) return c; + return null; +} + +function upsertExternalCalendar(source, externalId, name, color) { + const row = db.get().prepare(` + INSERT INTO external_calendars (source, external_id, name, color) + VALUES (?, ?, ?, ?) + ON CONFLICT(source, external_id) DO UPDATE SET + name = excluded.name, + color = excluded.color + RETURNING id + `).get(source, externalId, name, color); + return row.id; +} + +// -------------------------------------------------------- +// Credentials Helpers +// -------------------------------------------------------- + +function getAccountById(accountId) { + return db.get().prepare('SELECT * FROM caldav_accounts WHERE id = ?').get(accountId); +} + +function getAllAccounts() { + return db.get().prepare('SELECT * FROM caldav_accounts').all(); +} + +// -------------------------------------------------------- +// Export placeholder (will be filled in next tasks) +// -------------------------------------------------------- + +export { };