fix issue where user could not connect to caldav
Uses base caldav adapter to ensure connectivity rethrows all errors on CalDavCalendarAdapter
This commit is contained in:
parent
4f35dfa5f0
commit
49e7cbf803
2 changed files with 26 additions and 39 deletions
|
@ -103,6 +103,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
|||
};
|
||||
} catch (reason) {
|
||||
console.error(reason);
|
||||
throw reason;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,6 +154,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
|||
return null;
|
||||
} catch (reason) {
|
||||
console.error(reason);
|
||||
throw reason;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -186,6 +188,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
|||
return null;
|
||||
} catch (reason) {
|
||||
console.error(reason);
|
||||
throw reason;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -212,6 +215,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
|||
return events;
|
||||
} catch (reason) {
|
||||
console.error(reason);
|
||||
throw reason;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -235,6 +239,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
|||
}));
|
||||
} catch (reason) {
|
||||
console.error(reason);
|
||||
throw reason;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -295,6 +300,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
|
|||
return events;
|
||||
} catch (reason) {
|
||||
console.error(reason);
|
||||
throw reason;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ import { getSession } from "next-auth/client";
|
|||
import prisma from "../../../../lib/prisma";
|
||||
import { symmetricEncrypt } from "@lib/crypto";
|
||||
import logger from "@lib/logger";
|
||||
import { davRequest, getBasicAuthHeaders } from "tsdav";
|
||||
import { CalDavCalendar } from "@lib/integrations/CalDav/CalDavCalendarAdapter";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "POST") {
|
||||
|
@ -26,47 +26,28 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
},
|
||||
});
|
||||
|
||||
const header = getBasicAuthHeaders({
|
||||
username,
|
||||
password,
|
||||
});
|
||||
|
||||
try {
|
||||
const [response] = await davRequest({
|
||||
url: url,
|
||||
init: {
|
||||
method: "PROPFIND",
|
||||
namespace: "d",
|
||||
body: {
|
||||
propfind: {
|
||||
_attributes: {
|
||||
"xmlns:d": "DAV:",
|
||||
},
|
||||
prop: { "d:current-user-principal": {} },
|
||||
},
|
||||
},
|
||||
headers: header,
|
||||
},
|
||||
const dav = new CalDavCalendar({
|
||||
id: 0,
|
||||
type: "caldav_calendar",
|
||||
key: symmetricEncrypt(
|
||||
JSON.stringify({ username, password, url }),
|
||||
process.env.CALENDSO_ENCRYPTION_KEY
|
||||
),
|
||||
userId: session.user.id,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error("Could not add this caldav account", response?.statusText);
|
||||
logger.error(response.error);
|
||||
return res.status(200).json({ message: "Could not add this caldav account" });
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
await prisma.credential.create({
|
||||
data: {
|
||||
type: "caldav_calendar",
|
||||
key: symmetricEncrypt(
|
||||
JSON.stringify({ username, password, url }),
|
||||
process.env.CALENDSO_ENCRYPTION_KEY
|
||||
),
|
||||
userId: session.user.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
await dav.listCalendars();
|
||||
await prisma.credential.create({
|
||||
data: {
|
||||
type: "caldav_calendar",
|
||||
key: symmetricEncrypt(
|
||||
JSON.stringify({ username, password, url }),
|
||||
process.env.CALENDSO_ENCRYPTION_KEY
|
||||
),
|
||||
userId: session.user.id,
|
||||
},
|
||||
});
|
||||
} catch (reason) {
|
||||
logger.error("Could not add this caldav account", reason);
|
||||
return res.status(500).json({ message: "Could not add this caldav account" });
|
||||
|
|
Loading…
Reference in a new issue