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

10% Statements 1/10
0% Branches 0/2
0% Functions 0/7
11.11% Lines 1/9

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