All files / src/lib/loaders admin-loaders.ts

100% Statements 49/49
100% Branches 25/25
100% Functions 44/44
100% Lines 36/36

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            6x               4x           4x                                   4x     2x                     2x   1x           4x   1x     4x   1x           3x     1x     3x   1x           4x     2x             4x     2x             2x   1x               6x     3x     2x           3x   2x           4x   1x     4x     2x           3x     2x           3x     2x           4x     2x        
import { queryClient } from "../query-client";
import { queryKeys } from "../query-keys";
import { adminApi } from "../api";
import { STALE_2MIN, STALE_5MIN } from "./shared";
 
export function prefetchAdminDashboard() {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.dashboard(),
		queryFn: async () => {
			const [
				prosRes,
				publishedProsRes,
				projectsRes,
				publishedProjectsRes,
			] = await Promise.all([
				adminApi.listPros({ limit: "5" }),
				adminApi.listPros({ limit: "1", status: "published" }),
				adminApi.listProjects({ limit: "1" }),
				adminApi.listProjects({ limit: "1", status: "published" }),
			]);
			return {
				stats: {
					totalPros: prosRes.meta?.total ?? 0,
					publishedPros: publishedProsRes.meta?.total ?? 0,
					totalProjects: projectsRes.meta?.total ?? 0,
					publishedProjects: publishedProjectsRes.meta?.total ?? 0,
				},
				recentPros: (prosRes.data || []).slice(0, 5),
			};
		},
		staleTime: STALE_2MIN,
	});
}
 
export function prefetchAdminPros() {
	// #593: mirror AdminProsPage's default (incomplete shells filtered out) so the
	// loader populates the same queryKey the page reads — otherwise the prefetch
	// is wasted and the page refetches on mount.
	queryClient.prefetchInfiniteQuery({
		queryKey: queryKeys.admin.pros.list({ includeIncomplete: "false" }),
		queryFn: ({ pageParam = 0 }) =>
			adminApi.listPros({
				limit: "20",
				offset: pageParam.toString(),
				includeIncomplete: "false",
			}),
		initialPageParam: 0,
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminProDetail(proId: string) {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.pros.detail(proId),
		queryFn: () => adminApi.getPro(proId).then((r) => r.data),
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminProAnalytics(proId: string) {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.pros.detail(proId),
		queryFn: () => adminApi.getPro(proId).then((r) => r.data),
		staleTime: STALE_5MIN,
	});
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.pros.stats(proId),
		queryFn: () => adminApi.getProStats(proId).then((r) => r.data),
		staleTime: STALE_2MIN,
	});
}
 
export function prefetchAdminProCompanyProfile(proId: string) {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.pros.companyProfile(proId),
		queryFn: () =>
			adminApi.getCompanyProfile(proId).then((r) => r.data),
		staleTime: STALE_5MIN,
	});
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.pros.detail(proId),
		queryFn: () => adminApi.getPro(proId).then((r) => r.data),
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminProjects() {
	queryClient.prefetchInfiniteQuery({
		queryKey: queryKeys.admin.projects.list({}),
		queryFn: ({ pageParam = 0 }) =>
			adminApi.listProjects({ limit: "20", offset: pageParam.toString() }),
		initialPageParam: 0,
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminUsers() {
	queryClient.prefetchInfiniteQuery({
		queryKey: queryKeys.admin.users.list({}),
		queryFn: ({ pageParam = 0 }) =>
			adminApi.listUsers({ limit: "20", offset: pageParam.toString() }),
		initialPageParam: 0,
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminUserDetail(userId: string) {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.users.detail(userId),
		queryFn: () => adminApi.getUser(userId).then((r) => r.data),
		staleTime: STALE_5MIN,
	});
}
 
// Admin taxonomy types used for prefetching — must match the camelCase format
// expected by the API (see apps/api/src/routes/admin/taxonomy.config.ts).
// Exported for test validation against the API contract.
export const ALL_ADMIN_TAXONOMY_TYPES = ["businessTypes"] as const;
 
export function prefetchAdminTaxonomy() {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.taxonomy("businessTypes"),
		queryFn: () =>
			adminApi.listTaxonomy("businessTypes", false).then((r) => r.data || []),
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminBlogs() {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.blogs.list({}),
		queryFn: () => adminApi.listBlogs().then((r) => r.data || []),
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminBlogEdit(blogId: string) {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.blogs.detail(blogId),
		queryFn: () => adminApi.getBlog(blogId).then((r) => r.data),
		staleTime: STALE_5MIN,
	});
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.blogs.categories(),
		queryFn: () =>
			adminApi.listBlogCategories(true).then((r) => r.data || []),
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminBlogCategories() {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.blogs.categories(),
		queryFn: () =>
			adminApi.listBlogCategories(true).then((r) => r.data || []),
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminBlogSuggestions() {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.blogs.suggestions({}),
		queryFn: () =>
			adminApi.listBlogSuggestions().then((r) => r.data || []),
		staleTime: STALE_5MIN,
	});
}
 
export function prefetchAdminRoomCategories() {
	queryClient.ensureQueryData({
		queryKey: queryKeys.admin.roomCategories.list(),
		queryFn: () =>
			adminApi.listRoomCategoriesWithStats().then((r) => r.data || []),
		staleTime: STALE_5MIN,
	});
}