Process event location in EventManager
This commit is contained in:
		
							parent
							
								
									d7d1b79d0e
								
							
						
					
					
						commit
						a97862d4b8
					
				
					 2 changed files with 60 additions and 54 deletions
				
			
		|  | @ -3,6 +3,9 @@ import { Credential } from "@prisma/client"; | ||||||
| import async from "async"; | import async from "async"; | ||||||
| import { createMeeting, updateMeeting } from "@lib/videoClient"; | import { createMeeting, updateMeeting } from "@lib/videoClient"; | ||||||
| import prisma from "@lib/prisma"; | import prisma from "@lib/prisma"; | ||||||
|  | import { LocationType } from "@lib/location"; | ||||||
|  | import { v5 as uuidv5 } from "uuid"; | ||||||
|  | import merge from "lodash.merge"; | ||||||
| 
 | 
 | ||||||
| export interface EventResult { | export interface EventResult { | ||||||
|   type: string; |   type: string; | ||||||
|  | @ -29,6 +32,10 @@ export interface PartialReference { | ||||||
|   uid: string; |   uid: string; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | interface GetLocationRequestFromIntegrationRequest { | ||||||
|  |   location: string; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export default class EventManager { | export default class EventManager { | ||||||
|   calendarCredentials: Array<Credential>; |   calendarCredentials: Array<Credential>; | ||||||
|   videoCredentials: Array<Credential>; |   videoCredentials: Array<Credential>; | ||||||
|  | @ -51,6 +58,7 @@ export default class EventManager { | ||||||
|    * @param event |    * @param event | ||||||
|    */ |    */ | ||||||
|   public async create(event: CalendarEvent): Promise<CreateUpdateResult> { |   public async create(event: CalendarEvent): Promise<CreateUpdateResult> { | ||||||
|  |     event = EventManager.processLocation(event); | ||||||
|     const isVideo = EventManager.isIntegration(event.location); |     const isVideo = EventManager.isIntegration(event.location); | ||||||
| 
 | 
 | ||||||
|     // First, create all calendar events. If this is a video event, don't send a mail right here.
 |     // First, create all calendar events. If this is a video event, don't send a mail right here.
 | ||||||
|  | @ -82,6 +90,8 @@ export default class EventManager { | ||||||
|    * @param rescheduleUid |    * @param rescheduleUid | ||||||
|    */ |    */ | ||||||
|   public async update(event: CalendarEvent, rescheduleUid: string): Promise<CreateUpdateResult> { |   public async update(event: CalendarEvent, rescheduleUid: string): Promise<CreateUpdateResult> { | ||||||
|  |     event = EventManager.processLocation(event); | ||||||
|  | 
 | ||||||
|     // Get details of existing booking.
 |     // Get details of existing booking.
 | ||||||
|     const booking = await prisma.booking.findFirst({ |     const booking = await prisma.booking.findFirst({ | ||||||
|       where: { |       where: { | ||||||
|  | @ -227,4 +237,52 @@ export default class EventManager { | ||||||
|   private static isIntegration(location: string): boolean { |   private static isIntegration(location: string): boolean { | ||||||
|     return location.includes("integrations:"); |     return location.includes("integrations:"); | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Helper function for processLocation: Returns the conferenceData object to be merged | ||||||
|  |    * with the CalendarEvent. | ||||||
|  |    * | ||||||
|  |    * @param locationObj | ||||||
|  |    * @private | ||||||
|  |    */ | ||||||
|  |   private static getLocationRequestFromIntegration(locationObj: GetLocationRequestFromIntegrationRequest) { | ||||||
|  |     const location = locationObj.location; | ||||||
|  | 
 | ||||||
|  |     if (location === LocationType.GoogleMeet.valueOf() || location === LocationType.Zoom.valueOf()) { | ||||||
|  |       const requestId = uuidv5(location, uuidv5.URL); | ||||||
|  | 
 | ||||||
|  |       return { | ||||||
|  |         conferenceData: { | ||||||
|  |           createRequest: { | ||||||
|  |             requestId: requestId, | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |         location, | ||||||
|  |       }; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Takes a CalendarEvent and adds a ConferenceData object to the event | ||||||
|  |    * if the event has an integration-related location. | ||||||
|  |    * | ||||||
|  |    * @param event | ||||||
|  |    * @private | ||||||
|  |    */ | ||||||
|  |   private static processLocation(event: CalendarEvent): CalendarEvent { | ||||||
|  |     // If location is set to an integration location
 | ||||||
|  |     // Build proper transforms for evt object
 | ||||||
|  |     // Extend evt object with those transformations
 | ||||||
|  |     if (event.location?.includes("integration")) { | ||||||
|  |       const maybeLocationRequestObject = EventManager.getLocationRequestFromIntegration({ | ||||||
|  |         location: event.location, | ||||||
|  |       }); | ||||||
|  | 
 | ||||||
|  |       event = merge(event, maybeLocationRequestObject); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return event; | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -6,8 +6,6 @@ import short from "short-uuid"; | ||||||
| import { getBusyVideoTimes } from "@lib/videoClient"; | import { getBusyVideoTimes } from "@lib/videoClient"; | ||||||
| import EventAttendeeMail from "../../../lib/emails/EventAttendeeMail"; | import EventAttendeeMail from "../../../lib/emails/EventAttendeeMail"; | ||||||
| import { getEventName } from "@lib/event"; | import { getEventName } from "@lib/event"; | ||||||
| import { LocationType } from "@lib/location"; |  | ||||||
| import merge from "lodash.merge"; |  | ||||||
| import dayjs from "dayjs"; | import dayjs from "dayjs"; | ||||||
| import logger from "../../../lib/logger"; | import logger from "../../../lib/logger"; | ||||||
| import EventManager, { CreateUpdateResult, EventResult } from "@lib/events/EventManager"; | import EventManager, { CreateUpdateResult, EventResult } from "@lib/events/EventManager"; | ||||||
|  | @ -86,38 +84,6 @@ function isOutOfBounds( | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| interface GetLocationRequestFromIntegrationRequest { |  | ||||||
|   location: string; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| const getLocationRequestFromIntegration = ({ location }: GetLocationRequestFromIntegrationRequest) => { |  | ||||||
|   if (location === LocationType.GoogleMeet.valueOf()) { |  | ||||||
|     const requestId = uuidv5(location, uuidv5.URL); |  | ||||||
| 
 |  | ||||||
|     return { |  | ||||||
|       conferenceData: { |  | ||||||
|         createRequest: { |  | ||||||
|           requestId: requestId, |  | ||||||
|         }, |  | ||||||
|       }, |  | ||||||
|       location, |  | ||||||
|     }; |  | ||||||
|   } else if (location === LocationType.Zoom.valueOf()) { |  | ||||||
|     const requestId = uuidv5(location, uuidv5.URL); |  | ||||||
| 
 |  | ||||||
|     return { |  | ||||||
|       conferenceData: { |  | ||||||
|         createRequest: { |  | ||||||
|           requestId: requestId, |  | ||||||
|         }, |  | ||||||
|       }, |  | ||||||
|       location, |  | ||||||
|     }; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   return null; |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| export async function handleLegacyConfirmationMail( | export async function handleLegacyConfirmationMail( | ||||||
|   results: Array<EventResult>, |   results: Array<EventResult>, | ||||||
|   selectedEventType: { requiresConfirmation: boolean }, |   selectedEventType: { requiresConfirmation: boolean }, | ||||||
|  | @ -235,9 +201,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) | ||||||
|       }, |       }, | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     const rawLocation = req.body.location; |     const evt: CalendarEvent = { | ||||||
| 
 |  | ||||||
|     let evt: CalendarEvent = { |  | ||||||
|       type: selectedEventType.title, |       type: selectedEventType.title, | ||||||
|       title: getEventName(req.body.name, selectedEventType.title, selectedEventType.eventName), |       title: getEventName(req.body.name, selectedEventType.title, selectedEventType.eventName), | ||||||
|       description: req.body.notes, |       description: req.body.notes, | ||||||
|  | @ -245,25 +209,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) | ||||||
|       endTime: req.body.end, |       endTime: req.body.end, | ||||||
|       organizer: { email: currentUser.email, name: currentUser.name, timeZone: currentUser.timeZone }, |       organizer: { email: currentUser.email, name: currentUser.name, timeZone: currentUser.timeZone }, | ||||||
|       attendees: [{ email: req.body.email, name: req.body.name, timeZone: req.body.timeZone }], |       attendees: [{ email: req.body.email, name: req.body.name, timeZone: req.body.timeZone }], | ||||||
|  |       location: req.body.location, // Will be processed by the EventManager later.
 | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     // If phone or inPerson use raw location
 |  | ||||||
|     // set evt.location to req.body.location
 |  | ||||||
|     if (!rawLocation?.includes("integration")) { |  | ||||||
|       evt.location = rawLocation; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     // If location is set to an integration location
 |  | ||||||
|     // Build proper transforms for evt object
 |  | ||||||
|     // Extend evt object with those transformations
 |  | ||||||
|     if (rawLocation?.includes("integration")) { |  | ||||||
|       const maybeLocationRequestObject = getLocationRequestFromIntegration({ |  | ||||||
|         location: rawLocation, |  | ||||||
|       }); |  | ||||||
| 
 |  | ||||||
|       evt = merge(evt, maybeLocationRequestObject); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     const eventType = await prisma.eventType.findFirst({ |     const eventType = await prisma.eventType.findFirst({ | ||||||
|       where: { |       where: { | ||||||
|         userId: currentUser.id, |         userId: currentUser.id, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue
	
	 nicolas
						nicolas