
* Type fixes * Type fixes * Attemp to prevent unknown error in prod * Type fixes * Type fixes for onboarding * Extracts ConnectIntegration * Extracts IntegrationListItem * Extracts CalendarsList * Uses CalendarList on onboarding * Removes deprecated Alert * Extracts DisconnectIntegration * Extracts CalendarSwitch * Extracts ConnectedCalendarsList * Extracted connectedCalendar logic for reuse * Extracted SubHeadingTitleWithConnections * Type fixes * Fetched connected calendars in onboarding * Refreshes data on when adding/removing calendars on onboarding * Removed testing code * Type fixes * Feedback * Moved integration helpers * I was sleepy Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import _ from "lodash";
|
|
|
|
import { getErrorFromUnknown } from "@lib/errors";
|
|
|
|
import getCalendarCredentials from "./getCalendarCredentials";
|
|
|
|
export default async function getConnectedCalendars(
|
|
calendarCredentials: ReturnType<typeof getCalendarCredentials>,
|
|
selectedCalendars: { externalId: string }[]
|
|
) {
|
|
const connectedCalendars = await Promise.all(
|
|
calendarCredentials.map(async (item) => {
|
|
const { adapter, integration, credential } = item;
|
|
|
|
const credentialId = credential.id;
|
|
try {
|
|
const cals = await adapter.listCalendars();
|
|
const calendars = _(cals)
|
|
.map((cal) => ({
|
|
...cal,
|
|
primary: cal.primary || null,
|
|
isSelected: selectedCalendars.some((selected) => selected.externalId === cal.externalId),
|
|
}))
|
|
.sortBy(["primary"])
|
|
.value();
|
|
const primary = calendars.find((item) => item.primary) ?? calendars[0];
|
|
if (!primary) {
|
|
throw new Error("No primary calendar found");
|
|
}
|
|
return {
|
|
integration,
|
|
credentialId,
|
|
primary,
|
|
calendars,
|
|
};
|
|
} catch (_error) {
|
|
const error = getErrorFromUnknown(_error);
|
|
return {
|
|
integration,
|
|
credentialId,
|
|
error: {
|
|
message: error.message,
|
|
},
|
|
};
|
|
}
|
|
})
|
|
);
|
|
|
|
return connectedCalendars;
|
|
}
|