 6a211dd5b3
			
		
	
	
		6a211dd5b3
		
			
		
	
	
	
	
		
			
			* Concluded merge * Applied stash to newly merged * Always disconnect + remove redundant success message * Added named dialog to replace new=1 * Merged with main p2 * Set eventTypeId to @unique * WIP * Undo vscode changes * Availability dropdown works * Remove console.log + set schedule to null as it is unneeded * Added schedule to availability endpoint * Reduce one refresh; hotfix state inconsistency with forced refresh for now * Add missing translations * Fixed some type errors I missed * Ditch outdated remnant from before packages/prisma * Remove Availability section for teams * Bringing back the Availability section temporarily to teams to allow configuration * Migrated getting-started to new availability system + updated translations + updated seed * Fixed type error coming from main * Titlecase 'default' by providing translation * Fixed broken 'radio' buttons. * schedule deleted translation added * Added empty state for when no schedules are configured * Added correct created message + hotfix reload hard on delete to refresh state * Removed index renames * Type fixes * Update NewScheduleButton.tsx Co-authored-by: zomars <zomars@me.com> Co-authored-by: Bailey Pumfleet <pumfleet@hey.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
		
			
				
	
	
		
			116 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			116 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { GetServerSidePropsContext } from "next";
 | |
| import { Session } from "next-auth";
 | |
| import { serverSideTranslations } from "next-i18next/serverSideTranslations";
 | |
| 
 | |
| import { getSession } from "@lib/auth";
 | |
| import { getLocaleFromHeaders } from "@lib/core/i18n/i18n.utils";
 | |
| import prisma from "@lib/prisma";
 | |
| import { defaultAvatarSrc } from "@lib/profile";
 | |
| 
 | |
| import * as trpc from "@trpc/server";
 | |
| import { Maybe } from "@trpc/server";
 | |
| import * as trpcNext from "@trpc/server/adapters/next";
 | |
| 
 | |
| type CreateContextOptions = trpcNext.CreateNextContextOptions | GetServerSidePropsContext;
 | |
| 
 | |
| async function getUserFromSession({
 | |
|   session,
 | |
|   req,
 | |
| }: {
 | |
|   session: Maybe<Session>;
 | |
|   req: CreateContextOptions["req"];
 | |
| }) {
 | |
|   if (!session?.user?.id) {
 | |
|     return null;
 | |
|   }
 | |
|   const user = await prisma.user.findUnique({
 | |
|     where: {
 | |
|       id: session.user.id,
 | |
|     },
 | |
|     select: {
 | |
|       id: true,
 | |
|       username: true,
 | |
|       name: true,
 | |
|       email: true,
 | |
|       bio: true,
 | |
|       timeZone: true,
 | |
|       weekStart: true,
 | |
|       startTime: true,
 | |
|       endTime: true,
 | |
|       defaultScheduleId: true,
 | |
|       bufferTime: true,
 | |
|       theme: true,
 | |
|       createdDate: true,
 | |
|       hideBranding: true,
 | |
|       avatar: true,
 | |
|       twoFactorEnabled: true,
 | |
|       identityProvider: true,
 | |
|       brandColor: true,
 | |
|       darkBrandColor: true,
 | |
|       plan: true,
 | |
|       away: true,
 | |
|       credentials: {
 | |
|         select: {
 | |
|           id: true,
 | |
|           type: true,
 | |
|           key: true,
 | |
|         },
 | |
|         orderBy: {
 | |
|           id: "asc",
 | |
|         },
 | |
|       },
 | |
|       selectedCalendars: {
 | |
|         select: {
 | |
|           externalId: true,
 | |
|           integration: true,
 | |
|         },
 | |
|       },
 | |
|       completedOnboarding: true,
 | |
|       destinationCalendar: true,
 | |
|       locale: true,
 | |
|       timeFormat: true,
 | |
|       trialEndsAt: true,
 | |
|     },
 | |
|   });
 | |
| 
 | |
|   // some hacks to make sure `username` and `email` are never inferred as `null`
 | |
|   if (!user) {
 | |
|     return null;
 | |
|   }
 | |
|   const { email, username } = user;
 | |
|   if (!email) {
 | |
|     return null;
 | |
|   }
 | |
|   const avatar = user.avatar || defaultAvatarSrc({ email });
 | |
| 
 | |
|   const locale = user.locale || getLocaleFromHeaders(req);
 | |
|   return {
 | |
|     ...user,
 | |
|     avatar,
 | |
|     email,
 | |
|     username,
 | |
|     locale,
 | |
|   };
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Creates context for an incoming request
 | |
|  * @link https://trpc.io/docs/context
 | |
|  */
 | |
| export const createContext = async ({ req }: CreateContextOptions) => {
 | |
|   // for API-response caching see https://trpc.io/docs/caching
 | |
|   const session = await getSession({ req });
 | |
| 
 | |
|   const user = await getUserFromSession({ session, req });
 | |
|   const locale = user?.locale ?? getLocaleFromHeaders(req);
 | |
|   const i18n = await serverSideTranslations(locale, ["common"]);
 | |
|   return {
 | |
|     i18n,
 | |
|     prisma,
 | |
|     session,
 | |
|     user,
 | |
|     locale,
 | |
|   };
 | |
| };
 | |
| 
 | |
| export type Context = trpc.inferAsyncReturnType<typeof createContext>;
 |