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

75.67% Statements 28/37
100% Branches 12/12
70.37% Functions 19/27
83.33% Lines 25/30

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 118 119 120 121 122 123 124 125 126 127 128 129                                                102x       14x 14x 14x 14x 14x   14x     1x         1x         14x       1x           1x       14x     14x 10x         14x 6x         14x                   10x 10x 1x             10x                 13x               4x               4x                
import type { CurrencyCode } from "~app/utils/currency";
import type { PeerId, ReceiptId } from "~db/ids";
 
import { updateRevert as updateRevertDebts } from "../cache/debts";
import type { UseContextedMutationOptions } from "../context";
import { mergeUpdaterResults } from "../utils";
 
import {
	applyUpdate,
	getRevert,
	updateReceiptWithOutcomingDebtId,
	updateUpdatedAt,
} from "./utils";
 
export const options: UseContextedMutationOptions<
	"debts.update",
	{
		currDebt: {
			peerId: PeerId;
			amount: number;
			currencyCode: CurrencyCode;
			receiptId?: ReceiptId;
		};
	}
> = {
	mutationKey: "debts.update",
	onMutate:
		(controllerContext, { currDebt }) =>
		(updateObject) => {
			const { update } = updateObject;
			const newCurrencyCode = update.currencyCode ?? currDebt.currencyCode;
			const newAmount = update.amount ?? currDebt.amount;
			return updateRevertDebts(controllerContext, {
				getAll: (controller) =>
					mergeUpdaterResults(
						controller.update(
							currDebt.currencyCode,
							(sum) => sum - currDebt.amount,
							(updatedSum) => () => updatedSum + currDebt.amount,
						),
						controller.update(
							newCurrencyCode,
							(sum) => sum + newAmount,
							(updatedSum) => () => updatedSum - newAmount,
						),
					),
				getAllPeer: (controller) =>
					mergeUpdaterResults(
						controller.update(
							currDebt.peerId,
							currDebt.currencyCode,
							(sum) => sum - currDebt.amount,
							(updatedSum) => () => updatedSum + currDebt.amount,
						),
						controller.update(
							currDebt.peerId,
							newCurrencyCode,
							(sum) => sum + newAmount,
							(updatedSum) => () => updatedSum - newAmount,
						),
					),
				getPeersPaged: (controller) => controller.update(currDebt.peerId),
				getByPeerPaged: (controller) => {
					// Updating currency code or amount might change resolved list status
					if (update.currencyCode || update.amount) {
						controller.invalidate(currDebt.peerId, {
							filters: { showResolved: false },
						});
					}
					// Updating timestamp might change position in a list
					if (update.timestamp) {
						controller.invalidate(currDebt.peerId);
					}
					return undefined;
				},
				get: (controller) =>
					controller.update(
						updateObject.id,
						applyUpdate(update),
						getRevert(update),
					),
				getIntentions: undefined,
			});
		},
	onSuccess:
		(controllerContext, { currDebt }) =>
		(result, updateObject) => {
			if (currDebt.receiptId) {
				updateReceiptWithOutcomingDebtId(
					controllerContext,
					currDebt.receiptId,
					currDebt.peerId,
					updateObject.id,
				);
			}
			updateUpdatedAt(
				controllerContext,
				updateObject.id,
				result.updatedAt,
				result.reverseUpdated,
			);
		},
	mutateToastOptions:
		({ t }) =>
		(variablesSet) => ({
			text: t("toasts.updateDebt.mutate", {
				ns: "debts",
				debtsAmount: variablesSet.length,
			}),
		}),
	successToastOptions:
		({ t }) =>
		(resultSet) => ({
			text: t("toasts.updateDebt.success", {
				ns: "debts",
				debtsAmount: resultSet.length,
			}),
		}),
	errorToastOptions:
		({ t }) =>
		(errors) => ({
			text: t("toasts.updateDebt.error", {
				ns: "debts",
				debtsAmount: errors.length,
				errors,
			}),
		}),
};