All files / apps/web/src/handlers/user-connection-intentions remove.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 3/3
100% Lines 8/8

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 47 48 49            6x                       2x 2x     2x           2x               2x 1x         1x            
import { TRPCError } from "@trpc/server";
import { z } from "zod";
 
import { authProcedure } from "~web/handlers/trpc";
import { userIdSchema } from "~web/handlers/validation";
 
export const procedure = authProcedure
	.meta({
		title: "Remove  user connection intention",
		description:
			"Cancels an outbound  user connection intention sent to a given targetUserId.",
	})
	.input(
		z.strictObject({
			targetUserId: userIdSchema,
		}),
	)
	.mutation(async ({ ctx, input }) => {
		const { database } = ctx;
		const intention = await database
			.selectFrom("peers")
			.leftJoin("peers as reciprocalPeers", (qb) =>
				qb
					.onRef("reciprocalPeers.ownerUserId", "=", "peers.connectedUserId")
					.onRef("reciprocalPeers.connectedUserId", "=", "peers.ownerUserId"),
			)
			.select(["peers.id"])
			.where((eb) =>
				eb.and({
					"peers.ownerUserId": ctx.auth.userId,
					"peers.connectedUserId": input.targetUserId,
				}),
			)
			.where("reciprocalPeers.id", "is", null)
			.limit(1)
			.executeTakeFirst();
		if (!intention) {
			throw new TRPCError({
				code: "NOT_FOUND",
				message: `Intention for  user id "${input.targetUserId}" not found.`,
			});
		}
		await database
			.updateTable("peers")
			.set({ connectedUserId: null })
			.where("id", "=", intention.id)
			.executeTakeFirst();
	});