mejoras en el modelo de clarificacion de productos

This commit is contained in:
Lucas Tettamanti
2026-01-17 06:31:49 -03:00
parent 63b9ecef61
commit 204403560e
24 changed files with 1940 additions and 873 deletions

View File

@@ -146,36 +146,7 @@ export async function searchSnapshotItems({ tenantId, q = "", limit = 12 }) {
const query = String(q || "").trim();
if (!query) return { items: [], source: "snapshot" };
const like = `%${query}%`;
// #region agent log
const totalSnapshot = await pool.query(
"select count(*)::int as cnt from woo_products_snapshot where tenant_id=$1",
[tenantId]
);
const totalSellable = await pool.query(
"select count(*)::int as cnt from sellable_items where tenant_id=$1",
[tenantId]
);
fetch("http://127.0.0.1:7242/ingest/86c7b1cd-c414-4eae-852c-08e57e562b3b", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
sessionId: "debug-session",
runId: "pre-fix",
hypothesisId: "H8",
location: "wooSnapshot.js:152",
message: "snapshot_counts",
data: {
tenantId: tenantId || null,
total_snapshot: totalSnapshot?.rows?.[0]?.cnt ?? null,
total_sellable: totalSellable?.rows?.[0]?.cnt ?? null,
query,
limit: lim,
},
timestamp: Date.now(),
}),
}).catch(() => {});
// #endregion
const sql = `
const sql = `
select *
from sellable_items
where tenant_id=$1
@@ -184,26 +155,7 @@ export async function searchSnapshotItems({ tenantId, q = "", limit = 12 }) {
limit $3
`;
const { rows } = await pool.query(sql, [tenantId, like, lim]);
// #region agent log
fetch("http://127.0.0.1:7242/ingest/86c7b1cd-c414-4eae-852c-08e57e562b3b", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
sessionId: "debug-session",
runId: "pre-fix",
hypothesisId: "H8",
location: "wooSnapshot.js:168",
message: "snapshot_search_result",
data: {
query,
found: rows.length,
sample_names: rows.slice(0, 3).map((r) => r?.name).filter(Boolean),
},
timestamp: Date.now(),
}),
}).catch(() => {});
// #endregion
return { items: rows.map(snapshotRowToItem), source: "snapshot" };
return { items: rows.map(snapshotRowToItem), source: "snapshot" };
}
export async function getSnapshotPriceByWooId({ tenantId, wooId }) {
@@ -219,6 +171,27 @@ export async function getSnapshotPriceByWooId({ tenantId, wooId }) {
return price == null ? null : Number(price);
}
/**
* Obtiene productos de sellable_items por sus woo_product_ids.
* Usado para incluir productos encontrados vía aliases.
*/
export async function getSnapshotItemsByIds({ tenantId, wooProductIds }) {
if (!Array.isArray(wooProductIds) || wooProductIds.length === 0) {
return { items: [], source: "snapshot_by_id" };
}
const ids = wooProductIds.map(id => Number(id)).filter(id => id > 0);
if (ids.length === 0) return { items: [], source: "snapshot_by_id" };
const placeholders = ids.map((_, i) => `$${i + 2}`).join(",");
const sql = `
select *
from sellable_items
where tenant_id=$1 and woo_id in (${placeholders})
`;
const { rows } = await pool.query(sql, [tenantId, ...ids]);
return { items: rows.map(snapshotRowToItem), source: "snapshot_by_id" };
}
export async function upsertSnapshotItems({ tenantId, items, runId = null }) {
const rows = Array.isArray(items) ? items : [];
for (const item of rows) {