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
This commit is contained in:
		
							parent
							
								
									4a58da62d6
								
							
						
					
					
						commit
						0494fccb8e
					
				
					 2 changed files with 54 additions and 12 deletions
				
			
		|  | @ -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; | ||||
|  |  | |||
|  | @ -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 a new issue
	
	 Miguel Nieto A
						Miguel Nieto A