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 disconnected
pull/2333/head^2
Miguel Nieto A 2022-03-31 12:26:26 -05:00 committed by GitHub
parent 4a58da62d6
commit 0494fccb8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 12 deletions

View File

@ -25,20 +25,18 @@ const DestinationCalendarSelector = ({
const [selectedOption, setSelectedOption] = useState<{ value: string; label: string } | null>(null); const [selectedOption, setSelectedOption] = useState<{ value: string; label: string } | null>(null);
useEffect(() => { useEffect(() => {
if (!selectedOption) { const selected = query.data?.connectedCalendars
const selected = query.data?.connectedCalendars .map((connected) => connected.calendars ?? [])
.map((connected) => connected.calendars ?? []) .flat()
.flat() .find((cal) => cal.externalId === value);
.find((cal) => cal.externalId === value);
if (selected) { if (selected) {
setSelectedOption({ setSelectedOption({
value: `${selected.integration}:${selected.externalId}`, value: `${selected.integration}:${selected.externalId}`,
label: selected.name || "", label: selected.name || "",
}); });
}
} }
}, [query.data?.connectedCalendars, selectedOption, value]); }, [query.data?.connectedCalendars, value]);
if (!query.data?.connectedCalendars.length) { if (!query.data?.connectedCalendars.length) {
return null; return null;

View File

@ -429,6 +429,50 @@ const loggedInViewerRouter = createProtectedRouter()
// get all the connected integrations' calendars (from third party) // get all the connected integrations' calendars (from third party)
const connectedCalendars = await getConnectedCalendars(calendarCredentials, user.selectedCalendars); 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 { return {
connectedCalendars, connectedCalendars,
destinationCalendar: user.destinationCalendar, destinationCalendar: user.destinationCalendar,