Move source mapping to scrape database
This commit is contained in:
@@ -129,16 +129,23 @@ export async function listSuggestedProducts(
|
||||
|
||||
export async function listMappingSources(config, { manufacturerId, checkUrls = false }) {
|
||||
try {
|
||||
const result = await queryEndpoint(config, mappingSourcesSql({ manufacturerId }));
|
||||
const result = await queryEndpoint(
|
||||
config,
|
||||
mappingSourcesSql({
|
||||
manufacturerId,
|
||||
scrapeDatabase: config.database?.scrapeName || "catalog_scrape",
|
||||
}),
|
||||
);
|
||||
const mappingTableName = `${config.database?.scrapeName || "catalog_scrape"}.scrape_manufacturer_sources`;
|
||||
const items = result.items.map((row) =>
|
||||
normalizeMappingSource(row, "ps_product_catalog_manufacturer_source"),
|
||||
normalizeMappingSource(row, mappingTableName),
|
||||
);
|
||||
const checkedItems = checkUrls ? await checkMappingSourceUrls(items) : items;
|
||||
|
||||
return {
|
||||
configured: result.configured,
|
||||
mapped: checkedItems.some((item) => item.enabled),
|
||||
tableName: "ps_product_catalog_manufacturer_source",
|
||||
tableName: mappingTableName,
|
||||
items: checkedItems,
|
||||
sourceCount: checkedItems.filter((item) => item.enabled).length,
|
||||
testedCount: checkedItems.filter((item) => item.enabled && item.health === "ok").length,
|
||||
|
||||
@@ -382,14 +382,15 @@ export function mappingSourceTablesSql() {
|
||||
`;
|
||||
}
|
||||
|
||||
export function mappingSourcesSql({ manufacturerId }) {
|
||||
export function mappingSourcesSql({ manufacturerId, scrapeDatabase = "catalog_scrape" }) {
|
||||
const safeManufacturerId = parsePositiveInteger(manufacturerId, "manufacturerId");
|
||||
const scrapeSchema = escapeSqlIdentifier(scrapeDatabase);
|
||||
|
||||
return `
|
||||
SELECT
|
||||
ms.id_product_catalog_manufacturer_source AS mapping_id,
|
||||
ms.id_manufacturer,
|
||||
ms.id_product_catalog_source,
|
||||
ms.id AS mapping_id,
|
||||
ms.manufacturer_id AS id_manufacturer,
|
||||
s.id AS id_product_catalog_source,
|
||||
ms.enabled AS mapping_enabled,
|
||||
ms.priority AS mapping_priority,
|
||||
s.source_key,
|
||||
@@ -409,10 +410,10 @@ export function mappingSourcesSql({ manufacturerId }) {
|
||||
s.browser_headless,
|
||||
s.wait_after_load_ms,
|
||||
s.note
|
||||
FROM ps_product_catalog_manufacturer_source ms
|
||||
JOIN ps_product_catalog_source s
|
||||
ON s.id_product_catalog_source = ms.id_product_catalog_source
|
||||
WHERE ms.id_manufacturer = ${safeManufacturerId}
|
||||
FROM ${scrapeSchema}.scrape_manufacturer_sources ms
|
||||
JOIN ${scrapeSchema}.scrape_sources s
|
||||
ON s.id = ms.source_id
|
||||
WHERE ms.manufacturer_id = ${safeManufacturerId}
|
||||
ORDER BY
|
||||
ms.priority ASC,
|
||||
s.priority ASC,
|
||||
@@ -420,6 +421,14 @@ export function mappingSourcesSql({ manufacturerId }) {
|
||||
`;
|
||||
}
|
||||
|
||||
function escapeSqlIdentifier(value) {
|
||||
const text = String(value ?? "").trim();
|
||||
if (!/^[a-zA-Z0-9_]+$/.test(text)) {
|
||||
throw new Error("SQL identifier contains unsupported characters.");
|
||||
}
|
||||
return `\`${text}\``;
|
||||
}
|
||||
|
||||
function escapeSqlString(value) {
|
||||
return String(value ?? "").replaceAll("\\", "\\\\").replaceAll("'", "''").trim();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user