Set a default for "create events on" (#2215)
* fix: 🐛 Set a default for create events on * fix: 🐛 Save default value to db * Revert fixes in frontend * fix: 🐛 Set a default for "create events on" from backend" * fix: 🐛 Update frontend when destinationCalendar is disconnectedpull/2333/head^2
parent
4a58da62d6
commit
0494fccb8e
|
@ -25,7 +25,6 @@ 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()
|
||||
|
@ -37,8 +36,7 @@ const DestinationCalendarSelector = ({
|
|||
label: selected.name || "",
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [query.data?.connectedCalendars, selectedOption, value]);
|
||||
}, [query.data?.connectedCalendars, value]);
|
||||
|
||||
if (!query.data?.connectedCalendars.length) {
|
||||
return null;
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue