All files / apps/web/src/handlers/user get.ts

100% Statements 5/5
100% Branches 4/4
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46        6x             9x   9x   9x                                     9x                      
import { TRPCError } from "@trpc/server";
 
import { authProcedure } from "~web/handlers/trpc";
 
export const procedure = authProcedure
	.meta({
		title: "Get  user",
		description:
			"Returns the current  user's profile and its self-peer's name.",
	})
	.query(async ({ ctx }) => {
		const { database } = ctx;
		const { confirmationToken, id, name, avatarUrl, email, role } =
			await database
				.selectFrom("users")
				.innerJoin("peers", (jb) => jb.onRef("peers.id", "=", "users.id"))
				.select([
					"users.id",
					"peers.name",
					"users.confirmationToken",
					"users.avatarUrl",
					"users.email",
					"users.role",
				])
				.where("users.id", "=", ctx.auth.userId)
				.executeTakeFirstOrThrow(
					() =>
						/* c8 ignore start */
						new TRPCError({
							code: "INTERNAL_SERVER_ERROR",
							message: `No result for "${ctx.auth.email}"  user found, self-peer may be non-existent.`,
						}),
					/* c8 ignore stop */
				);
		return {
			user: {
				id,
				email,
				verified: !confirmationToken,
				avatarUrl: avatarUrl || undefined,
				role: role ?? undefined,
			},
			peer: { name },
		};
	});