All files / src/hooks/queries useAdminImports.ts

100% Statements 45/45
95% Branches 19/20
100% Functions 21/21
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 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                              5x 5x     2x 2x 2x 2x       4x                               6x 6x 6x   6x     3x 3x           3x     1x 1x                     3x     1x       1x               1x 1x           1x       1x     1x           1x 1x   1x 1x     1x     1x           1x 1x   1x 1x     1x           1x 1x   1x 1x     1x     1x        
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { adminApi } from "../../lib/api";
import {
	adminImportsApi,
	type MaterializeSelection,
	type ProImportStatus,
} from "../../lib/api/admin";
import { queryKeys } from "../../lib/query-keys";
 
export interface ProSearchResult {
	id: string;
	businessName: string;
}
 
export function useAdminProSearch(query: string) {
	const trimmed = query.trim();
	return useQuery({
		queryKey: ["admin", "pros", "search", trimmed],
		queryFn: async () => {
			const params: Record<string, string> = { limit: "20" };
			if (trimmed) params.search = trimmed;
			const response = await adminApi.listPros(params);
			const list = (response.data ?? []) as Array<{
				id: string;
				businessName?: string;
			}>;
			return list.map((p) => ({
				id: p.id,
				businessName: p.businessName ?? "(unnamed)",
			}));
		},
		// Don't fire on first keystroke; small debounce-ish behavior:
		// only run when query has 2+ chars OR is explicitly empty (initial list).
		enabled: trimmed.length === 0 || trimmed.length >= 2,
		staleTime: 30_000,
	});
}
 
export function useAdminImports(filters?: {
	proId?: string;
	status?: ProImportStatus;
}) {
	const filterParams: Record<string, string> = {};
	if (filters?.proId) filterParams.proId = filters.proId;
	if (filters?.status) filterParams.status = filters.status;
 
	return useQuery({
		queryKey: queryKeys.admin.imports.list(filterParams),
		queryFn: async () => {
			const response = await adminImportsApi.list(filters);
			return response.data?.imports ?? [];
		},
	});
}
 
export function useAdminImport(importId: string | null) {
	return useQuery({
		queryKey: queryKeys.admin.imports.detail(importId ?? ""),
		queryFn: async () => {
			const response = await adminImportsApi.get(importId as string);
			return response.data;
		},
		enabled: !!importId,
	});
}
 
export function useAdminImportEvents(
	importId: string | null,
	since: number | undefined,
	options?: { refetchInterval?: number | false },
) {
	return useQuery({
		queryKey: queryKeys.admin.imports.events(importId ?? "", since),
		queryFn: async () => {
			const response = await adminImportsApi.events(
				importId as string,
				since,
			);
			return response.data;
		},
		enabled: !!importId,
		refetchInterval: options?.refetchInterval ?? false,
	});
}
 
export function useEnqueueImport() {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: async (input: {
			proId: string;
			sourceUrl: string;
			consentAt?: number;
		}) => {
			const response = await adminImportsApi.enqueue(input.proId, {
				sourceUrl: input.sourceUrl,
				consentAt: input.consentAt,
			});
			return response.data;
		},
		onSuccess: () => {
			qc.invalidateQueries({ queryKey: queryKeys.admin.imports.all });
		},
	});
}
 
export function useApproveImport(importId: string) {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: async (selection: MaterializeSelection) => {
			const response = await adminImportsApi.approve(importId, selection);
			return response.data;
		},
		onSuccess: () => {
			qc.invalidateQueries({
				queryKey: queryKeys.admin.imports.detail(importId),
			});
			qc.invalidateQueries({ queryKey: queryKeys.admin.imports.all });
		},
	});
}
 
export function useBulkApproveImports() {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: async (importIds: string[]) => {
			const response = await adminImportsApi.bulkApprove(importIds);
			return response.data;
		},
		onSuccess: () => {
			qc.invalidateQueries({ queryKey: queryKeys.admin.imports.all });
		},
	});
}
 
export function useCancelImport(importId: string) {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: async () => {
			const response = await adminImportsApi.cancel(importId);
			return response.data;
		},
		onSuccess: () => {
			qc.invalidateQueries({
				queryKey: queryKeys.admin.imports.detail(importId),
			});
			qc.invalidateQueries({ queryKey: queryKeys.admin.imports.all });
		},
	});
}