audit and sync
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { searchSnapshotItems } from "../../shared/wooSnapshot.js";
|
||||
import { listProducts, getProductByWooId, updateProductSellUnit, bulkUpdateProductSellUnit, updateProduct } from "../db/repo.js";
|
||||
import { searchSnapshotItems, syncFromWoo, pushProductToWoo } from "../../shared/wooSnapshot.js";
|
||||
import { listProducts, getProductByWooId, updateProductSellUnit, bulkUpdateProductSellUnit, updateProduct, insertAuditLog } from "../db/repo.js";
|
||||
|
||||
export async function handleSearchProducts({ tenantId, q = "", limit = "10", forceWoo = "0" }) {
|
||||
const { items, source } = await searchSnapshotItems({
|
||||
@@ -20,9 +20,34 @@ export async function handleGetProduct({ tenantId, wooProductId }) {
|
||||
}
|
||||
|
||||
export async function handleSyncProducts({ tenantId }) {
|
||||
// This is a placeholder - actual sync would fetch from Woo API
|
||||
// For now, just return success
|
||||
return { ok: true, message: "Sync triggered (use import script for full sync)" };
|
||||
// Placeholder legacy - redirigir a syncFromWoo
|
||||
return handleSyncFromWoo({ tenantId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Sincroniza todos los productos desde WooCommerce (solo para emergencias)
|
||||
*/
|
||||
export async function handleSyncFromWoo({ tenantId }) {
|
||||
console.log("[products] handleSyncFromWoo starting...");
|
||||
|
||||
try {
|
||||
const result = await syncFromWoo({ tenantId });
|
||||
|
||||
// Registrar en audit_log
|
||||
await insertAuditLog({
|
||||
tenantId,
|
||||
entityType: 'product',
|
||||
entityId: 'all',
|
||||
action: 'sync_from_woo',
|
||||
changes: { synced_count: result.synced, ms: result.ms },
|
||||
actor: 'ui'
|
||||
});
|
||||
|
||||
return { ok: true, synced: result.synced, ms: result.ms };
|
||||
} catch (err) {
|
||||
console.error("[products] handleSyncFromWoo error:", err);
|
||||
return { ok: false, error: err.message };
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleUpdateProductUnit({ tenantId, wooProductId, sell_unit }) {
|
||||
@@ -35,8 +60,43 @@ export async function handleBulkUpdateProductUnit({ tenantId, wooProductIds, sel
|
||||
return { ok: true, updated_count: wooProductIds.length, sell_unit };
|
||||
}
|
||||
|
||||
/**
|
||||
* Actualiza un producto localmente y automáticamente lo pushea a WooCommerce
|
||||
*/
|
||||
export async function handleUpdateProduct({ tenantId, wooProductId, sell_unit, categories }) {
|
||||
// 1. Guardar cambios localmente
|
||||
await updateProduct({ tenantId, wooProductId, sell_unit, categories });
|
||||
return { ok: true, woo_product_id: wooProductId, sell_unit, categories };
|
||||
|
||||
// 2. Auto-push a WooCommerce (best effort - no falla si Woo no responde)
|
||||
let wooPushResult = null;
|
||||
try {
|
||||
wooPushResult = await pushProductToWoo({ tenantId, wooProductId, categories, sellUnit: sell_unit });
|
||||
console.log("[products] handleUpdateProduct: pushed to Woo", { wooProductId, updated: wooPushResult?.updated });
|
||||
} catch (err) {
|
||||
console.error("[products] handleUpdateProduct: Woo push failed (continuing anyway)", err.message);
|
||||
// No lanzamos el error - el guardado local fue exitoso
|
||||
}
|
||||
|
||||
// 3. Registrar en audit_log
|
||||
await insertAuditLog({
|
||||
tenantId,
|
||||
entityType: 'product',
|
||||
entityId: String(wooProductId),
|
||||
action: 'update',
|
||||
changes: {
|
||||
sell_unit: { new: sell_unit },
|
||||
categories: { new: categories },
|
||||
woo_synced: wooPushResult?.updated ?? false
|
||||
},
|
||||
actor: 'ui'
|
||||
});
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
woo_product_id: wooProductId,
|
||||
sell_unit,
|
||||
categories,
|
||||
woo_synced: wooPushResult?.updated ?? false
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user