All files / packages/mutations/src/debt-intentions accept.ts

80.64% Statements 25/31
33.33% Branches 2/6
77.77% Functions 21/27
83.33% Lines 20/24

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 112 113 114 115 116 117                      37x       14x 14x   14x           14x           14x     14x 14x 2x   2x             1x                 14x                                         10x 10x       10x   10x           10x         8x               4x               3x                
import type { DebtIntention } from "~app/trpc-types";
 
import {
	update as updateDebts,
	updateRevert as updateRevertDebts,
} from "../cache/debts";
import type { UseContextedMutationOptions } from "../context";
 
export const options: UseContextedMutationOptions<
	"debtIntentions.accept",
	{ intention: DebtIntention }
> = {
	mutationKey: "debtIntentions.accept",
	onMutate:
		(controllerContext, { intention }) =>
		() =>
			updateRevertDebts(controllerContext, {
				getAll: (controller) =>
					controller.update(
						intention.currencyCode,
						(sum) => sum - (intention.current?.amount ?? 0) + intention.amount,
						(snapshot) => () => snapshot,
					),
				getAllPeer: (controller) =>
					controller.update(
						intention.peerId,
						intention.currencyCode,
						(sum) => sum - (intention.current?.amount ?? 0) + intention.amount,
						(snapshot) => () => snapshot,
					),
				getPeersPaged: (controller) => controller.update(intention.peerId),
				getByPeerPaged: undefined,
				get: (controller) => {
					const updatedAt = Temporal.Now.zonedDateTimeISO();
					if (intention.current) {
						return controller.update(
							intention.id,
							(debt) => ({
								...debt,
								currencyCode: intention.currencyCode,
								amount: intention.amount,
								timestamp: intention.timestamp,
								updatedAt,
							}),
							(snapshot) => (debt) => ({
								...debt,
								currencyCode: snapshot.currencyCode,
								amount: snapshot.amount,
								timestamp: snapshot.timestamp,
								updatedAt: snapshot.updatedAt,
							}),
						);
					}
					return controller.add({
						id: intention.id,
						peerId: intention.peerId,
						currencyCode: intention.currencyCode,
						amount: intention.amount,
						timestamp: intention.timestamp,
						note: intention.note,
						updatedAt,
						their: {
							updatedAt: intention.updatedAt,
							timestamp: intention.timestamp,
							amount: intention.amount,
							currencyCode: intention.currencyCode,
						},
						receiptId: intention.receiptId,
					});
				},
				getIntentions: undefined,
			}),
	onSuccess:
		(controllerContext, { intention }) =>
		(data) =>
			updateDebts(controllerContext, {
				getAll: undefined,
				getAllPeer: undefined,
				getPeersPaged: undefined,
				getByPeerPaged: (controller) => controller.invalidate(intention.peerId),
				get: (controller) => {
					controller.update(intention.id, (debt) => ({
						...debt,
						updatedAt: data.updatedAt,
					}));
				},
				getIntentions: (controller) => {
					controller.remove(intention.id);
				},
			}),
	mutateToastOptions:
		({ t }) =>
		(variablesSet) => ({
			text: t("toasts.acceptIntention.mutate", {
				ns: "debts",
				debtsAmount: variablesSet.length,
			}),
		}),
	successToastOptions:
		({ t }) =>
		(resultSet) => ({
			text: t("toasts.acceptIntention.success", {
				ns: "debts",
				debtsAmount: resultSet.length,
			}),
		}),
	errorToastOptions:
		({ t }) =>
		(errors) => ({
			text: t("toasts.acceptIntention.error", {
				ns: "debts",
				debtsAmount: errors.length,
				errors,
			}),
		}),
};