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

75.67% Statements 28/37
45.45% Branches 10/22
90% Functions 18/20
73.52% Lines 25/34

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 130 131 132 133 134 135 136 137 138                18x       2x 2x   2x     2x 2x               2x 2x         2x               3x 3x         3x               2x   3x   2x         2x                                                   1x 1x         1x                             4x       4x         2x       2x         2x     2x          
import type { PeerId, UserId } from "~db/ids";
 
import { update as updateReceipts } from "../cache/receipts";
import type { UseContextedMutationOptions } from "../context";
 
export const options: UseContextedMutationOptions<
	"receipts.add",
	{ selfUserId: UserId }
> = {
	mutationKey: "receipts.add",
	onSuccess:
		(controllerContext, { selfUserId }) =>
		(result, variables) => {
			updateReceipts(controllerContext, {
				getPaged: (controller) => {
					void controller.invalidate();
				},
				get: (controller) => {
					const selfPeerId = selfUserId as PeerId;
					controller.add({
						id: result.id,
						createdAt: result.createdAt,
						name: variables.name,
						issued: variables.issued,
						currencyCode: variables.currencyCode,
						participants:
							variables.participants?.map(({ peerId, role }, index) => {
								const matchedResult = result.participants[index];
								Iif (!matchedResult) {
									throw new Error(
										`Expected to have item index ${index} returned from receipt creation.`,
									);
								}
								return {
									role: peerId === selfPeerId ? "owner" : role,
									createdAt: matchedResult.createdAt,
									peerId,
								};
							}) ?? [],
						items:
							variables.items?.map((item, index) => {
								const matchedItem = result.items[index];
								Iif (!matchedItem) {
									throw new Error(
										`Expected to have item index ${index} returned from receipt creation.`,
									);
								}
								return {
									id: matchedItem.id,
									createdAt: matchedItem.createdAt,
									name: item.name,
									price: item.price,
									quantity: item.quantity,
									consumers:
										item.consumers?.map((consumer) => {
											const matchedConsumer = matchedItem.consumers?.find(
												(lookupConsumer) =>
													lookupConsumer.peerId === consumer.peerId,
											);
											Iif (!matchedConsumer) {
												throw new Error(
													`Expected to have consumer with peer id "${consumer.peerId}" returned from receipt creation.`,
												);
											}
											return {
												peerId: consumer.peerId,
												part: consumer.part,
												createdAt: matchedConsumer.createdAt,
											};
										}) ?? [],
									payers:
										item.payers?.map((payer) => {
											const matchedPayer = matchedItem.payers?.find(
												(lookupPayer) => lookupPayer.peerId === payer.peerId,
											);
											if (!matchedPayer) {
												throw new Error(
													`Expected to have payer with peer id "${payer.peerId}" returned from receipt creation.`,
												);
											}
											return {
												peerId: payer.peerId,
												part: payer.part,
												createdAt: matchedPayer.createdAt,
											};
										}) ?? [],
								};
							}) ?? [],
						payers:
							variables.payers?.map((payer, index) => {
								const matchedResult = result.payers[index];
								Iif (!matchedResult) {
									throw new Error(
										`Expected to have payer index ${index} returned from receipt creation.`,
									);
								}
								return {
									createdAt: matchedResult.createdAt,
									peerId: payer.peerId,
									part: payer.part,
								};
							}) ?? [],
						ownerPeerId: selfPeerId,
						selfPeerId,
						debts: { direction: "outcoming", debts: [] },
					});
				},
			});
		},
	mutateToastOptions:
		({ t }) =>
		(variablesSet) => ({
			text: t("toasts.addReceipt.mutate", {
				ns: "receipts",
				receiptsCount: variablesSet.length,
				receipts: variablesSet.map((variables) => `"${variables.name}"`),
			}),
		}),
	successToastOptions:
		({ t }) =>
		(_result, variablesSet) => ({
			text: t("toasts.addReceipt.success", {
				ns: "receipts",
				receiptsCount: variablesSet.length,
				receipts: variablesSet.map((variables) => `"${variables.name}"`),
			}),
		}),
	errorToastOptions:
		({ t }) =>
		(errors, variablesSet) => ({
			text: t("toasts.addReceipt.error", {
				ns: "receipts",
				receipts: variablesSet.map((variables) => `"${variables.name}"`),
				errors,
			}),
		}),
};