All files / packages/mutations/src/receipt-item-payers update.ts

18.75% Statements 3/16
0% Branches 0/2
0% Functions 0/11
20% Lines 3/15

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                      78x                         78x                               78x                                                          
import type { TRPCMutationInput } from "~app/trpc";
import type { ReceiptItemPayer } from "~app/trpc-types";
import type { ReceiptId } from "~db/ids";
 
import { updateRevert as updateRevertReceipts } from "../cache/receipts";
import type { UseContextedMutationOptions } from "../context";
import type { SnapshotFn, UpdateFn } from "../types";
 
import { getPayersItems } from "./utils";
 
const applyUpdate =
	(
		update: TRPCMutationInput<"receiptItemPayers.update">["update"],
	): UpdateFn<ReceiptItemPayer> =>
	(payer) => {
		switch (update.type) {
			// We want this to blow up in case we add more cases
			// oxlint-disable-next-line typescript/no-unnecessary-condition
			case "part":
				return { ...payer, part: update.part };
		}
	};
 
const getRevert =
	(
		update: TRPCMutationInput<"receiptItemPayers.update">["update"],
	): SnapshotFn<ReceiptItemPayer> =>
	(snapshot) =>
	(item) => {
		switch (update.type) {
			// We want this to blow up in case we add more cases
			// oxlint-disable-next-line typescript/no-unnecessary-condition
			case "part":
				return { ...item, part: snapshot.part };
		}
	};
 
export const options: UseContextedMutationOptions<
	"receiptItemPayers.update",
	{ receiptId: ReceiptId }
> = {
	mutationKey: "receiptItemPayers.update",
	onMutate:
		(controllerContext, { receiptId }) =>
		(variables) =>
			updateRevertReceipts(controllerContext, {
				get: (controller) =>
					controller.updateItemPayer(
						receiptId,
						variables.itemId,
						variables.peerId,
						applyUpdate(variables.update),
						getRevert(variables.update),
					),
				getPaged: undefined,
			}),
	errorToastOptions:
		({ t }) =>
		(errors, variablesSet) => ({
			text: t("toasts.updateItemPayer.error", {
				ns: "receipts",
				items: getPayersItems(
					t,
					variablesSet.map(({ itemId }) => itemId),
				),
				errors,
			}),
		}),
};