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 | 6x 5x 5x 5x 1x 4x 1x 3x 3x 1x 1x 1x 1x 1x 1x 3x | import { TRPCError } from "@trpc/server";
import type { Updateable } from "kysely";
import { z } from "zod";
import { receiptNameSchema } from "~app/utils/validation";
import type { DB } from "~db/types.gen";
import { temporalSchemas } from "~utils/temporal";
import { authProcedure } from "~web/handlers/trpc";
import { currencyCodeSchema, receiptIdSchema } from "~web/handlers/validation";
type ReceiptUpdateObject = Updateable<DB["receipts"]>;
export const procedure = authProcedure
.meta({
title: "Update receipt",
description:
"Updates the name, issued date, or currency of a receipt owned by the current user.",
})
.input(
z.strictObject({
id: receiptIdSchema,
update: z.discriminatedUnion("type", [
z.strictObject({
type: z.literal("name"),
name: receiptNameSchema,
}),
z.strictObject({
type: z.literal("issued"),
issued: temporalSchemas.plainDate,
}),
z.strictObject({
type: z.literal("currencyCode"),
currencyCode: currencyCodeSchema,
}),
]),
}),
)
.mutation(async ({ input, ctx }) => {
const { database } = ctx;
const receipt = await database
.selectFrom("receipts")
.select(["ownerUserId"])
.where("id", "=", input.id)
.limit(1)
.executeTakeFirst();
if (!receipt) {
throw new TRPCError({
code: "NOT_FOUND",
message: `No receipt found by id "${input.id}".`,
});
}
if (receipt.ownerUserId !== ctx.auth.userId) {
throw new TRPCError({
code: "FORBIDDEN",
message: `Receipt "${input.id}" is not owned by "${ctx.auth.email}".`,
});
}
const setObject: ReceiptUpdateObject = {};
switch (input.update.type) {
case "currencyCode":
setObject.currencyCode = input.update.currencyCode;
break;
case "issued":
setObject.issued = input.update.issued;
break;
case "name":
setObject.name = input.update.name;
break;
}
await database
.updateTable("receipts")
.set(setObject)
.where("id", "=", input.id)
.executeTakeFirst();
});
|