remover node_modules y añadir .gitignore
This commit is contained in:
+60
-1
@@ -796,6 +796,14 @@ app.delete('/api/categorias/:id/subcategorias/:subId', requireCategoriasManageme
|
||||
return res.status(400).json({ error: 'Primero elimina las sub-subcategorías' });
|
||||
}
|
||||
|
||||
// Determinar si la subcat a eliminar es nivel 1 (hijo directo de la categoría)
|
||||
// o nivel 2 (sub-subcategoría, hijo de una subcat). Esto define el nuevo path
|
||||
// de los productos: subir un nivel.
|
||||
let isSubSub = false;
|
||||
if (Array.isArray(parent.subcategorias)) {
|
||||
isSubSub = !parent.subcategorias.some(s => s.id === subId);
|
||||
}
|
||||
|
||||
// Quitar del árbol (recursivo, encuentra el padre correcto y splice)
|
||||
removeSubcatRecursive(parent.subcategorias, subId);
|
||||
// Mantener array limpio: si la raíz quedó sin subcats, eliminamos la propiedad
|
||||
@@ -803,8 +811,59 @@ app.delete('/api/categorias/:id/subcategorias/:subId', requireCategoriasManageme
|
||||
delete parent.subcategorias;
|
||||
}
|
||||
|
||||
// Mover productos asignados a esta subcat al padre inmediato
|
||||
const productos = await getProductos();
|
||||
let movidos = 0;
|
||||
for (const p of productos) {
|
||||
if (!Array.isArray(p.sub) || p.sub.length === 0) continue;
|
||||
|
||||
// Si el producto está en el path de la subcat eliminada, subirlo un nivel
|
||||
// Ejemplo: subId es 'hombre' en [bolsos, hombre, morrales] -> queda [bolsos, hombre]
|
||||
// Ejemplo: subId es 'morrales' en [bolsos, hombre, morrales] -> queda [bolsos, hombre]
|
||||
const idx = p.sub.indexOf(subId);
|
||||
if (idx === -1) continue;
|
||||
|
||||
// Calcular nuevo path y carpeta destino
|
||||
const oldSub = [...p.sub];
|
||||
const newSub = oldSub.slice(0, idx); // elimina el subId y lo que esté debajo
|
||||
|
||||
// Si es sub-sub (nivel 2), al subir queda en nivel 1 (dentro de la categoría).
|
||||
// Si es nivel 1, al subir queda vacío (producto directo de la categoría).
|
||||
// Actualizar JSON
|
||||
p.sub = newSub;
|
||||
|
||||
// Mover imagen físicamente a la nueva carpeta
|
||||
if (p.img && !p.img.startsWith('agotados/')) {
|
||||
const oldPathParts = [p.cat, ...oldSub];
|
||||
const newPathParts = [p.cat, ...newSub];
|
||||
const oldDir = oldPathParts.join('/');
|
||||
const newDir = newPathParts.join('/');
|
||||
|
||||
const oldImgPath = path.join(IMG_DIR, oldDir, `${p.ref}.webp`);
|
||||
const newImgDir = path.join(IMG_DIR, newDir);
|
||||
const newImgPath = path.join(newImgDir, `${p.ref}.webp`);
|
||||
|
||||
try {
|
||||
await fs.mkdir(newImgDir, { recursive: true });
|
||||
await fs.rename(oldImgPath, newImgPath);
|
||||
p.img = `${newDir}/${p.ref}.webp`;
|
||||
movidos++;
|
||||
} catch (err) {
|
||||
// Si la imagen no existe u otro error, solo actualizamos el JSON
|
||||
if (err.code !== 'ENOENT') {
|
||||
console.error(`Error moviendo imagen ${p.img}:`, err.message);
|
||||
}
|
||||
p.img = `${newDir}/${p.ref}.webp`;
|
||||
movidos++;
|
||||
}
|
||||
} else {
|
||||
movidos++;
|
||||
}
|
||||
}
|
||||
|
||||
await saveProductos(productos);
|
||||
await saveCategorias(categorias);
|
||||
res.json({ ok: true });
|
||||
res.json({ ok: true, movidos });
|
||||
} catch (err) {
|
||||
console.error('Delete subcategory error:', err);
|
||||
res.status(500).json({ error: 'Error al eliminar subcategoría' });
|
||||
|
||||
Reference in New Issue
Block a user