2021-09-27 14:47:55 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
|
|
import * as trpc from "@trpc/server";
|
|
|
|
import { Maybe } from "@trpc/server";
|
|
|
|
import * as trpcNext from "@trpc/server/adapters/next";
|
2021-10-11 12:03:50 +00:00
|
|
|
import { NextApiRequest } from "next";
|
2021-09-27 14:47:55 +00:00
|
|
|
|
|
|
|
import { getSession, Session } from "@lib/auth";
|
2021-10-11 12:03:50 +00:00
|
|
|
import { getLocaleFromHeaders } from "@lib/core/i18n/i18n.utils";
|
2021-09-27 14:47:55 +00:00
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
import { defaultAvatarSrc } from "@lib/profile";
|
|
|
|
|
2021-10-11 12:03:50 +00:00
|
|
|
async function getUserFromSession({ session, req }: { session: Maybe<Session>; req: NextApiRequest }) {
|
2021-09-27 14:47:55 +00:00
|
|
|
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,
|
|
|
|
bufferTime: true,
|
|
|
|
theme: true,
|
|
|
|
createdDate: true,
|
|
|
|
hideBranding: true,
|
|
|
|
avatar: true,
|
2021-10-12 09:35:44 +00:00
|
|
|
credentials: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
type: true,
|
|
|
|
key: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
selectedCalendars: {
|
|
|
|
select: {
|
|
|
|
externalId: true,
|
|
|
|
integration: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
completedOnboarding: true,
|
2021-10-11 12:03:50 +00:00
|
|
|
locale: true,
|
2021-09-27 14:47:55 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// some hacks to make sure `username` and `email` are never inferred as `null`
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const { email, username } = user;
|
|
|
|
if (!username || !email) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const avatar = user.avatar || defaultAvatarSrc({ email });
|
2021-10-11 12:03:50 +00:00
|
|
|
|
|
|
|
const locale = user.locale ?? getLocaleFromHeaders(req);
|
2021-09-27 14:47:55 +00:00
|
|
|
return {
|
|
|
|
...user,
|
|
|
|
avatar,
|
|
|
|
email,
|
|
|
|
username,
|
2021-10-11 12:03:50 +00:00
|
|
|
locale,
|
2021-09-27 14:47:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates context for an incoming request
|
|
|
|
* @link https://trpc.io/docs/context
|
|
|
|
*/
|
|
|
|
export const createContext = async ({ req, res }: trpcNext.CreateNextContextOptions) => {
|
|
|
|
// for API-response caching see https://trpc.io/docs/caching
|
|
|
|
const session = await getSession({ req });
|
|
|
|
|
2021-10-11 12:03:50 +00:00
|
|
|
const user = await getUserFromSession({ session, req });
|
|
|
|
const locale = user?.locale ?? getLocaleFromHeaders(req);
|
2021-09-27 14:47:55 +00:00
|
|
|
return {
|
|
|
|
prisma,
|
|
|
|
session,
|
2021-10-11 12:03:50 +00:00
|
|
|
user,
|
|
|
|
locale,
|
2021-09-27 14:47:55 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Context = trpc.inferAsyncReturnType<typeof createContext>;
|