All files / packages/mutations/src/receipt-participants update.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 10/10
100% Lines 14/14

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                78x     4x 4x       4x         78x     4x 2x 2x       2x         78x   4x 4x   4x                   2x                
import type { TRPCMutationInput } from "~app/trpc";
import type { ReceiptParticipant } from "~app/trpc-types";
 
import { updateRevert as updateRevertReceipts } from "../cache/receipts";
import type { UseContextedMutationOptions } from "../context";
import type { SnapshotFn, UpdateFn } from "../types";
 
const applyUpdate =
	(
		update: TRPCMutationInput<"receiptParticipants.update">["update"],
	): UpdateFn<ReceiptParticipant> =>
	(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 "role":
				return { ...item, role: update.role };
		}
	};
 
const getRevert =
	(
		update: TRPCMutationInput<"receiptParticipants.update">["update"],
	): SnapshotFn<ReceiptParticipant> =>
	(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 "role":
				return { ...item, role: snapshot.role };
		}
	};
 
export const options: UseContextedMutationOptions<"receiptParticipants.update"> =
	{
		mutationKey: "receiptParticipants.update",
		onMutate: (controllerContext) => (variables) =>
			updateRevertReceipts(controllerContext, {
				get: (controller) =>
					controller.updateParticipant(
						variables.receiptId,
						variables.peerId,
						applyUpdate(variables.update),
						getRevert(variables.update),
					),
				getPaged: undefined,
			}),
		errorToastOptions:
			({ t }) =>
			(errors) => ({
				text: t("toasts.updateParticipant.error", {
					ns: "receipts",
					participantsCount: errors.length,
					errors,
				}),
			}),
	};