feat(caldav): implement account CRUD operations
Add createAccount, updateAccount, deleteAccount functions with validation, error handling, and logging. Implements Task 3 from the CalDAV multi-account spec. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -53,7 +53,108 @@ function getAllAccounts() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------
|
// --------------------------------------------------------
|
||||||
// Export placeholder (will be filled in next tasks)
|
// Account CRUD Operations
|
||||||
// --------------------------------------------------------
|
// --------------------------------------------------------
|
||||||
|
|
||||||
export { };
|
/**
|
||||||
|
* Create a new CalDAV account
|
||||||
|
* @param {string} label - User-friendly label
|
||||||
|
* @param {string} url - CalDAV server URL
|
||||||
|
* @param {string} username - Account username
|
||||||
|
* @param {string} password - Account password
|
||||||
|
* @returns {number} Account ID
|
||||||
|
*/
|
||||||
|
function createAccount(label, url, username, password) {
|
||||||
|
if (!label || !url || !username || !password) {
|
||||||
|
throw new Error('All fields required: label, url, username, password');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = db.get().prepare(`
|
||||||
|
INSERT INTO caldav_accounts (label, url, username, password)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
`).run(label, url, username, password);
|
||||||
|
|
||||||
|
log.info('CalDAV account created', { id: result.lastInsertRowid, label });
|
||||||
|
return result.lastInsertRowid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update an existing CalDAV account
|
||||||
|
* @param {number} accountId - Account ID
|
||||||
|
* @param {object} updates - Fields to update
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
function updateAccount(accountId, updates) {
|
||||||
|
const account = getAccountById(accountId);
|
||||||
|
if (!account) {
|
||||||
|
throw new Error(`CalDAV account not found: ${accountId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { label, url, username, password } = updates;
|
||||||
|
const fields = [];
|
||||||
|
const values = [];
|
||||||
|
|
||||||
|
if (label !== undefined) {
|
||||||
|
fields.push('label = ?');
|
||||||
|
values.push(label);
|
||||||
|
}
|
||||||
|
if (url !== undefined) {
|
||||||
|
fields.push('url = ?');
|
||||||
|
values.push(url);
|
||||||
|
}
|
||||||
|
if (username !== undefined) {
|
||||||
|
fields.push('username = ?');
|
||||||
|
values.push(username);
|
||||||
|
}
|
||||||
|
if (password !== undefined) {
|
||||||
|
fields.push('password = ?');
|
||||||
|
values.push(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fields.length === 0) {
|
||||||
|
throw new Error('No fields to update');
|
||||||
|
}
|
||||||
|
|
||||||
|
values.push(accountId);
|
||||||
|
|
||||||
|
db.get().prepare(`
|
||||||
|
UPDATE caldav_accounts
|
||||||
|
SET ${fields.join(', ')}
|
||||||
|
WHERE id = ?
|
||||||
|
`).run(...values);
|
||||||
|
|
||||||
|
log.info('CalDAV account updated', { id: accountId, fields: Object.keys(updates) });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a CalDAV account and all associated data
|
||||||
|
* @param {number} accountId - Account ID
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
function deleteAccount(accountId) {
|
||||||
|
const account = getAccountById(accountId);
|
||||||
|
if (!account) {
|
||||||
|
throw new Error(`CalDAV account not found: ${accountId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Foreign key constraints will cascade delete:
|
||||||
|
// - caldav_selected_calendars
|
||||||
|
// - external_calendars (via source)
|
||||||
|
// - calendar_events (via external_calendar_id)
|
||||||
|
|
||||||
|
db.get().prepare('DELETE FROM caldav_accounts WHERE id = ?').run(accountId);
|
||||||
|
|
||||||
|
log.info('CalDAV account deleted', { id: accountId, label: account.label });
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------
|
||||||
|
// Exports
|
||||||
|
// --------------------------------------------------------
|
||||||
|
|
||||||
|
export {
|
||||||
|
createAccount,
|
||||||
|
updateAccount,
|
||||||
|
deleteAccount,
|
||||||
|
getAccountById,
|
||||||
|
getAllAccounts
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user