All files / packages/mutations/src/cache/user-connection-intentions get-all.ts

76.36% Statements 42/55
66.66% Branches 2/3
65.71% Functions 23/35
76.36% Lines 42/55

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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                                                176x       17x 17x     176x         17x 17x 17x 17x 17x 17x     17x         176x                                 176x         10x 10x 10x   10x           176x           7x 7x         176x 20x                       176x 4x       176x 20x 10x 10x 5x       176x 4x         176x 20x                 176x 4x 2x   176x 2x       2x                           176x       10x       10x                          
import type {
	InboundIntention,
	OutboundIntention,
	UserConnectionIntentions,
} from "~app/trpc-types";
import type { UserId } from "~db/ids";
import type { ItemWithIndex } from "~utils/array";
import { addToArray, removeFromArray, replaceInArray } from "~utils/array";
 
import type {
	ControllerContext,
	ControllerWith,
	SnapshotFn,
	UpdateFn,
} from "../../types";
import {
	applyUpdateFnWithRevert,
	applyWithRevert,
	getUpdatedData,
	withRef,
} from "../utils";
 
type Controller = ControllerWith<{
	procedure: ControllerContext["trpc"]["userConnectionIntentions"]["getAll"];
}>;
 
type Direction = "inbound" | "outbound";
type IntentionMapping = {
	inbound: InboundIntention;
	outbound: OutboundIntention;
};
 
const updateIntentions = (
	{ queryClient, procedure }: Controller,
	updater: (intentions: UserConnectionIntentions) => UserConnectionIntentions,
) =>
	queryClient.setQueryData(procedure.queryKey(), (intentions) =>
		getUpdatedData(intentions, updater),
	);
 
const updateDirectionIntentions = <D extends Direction>(
	controller: Controller,
	direction: Direction,
	updater: UpdateFn<IntentionMapping[D][]>,
) =>
	withRef<IntentionMapping[D][] | undefined>((ref) => {
		updateIntentions(controller, (intentions) => {
			const prevIntentions = intentions[direction] as IntentionMapping[D][];
			ref.current = prevIntentions;
			const nextIntentions = updater(prevIntentions);
			Iif (nextIntentions === prevIntentions) {
				return intentions;
			}
			return { ...intentions, [direction]: nextIntentions };
		});
	}).current;
 
const updateIntention =
	<D extends Direction>(
		controller: Controller,
		direction: Direction,
		userId: UserId,
	) =>
	(updater: UpdateFn<IntentionMapping[D]>) =>
		withRef<IntentionMapping[D] | undefined>((ref) => {
			updateDirectionIntentions<D>(controller, direction, (intentions) =>
				replaceInArray(
					intentions,
					(intention) => intention.user.id === userId,
					updater,
					ref,
				),
			);
		}).current;
 
const removeIntention = <D extends Direction>(
	controller: Controller,
	direction: Direction,
	userId: UserId,
) =>
	withRef<ItemWithIndex<IntentionMapping[D]> | undefined>((ref) => {
		updateDirectionIntentions<D>(controller, direction, (intentions) =>
			removeFromArray(
				intentions,
				(intention) => intention.user.id === userId,
				ref,
			),
		);
	}).current;
 
const addIntention = <D extends Direction>(
	controller: Controller,
	direction: Direction,
	intention: IntentionMapping[D],
	index = 0,
) => {
	updateDirectionIntentions<D>(controller, direction, (intentions) =>
		addToArray(intentions, intention, index),
	);
};
 
const updateRevert =
	<D extends Direction>(controller: Controller, direction: D) =>
	(
		userId: UserId,
		updateFn: UpdateFn<IntentionMapping[D]>,
		revertFn?: SnapshotFn<IntentionMapping[D]>,
	) =>
		applyUpdateFnWithRevert(
			updateIntention<D>(controller, direction, userId),
			updateFn,
			revertFn,
		);
 
const update =
	<D extends Direction>(controller: Controller, direction: D) =>
	(userId: UserId, updateFn: UpdateFn<IntentionMapping[D]>) =>
		updateIntention<D>(controller, direction, userId)(updateFn);
 
const removeRevert =
	<D extends Direction>(controller: Controller, direction: D) =>
	(userId: UserId) =>
		applyWithRevert(
			() => removeIntention(controller, direction, userId),
			({ index, item }) => addIntention(controller, direction, item, index),
		);
 
const remove =
	<D extends Direction>(controller: Controller, direction: D) =>
	(userId: UserId) =>
	() =>
		removeIntention(controller, direction, userId);
 
const addRevert =
	<D extends Direction>(controller: Controller, direction: D) =>
	(intention: IntentionMapping[D], index?: number) =>
		applyWithRevert(
			() => addIntention(controller, direction, intention, index),
			() => {
				removeIntention(controller, direction, intention.user.id);
			},
		);
 
const add =
	<D extends Direction>(controller: Controller, direction: D) =>
	(intention: IntentionMapping[D], index?: number) =>
		addIntention(controller, direction, intention, index);
 
export const getController = ({ queryClient, trpc }: ControllerContext) => {
	const controller = {
		queryClient,
		procedure: trpc.userConnectionIntentions.getAll,
	};
	return {
		inbound: {
			update: update(controller, "inbound"),
			add: add(controller, "inbound"),
			remove: remove(controller, "inbound"),
		},
		outbound: {
			update: update(controller, "outbound"),
			add: add(controller, "outbound"),
			remove: remove(controller, "outbound"),
		},
	};
};
 
export const getRevertController = ({
	queryClient,
	trpc,
}: ControllerContext) => {
	const controller = {
		queryClient,
		procedure: trpc.userConnectionIntentions.getAll,
	};
	return {
		inbound: {
			update: updateRevert(controller, "inbound"),
			add: addRevert(controller, "inbound"),
			remove: removeRevert(controller, "inbound"),
		},
		outbound: {
			update: updateRevert(controller, "outbound"),
			add: addRevert(controller, "outbound"),
			remove: removeRevert(controller, "outbound"),
		},
	};
};