All files / apps/web/src/handlers/auth void-user.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 1/1
100% Lines 7/7

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            6x                       2x 2x           2x 1x         1x       1x        
import { TRPCError } from "@trpc/server";
import { z } from "zod";
 
import { voidUserTokenSchema } from "~app/utils/validation";
import { unauthProcedure } from "~web/handlers/trpc";
 
export const procedure = unauthProcedure
	.meta({
		title: "Void user",
		description:
			"Permanently deletes an unconfirmed user identified by its confirmation token.",
	})
	.input(
		z.strictObject({
			token: voidUserTokenSchema,
		}),
	)
	.mutation(async ({ input, ctx }) => {
		const { database } = ctx;
		const user = await database
			.selectFrom("users")
			.select(["id", "email"])
			.where("confirmationToken", "=", input.token)
			.limit(1)
			.executeTakeFirst();
		if (!user) {
			throw new TRPCError({
				code: "NOT_FOUND",
				message: `There is no user with confirmation token "${input.token}".`,
			});
		}
		await database
			.deleteFrom("users")
			.where("users.id", "=", user.id)
			.executeTakeFirst();
		return {
			email: user.email,
		};
	});