All files / packages/mutations/src/receipts update.ts

15.78% Statements 3/19
0% Branches 0/6
0% Functions 0/10
16.66% Lines 3/18

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                         78x                             78x                                            
import type { TRPCMutationInput } from "~app/trpc";
import type { Receipt } 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<"receipts.update">["update"]): UpdateFn<Receipt> =>
	(item) => {
		switch (update.type) {
			case "name":
				return { ...item, name: update.name };
			case "issued":
				return { ...item, issued: update.issued };
			case "currencyCode":
				return { ...item, currencyCode: update.currencyCode };
		}
	};
 
const getRevert =
	(
		update: TRPCMutationInput<"receipts.update">["update"],
	): SnapshotFn<Receipt> =>
	(snapshot) =>
	(receipt) => {
		switch (update.type) {
			case "name":
				return { ...receipt, name: snapshot.name };
			case "issued":
				return { ...receipt, issued: snapshot.issued };
			case "currencyCode":
				return { ...receipt, currencyCode: snapshot.currencyCode };
		}
	};
 
export const options: UseContextedMutationOptions<"receipts.update"> = {
	mutationKey: "receipts.update",
	onMutate: (controllerContext) => (updateObject) =>
		updateRevertReceipts(controllerContext, {
			get: (controller) =>
				controller.update(
					updateObject.id,
					applyUpdate(updateObject.update),
					getRevert(updateObject.update),
				),
			getPaged: undefined,
		}),
	errorToastOptions:
		({ t }) =>
		(errors) => ({
			text: t("toasts.updateReceipt.error", {
				ns: "receipts",
				receiptsCount: errors.length,
				errors,
			}),
		}),
};