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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 6x 7x 2x 1x 1x 5x 5x 5x 1x 4x 1x 3x 3x 1x 1x 2x 2x 3x | import { TRPCError } from "@trpc/server";
import type { Updateable } from "kysely";
import { z } from "zod";
import { peerNameSchema } from "~app/utils/validation";
import type { DB } from "~db/types.gen";
import { authProcedure } from "~web/handlers/trpc";
import { peerIdSchema } from "~web/handlers/validation";
export const procedure = authProcedure
.meta({
title: "Update peer",
description:
"Updates the name or public name of a peer owned by the current user.",
})
.input(
z.strictObject({
id: peerIdSchema,
update: z.discriminatedUnion("type", [
z.strictObject({
type: z.literal("name"),
name: peerNameSchema,
}),
z.strictObject({
type: z.literal("publicName"),
publicName: peerNameSchema.optional(),
}),
]),
}),
)
.mutation(async ({ input, ctx }) => {
if (input.id === ctx.auth.userId) {
switch (input.update.type) {
case "name":
throw new TRPCError({
code: "BAD_REQUEST",
message:
'Please use "user.changeName" handler to update your own name.',
});
default:
throw new TRPCError({
code: "BAD_REQUEST",
message:
'Updating self peer property expect but "name" is not allowed.',
});
}
}
const { database } = ctx;
const peer = await database
.selectFrom("peers")
.select("ownerUserId")
.where("id", "=", input.id)
.limit(1)
.executeTakeFirst();
if (!peer) {
throw new TRPCError({
code: "NOT_FOUND",
message: `No peer found by id "${input.id}".`,
});
}
if (peer.ownerUserId !== ctx.auth.userId) {
throw new TRPCError({
code: "FORBIDDEN",
message: `Peer "${input.id}" is not owned by "${ctx.auth.email}".`,
});
}
let setObject: Updateable<DB["peers"]> = {};
switch (input.update.type) {
case "name":
setObject = { name: input.update.name };
break;
case "publicName":
setObject = { publicName: input.update.publicName || null };
break;
}
await database
.updateTable("peers")
.set(setObject)
.where("id", "=", input.id)
.executeTakeFirst();
});
|