All files / src/lib/api/admin imports.ts

100% Statements 15/15
100% Branches 8/8
100% Functions 7/7
100% Lines 13/13

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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                                                                                                                                                                                                                                                                                              95x   2x 2x 2x 2x 2x           1x       2x 2x                 1x             1x             1x                           1x            
/**
 * Admin Pro Imports API client.
 *
 * Wraps the 6 admin endpoints under `/api/admin/imports/*` and the
 * enqueue endpoint at `/api/admin/pros/:proId/imports`.
 */
 
import { request } from "../base";
 
export type ProImportStatus =
	| "queued"
	| "running"
	| "completed"
	| "failed"
	| "cancelled";
 
export interface ProImportListItem {
	id: string;
	proId: string;
	proBusinessName: string | null;
	sourceUrl: string;
	status: ProImportStatus;
	statsJson: {
		projectsFound?: number;
		photosDownloaded?: number;
		costCents?: number;
		manifestKey?: string;
	} | null;
	errorCode: string | null;
	errorMessage: string | null;
	startedAt: number | null;
	completedAt: number | null;
	createdAt: number;
	approvedAt: number | null;
	submittedBy: string | null;
}
 
export interface ProImportDetail extends ProImportListItem {
	sourceType: string;
	consentAt: number;
	idempotencyKey: string;
	approvedBy: string | null;
}
 
export interface ScrapedPhoto {
	file: string;
	originalUrl: string;
	caption: string;
	isCover: boolean;
}
 
export interface ScrapedProject {
	title: string;
	slug: string;
	description: string;
	propertyTypeRaw: string;
	budgetRangeRaw: string;
	locationRaw: string;
	yearCompleted: number | null;
	durationRaw: string;
	photos: ScrapedPhoto[];
}
 
export interface ScrapedProfile {
	businessName: string;
	slug: string;
	sourcePlatform: string;
	sourceUrl: string;
	whatsapp: string;
	phoneAlternate: string;
	email: string;
	tagline: string;
	about: string;
	cityRaw: string;
	addressLine1: string;
	addressCity: string;
	addressState: string;
	addressPincode: string;
	teamSizeRaw: string;
	yearsInBusiness: number | null;
	serviceAreasRaw: string[];
	credentialsRaw: string[];
	reviewRating: number | null;
	reviewCount: number | null;
	typicalCostRaw: string;
	logoUrl: string;
	coverImage: string;
}
 
export type YamlParsed =
	| { ok: true; data: { pro: ScrapedProfile; projects: ScrapedProject[] } }
	| { ok: false; errors: string[] };
 
export interface TaxonomyMatch {
	id: string | null;
	score: number;
	candidate: string;
	recommendation: "auto" | "review" | "unknown";
	alternatives: Array<{ id: string; name: string; score: number }>;
}
 
export interface TaxonomySuggestions {
	city: TaxonomyMatch | null;
	serviceAreas: TaxonomyMatch[];
	materials: TaxonomyMatch[];
}
 
export interface ProImportDetailResponse {
	import: ProImportDetail;
	yamlParsed: YamlParsed | null;
	taxonomy: TaxonomySuggestions | null;
}
 
export interface ProImportEvent {
	id: string;
	importId: string;
	kind: string;
	payloadJson: Record<string, unknown> | null;
	createdAt: number;
}
 
export interface ProImportEventsResponse {
	events: ProImportEvent[];
	cursor: number;
}
 
export interface MaterializeSelection {
	profile: { fields: string[] };
	projects: Array<{
		scrapedSlug: string;
		include: boolean;
		photos: string[];
		overrides?: Partial<{ title: string; description: string }>;
	}>;
}
 
export interface MaterializeResult {
	profileFieldsApplied: number;
	projectsCreated: number;
	photosCopied: number;
	skipped: Array<{ reason: string; slug: string }>;
}
 
export const adminImportsApi = {
	async list(filters?: { proId?: string; status?: ProImportStatus }) {
		const params = new URLSearchParams();
		if (filters?.proId) params.set("proId", filters.proId);
		if (filters?.status) params.set("status", filters.status);
		const qs = params.toString() ? `?${params.toString()}` : "";
		return request<{ imports: ProImportListItem[] }>(
			`/api/admin/imports${qs}`,
		);
	},
 
	async get(importId: string) {
		return request<ProImportDetailResponse>(`/api/admin/imports/${importId}`);
	},
 
	async events(importId: string, since?: number) {
		const qs = since ? `?since=${since}` : "";
		return request<ProImportEventsResponse>(
			`/api/admin/imports/${importId}/events${qs}`,
		);
	},
 
	async enqueue(
		proId: string,
		body: { sourceUrl: string; consentAt?: number; force?: boolean },
	) {
		return request<{ importId: string; status: ProImportStatus }>(
			`/api/admin/pros/${proId}/imports`,
			{ method: "POST", body },
		);
	},
 
	async approve(importId: string, selection: MaterializeSelection) {
		return request<MaterializeResult>(
			`/api/admin/imports/${importId}/approve`,
			{ method: "POST", body: { selection } },
		);
	},
 
	async bulkApprove(importIds: string[]) {
		return request<{
			results: Array<{
				importId: string;
				ok: boolean;
				result?: MaterializeResult;
				error?: string;
			}>;
		}>(`/api/admin/imports/bulk-approve`, {
			method: "POST",
			body: { importIds },
		});
	},
 
	async cancel(importId: string) {
		return request<{ id: string; status: ProImportStatus }>(
			`/api/admin/imports/${importId}/cancel`,
			{ method: "POST" },
		);
	},
};