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

100% Statements 29/29
100% Branches 6/6
100% Functions 18/18
100% Lines 28/28

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                      78x     3x 3x       3x         78x     3x 1x 1x       1x         78x     2x 2x       2x         78x     2x 1x 1x       1x             78x       5x 5x 2x   2x                 3x   3x                       2x         2x 2x            
import type { TRPCMutationInput } from "~app/trpc";
import type { ReceiptItemConsumer, ReceiptPayer } 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 { getConsumersItems } from "./utils";
 
const applyUpdate =
	(
		update: TRPCMutationInput<"receiptItemConsumers.update">["update"],
	): UpdateFn<ReceiptItemConsumer> =>
	(consumer) => {
		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 { ...consumer, part: update.part };
		}
	};
 
const getRevert =
	(
		update: TRPCMutationInput<"receiptItemConsumers.update">["update"],
	): SnapshotFn<ReceiptItemConsumer> =>
	(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 };
		}
	};
 
const applyUpdatePayer =
	(
		update: TRPCMutationInput<"receiptItemConsumers.update">["update"],
	): UpdateFn<ReceiptPayer> =>
	(part) => {
		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 { ...part, part: update.part };
		}
	};
 
const getRevertPayer =
	(
		update: TRPCMutationInput<"receiptItemConsumers.update">["update"],
	): SnapshotFn<ReceiptPayer> =>
	(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<
	"receiptItemConsumers.update",
	{ receiptId: ReceiptId }
> = {
	mutationKey: "receiptItemConsumers.update",
	onMutate:
		(controllerContext, { receiptId }) =>
		(variables) => {
			if (variables.itemId === receiptId) {
				return updateRevertReceipts(controllerContext, {
					get: (controller) =>
						controller.updatePayer(
							receiptId,
							variables.peerId,
							applyUpdatePayer(variables.update),
							getRevertPayer(variables.update),
						),
					getPaged: undefined,
				});
			}
			return updateRevertReceipts(controllerContext, {
				get: (controller) =>
					controller.updateItemConsumer(
						receiptId,
						variables.itemId,
						variables.peerId,
						applyUpdate(variables.update),
						getRevert(variables.update),
					),
				getPaged: undefined,
			});
		},
	errorToastOptions:
		({ t }, contexts) =>
		(errors, variablesSet) => ({
			text: t("toasts.updateConsumer.error", {
				ns: "receipts",
				items: getConsumersItems(
					t,
					variablesSet.map(({ itemId }) => itemId),
					contexts.map((context) => context.receiptId),
				),
				errors,
			}),
		}),
};