diff --git a/apps/web/components/DestinationCalendarSelector.tsx b/apps/web/components/DestinationCalendarSelector.tsx index fb515ea99b..7f6933bd28 100644 --- a/apps/web/components/DestinationCalendarSelector.tsx +++ b/apps/web/components/DestinationCalendarSelector.tsx @@ -25,20 +25,18 @@ const DestinationCalendarSelector = ({ const [selectedOption, setSelectedOption] = useState<{ value: string; label: string } | null>(null); useEffect(() => { - if (!selectedOption) { - const selected = query.data?.connectedCalendars - .map((connected) => connected.calendars ?? []) - .flat() - .find((cal) => cal.externalId === value); + const selected = query.data?.connectedCalendars + .map((connected) => connected.calendars ?? []) + .flat() + .find((cal) => cal.externalId === value); - if (selected) { - setSelectedOption({ - value: `${selected.integration}:${selected.externalId}`, - label: selected.name || "", - }); - } + if (selected) { + setSelectedOption({ + value: `${selected.integration}:${selected.externalId}`, + label: selected.name || "", + }); } - }, [query.data?.connectedCalendars, selectedOption, value]); + }, [query.data?.connectedCalendars, value]); if (!query.data?.connectedCalendars.length) { return null; diff --git a/apps/web/server/routers/viewer.tsx b/apps/web/server/routers/viewer.tsx index 4ed8e024f4..58b97ff114 100644 --- a/apps/web/server/routers/viewer.tsx +++ b/apps/web/server/routers/viewer.tsx @@ -429,6 +429,50 @@ const loggedInViewerRouter = createProtectedRouter() // get all the connected integrations' calendars (from third party) const connectedCalendars = await getConnectedCalendars(calendarCredentials, user.selectedCalendars); + if (connectedCalendars.length === 0) { + /* As there are no connected calendars, delete the destination calendar if it exists */ + if (user.destinationCalendar) { + await ctx.prisma.destinationCalendar.delete({ + where: { userId: user.id }, + }); + user.destinationCalendar = null; + } + } else if (!user.destinationCalendar) { + /* + There are connected calendars, but no destination calendar + So create a default destination calendar with the first primary connected calendar + */ + const { integration = "", externalId = "" } = connectedCalendars[0].primary ?? {}; + user.destinationCalendar = await ctx.prisma.destinationCalendar.create({ + data: { + userId: user.id, + integration, + externalId, + }, + }); + } else { + /* There are connected calendars and a destination calendar */ + + // Check if destinationCalendar exists in connectedCalendars + const allCals = connectedCalendars.map((cal) => cal.calendars ?? []).flat(); + const destinationCal = allCals.find( + (cal) => + cal.externalId === user.destinationCalendar?.externalId && + cal.integration === user.destinationCalendar?.integration + ); + if (!destinationCal) { + // If destinationCalendar is out of date, update it with the first primary connected calendar + const { integration = "", externalId = "" } = connectedCalendars[0].primary ?? {}; + user.destinationCalendar = await ctx.prisma.destinationCalendar.update({ + where: { userId: user.id }, + data: { + integration, + externalId, + }, + }); + } + } + return { connectedCalendars, destinationCalendar: user.destinationCalendar,