| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | import { GetServerSidePropsContext } from "next"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-09 22:56:05 +00:00
										 |  |  | import { PaymentData } from "@calcom/stripe/server"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:36:13 +00:00
										 |  |  | import { asStringOrThrow } from "@lib/asStringOrNull"; | 
					
						
							|  |  |  | import prisma from "@lib/prisma"; | 
					
						
							|  |  |  | import { inferSSRProps } from "@lib/types/inferSSRProps"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export type PaymentPageProps = inferSSRProps<typeof getServerSideProps>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const getServerSideProps = async (context: GetServerSidePropsContext) => { | 
					
						
							|  |  |  |   const rawPayment = await prisma.payment.findFirst({ | 
					
						
							|  |  |  |     where: { | 
					
						
							|  |  |  |       uid: asStringOrThrow(context.query.uid), | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     select: { | 
					
						
							|  |  |  |       data: true, | 
					
						
							|  |  |  |       success: true, | 
					
						
							|  |  |  |       uid: true, | 
					
						
							|  |  |  |       refunded: true, | 
					
						
							|  |  |  |       bookingId: true, | 
					
						
							|  |  |  |       booking: { | 
					
						
							|  |  |  |         select: { | 
					
						
							|  |  |  |           description: true, | 
					
						
							|  |  |  |           title: true, | 
					
						
							|  |  |  |           startTime: true, | 
					
						
							|  |  |  |           attendees: { | 
					
						
							|  |  |  |             select: { | 
					
						
							|  |  |  |               email: true, | 
					
						
							|  |  |  |               name: true, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           eventTypeId: true, | 
					
						
							|  |  |  |           location: true, | 
					
						
							|  |  |  |           eventType: { | 
					
						
							|  |  |  |             select: { | 
					
						
							|  |  |  |               id: true, | 
					
						
							|  |  |  |               title: true, | 
					
						
							|  |  |  |               description: true, | 
					
						
							|  |  |  |               length: true, | 
					
						
							|  |  |  |               eventName: true, | 
					
						
							|  |  |  |               requiresConfirmation: true, | 
					
						
							|  |  |  |               userId: true, | 
					
						
							|  |  |  |               users: { | 
					
						
							|  |  |  |                 select: { | 
					
						
							|  |  |  |                   name: true, | 
					
						
							|  |  |  |                   username: true, | 
					
						
							|  |  |  |                   hideBranding: true, | 
					
						
							|  |  |  |                   plan: true, | 
					
						
							|  |  |  |                   theme: true, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |               team: { | 
					
						
							|  |  |  |                 select: { | 
					
						
							|  |  |  |                   name: true, | 
					
						
							|  |  |  |                   hideBranding: true, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |               price: true, | 
					
						
							|  |  |  |               currency: true, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-24 16:45:35 +00:00
										 |  |  |   if (!rawPayment) throw Error("Payment not found"); | 
					
						
							| 
									
										
										
										
											2021-09-22 18:36:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const { data, booking: _booking, ...restPayment } = rawPayment; | 
					
						
							|  |  |  |   const payment = { | 
					
						
							|  |  |  |     ...restPayment, | 
					
						
							|  |  |  |     data: data as unknown as PaymentData, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-24 16:45:35 +00:00
										 |  |  |   if (!_booking) throw Error("Booking not found"); | 
					
						
							| 
									
										
										
										
											2021-09-22 18:36:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const { startTime, eventType, ...restBooking } = _booking; | 
					
						
							|  |  |  |   const booking = { | 
					
						
							|  |  |  |     ...restBooking, | 
					
						
							|  |  |  |     startTime: startTime.toString(), | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-24 16:45:35 +00:00
										 |  |  |   if (!eventType) throw Error("Event not found"); | 
					
						
							| 
									
										
										
										
											2021-09-22 18:36:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const [user] = eventType.users; | 
					
						
							|  |  |  |   if (!user) return { notFound: true }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const profile = { | 
					
						
							|  |  |  |     name: eventType.team?.name || user?.name || null, | 
					
						
							|  |  |  |     theme: (!eventType.team?.name && user?.theme) || null, | 
					
						
							|  |  |  |     hideBranding: eventType.team?.hideBranding || user?.hideBranding || null, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     props: { | 
					
						
							|  |  |  |       user, | 
					
						
							|  |  |  |       eventType, | 
					
						
							|  |  |  |       booking, | 
					
						
							|  |  |  |       payment, | 
					
						
							|  |  |  |       profile, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; |