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

81.81% Statements 9/11
50% Branches 1/2
87.5% Functions 7/8
80% Lines 8/10

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                    78x       2x 2x 2x   2x                               1x         1x 1x            
import type { ReceiptId } from "~db/ids";
 
import { updateRevert as updateRevertReceipts } from "../cache/receipts";
import type { UseContextedMutationOptions } from "../context";
 
import { getConsumersItems } from "./utils";
 
export const options: UseContextedMutationOptions<
	"receiptItemConsumers.remove",
	{ receiptId: ReceiptId }
> = {
	mutationKey: "receiptItemConsumers.remove",
	onMutate:
		(controllerContext, { receiptId }) =>
		(variables) => {
			Eif (variables.itemId === receiptId) {
				return updateRevertReceipts(controllerContext, {
					get: (controller) =>
						controller.removePayer(receiptId, variables.peerId),
					getPaged: undefined,
				});
			}
			return updateRevertReceipts(controllerContext, {
				get: (controller) =>
					controller.removeItemConsumer(
						receiptId,
						variables.itemId,
						variables.peerId,
					),
				getPaged: undefined,
			});
		},
	errorToastOptions:
		({ t }, contexts) =>
		(errors, variablesSet) => ({
			text: t("toasts.removeConsumer.error", {
				ns: "receipts",
				items: getConsumersItems(
					t,
					variablesSet.map(({ itemId }) => itemId),
					contexts.map((context) => context.receiptId),
				),
				errors,
			}),
		}),
};