84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
import { MATCH_CONFIGS } from "../dashboard/admin/matcher/constants";
|
|
import { MatchableEntity, MatchConfig } from "../dashboard/admin/matcher/types";
|
|
import {
|
|
normalizeEntity,
|
|
resolveCollection,
|
|
} from "../dashboard/admin/matcher/utils";
|
|
|
|
export async function getMatcherData(
|
|
matchType: string,
|
|
cookieHeader: string,
|
|
baseUrl: string
|
|
): Promise<{
|
|
sourceItems: MatchableEntity[];
|
|
targetItems: MatchableEntity[];
|
|
}> {
|
|
const config = MATCH_CONFIGS[matchType];
|
|
if (!config) {
|
|
return { sourceItems: [], targetItems: [] };
|
|
}
|
|
|
|
try {
|
|
const [sourceItems, targetItems] = await Promise.all([
|
|
fetchEntities(config.sourceEndpoint, config, cookieHeader, baseUrl, true),
|
|
fetchEntities(
|
|
config.targetEndpoint,
|
|
config,
|
|
cookieHeader,
|
|
baseUrl,
|
|
false
|
|
),
|
|
]);
|
|
|
|
return { sourceItems, targetItems };
|
|
} catch (error) {
|
|
console.error("Error fetching matcher data:", error);
|
|
return { sourceItems: [], targetItems: [] };
|
|
}
|
|
}
|
|
|
|
export async function fetchEntities(
|
|
endpoint: string,
|
|
config: MatchConfig,
|
|
cookieHeader: string,
|
|
baseUrl: string,
|
|
isSource: boolean
|
|
): Promise<MatchableEntity[]> {
|
|
const url = new URL(`${baseUrl}${endpoint}`);
|
|
|
|
// For now, fetch all items (no pagination limit)
|
|
// In production, you might want to add pagination
|
|
const response = await fetch(url.toString(), {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Cookie: cookieHeader,
|
|
},
|
|
body: JSON.stringify({
|
|
filters: {},
|
|
pagination: { page: 1, limit: 1000 },
|
|
sort: {},
|
|
}),
|
|
cache: "no-store",
|
|
});
|
|
|
|
if (!response.ok) {
|
|
console.error(`Failed to fetch from ${endpoint}:`, response.status);
|
|
return [];
|
|
}
|
|
|
|
const data = await response.json();
|
|
console.log("[fetchEntities] data:", data, url.toString());
|
|
const collectionKeys = isSource
|
|
? config.sourceCollectionKeys
|
|
: config.targetCollectionKeys;
|
|
const primaryKey = isSource
|
|
? config.sourcePrimaryKey
|
|
: config.targetPrimaryKey;
|
|
|
|
const collection = resolveCollection(data, collectionKeys);
|
|
return collection.map((item, index) =>
|
|
normalizeEntity(item, primaryKey || "name", index + 1)
|
|
);
|
|
}
|