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

100% Statements 43/43
81.81% Branches 27/33
100% Functions 14/14
100% Lines 41/41

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                    102x 18x         102x 14x 14x 14x 8x   14x 6x   14x 6x   14x 2x   14x 14x 12x   14x       102x 14x 4x 4x 4x 2x   4x 2x   4x 1x   4x 1x   4x 4x 3x   4x     102x           1x   1x           1x                     102x           10x           10x                                 10x        
import type { TRPCMutationInput } from "~app/trpc";
import type { Debt } from "~app/trpc-types";
import type { DebtId, PeerId, ReceiptId } from "~db/ids";
 
import { update as updateDebts } from "../cache/debts";
import { update as updateReceipts } from "../cache/receipts";
import type { ControllerContext, SnapshotFn, UpdateFn } from "../types";
 
type DebtUpdateObject = TRPCMutationInput<"debts.update">["update"];
 
const isUpdateSyncable = (update: DebtUpdateObject) =>
	update.amount !== undefined ||
	update.timestamp !== undefined ||
	update.currencyCode !== undefined;
 
export const applyUpdate =
	(update: DebtUpdateObject): UpdateFn<Debt> =>
	(debt) => {
		const nextDebt = { ...debt };
		if (update.amount !== undefined) {
			nextDebt.amount = update.amount;
		}
		if (update.timestamp !== undefined) {
			nextDebt.timestamp = update.timestamp;
		}
		if (update.currencyCode !== undefined) {
			nextDebt.currencyCode = update.currencyCode;
		}
		if (update.note !== undefined) {
			nextDebt.note = update.note;
		}
		const updateSyncable = isUpdateSyncable(update);
		if (updateSyncable) {
			nextDebt.updatedAt = Temporal.Now.zonedDateTimeISO();
		}
		return nextDebt;
	};
 
export const getRevert =
	(update: DebtUpdateObject): SnapshotFn<Debt> =>
	(snapshot) =>
	(debt) => {
		const revertDebt = { ...debt };
		if (update.amount !== undefined) {
			revertDebt.amount = snapshot.amount;
		}
		if (update.timestamp !== undefined) {
			revertDebt.timestamp = snapshot.timestamp;
		}
		if (update.currencyCode !== undefined) {
			revertDebt.currencyCode = snapshot.currencyCode;
		}
		if (update.note !== undefined) {
			revertDebt.note = snapshot.note;
		}
		const wasUpdateSyncable = isUpdateSyncable(update);
		if (wasUpdateSyncable) {
			revertDebt.updatedAt = snapshot.updatedAt;
		}
		return revertDebt;
	};
 
export const updateReceiptWithOutcomingDebtId = (
	controllerContext: ControllerContext,
	receiptId: ReceiptId,
	peerId: PeerId,
	debtId: DebtId,
) => {
	updateReceipts(controllerContext, {
		get: (controller) => {
			controller.update(receiptId, (receipt) => ({
				...receipt,
				debts: {
					direction: "outcoming",
					debts:
						receipt.debts.direction === "outcoming"
							? receipt.debts.debts.some((debt) => debt.id === debtId)
								? receipt.debts.debts
								: [...receipt.debts.debts, { id: debtId, peerId }]
							: [{ id: debtId, peerId }],
				},
			}));
		},
		getPaged: undefined,
	});
};
 
export const updateUpdatedAt = (
	controllerContext: ControllerContext,
	debtId: DebtId,
	updatedAt: Temporal.ZonedDateTime | undefined,
	reverseUpdated: boolean | undefined,
) => {
	updateDebts(controllerContext, {
		getAll: undefined,
		getAllPeer: undefined,
		getPeersPaged: undefined,
		getByPeerPaged: undefined,
		get: (controller) => {
			controller.update(debtId, (debt) => ({
				...debt,
				updatedAt: updatedAt || debt.updatedAt,
				their: reverseUpdated
					? {
							amount: debt.amount,
							currencyCode: debt.currencyCode,
							timestamp: debt.timestamp,
							updatedAt: updatedAt || debt.updatedAt,
						}
					: debt.their,
			}));
		},
		getIntentions: (controller) => {
			// It seems like it doesn't work
			// because whenever we update lockedTimestmap it is more fresh than counteryparty's
			// hence we never get intentions actually updated here
			controller.invalidate();
		},
	});
};