import db from "./db"; // Registro de cada tanda de actividades que crea el agendador: quién las creó, para quién y los ids // que quedaron en Dynamics. Sirve para (1) «Deshacer» una tanda (cancelando lo que siga abierto) y // (2) auditar. Se crea aquí (IF NOT EXISTS), igual que plan_cerrado: db.js se evalúa una sola vez por // proceso y una tabla agregada allí no existiría hasta reiniciar el servidor. db.exec(` CREATE TABLE IF NOT EXISTS agendador_lote ( id INTEGER PRIMARY KEY AUTOINCREMENT, creado_por TEXT NOT NULL, creado_en TEXT NOT NULL, owner_id TEXT NOT NULL, owner_email TEXT NOT NULL, owner_name TEXT NOT NULL, desde TEXT NOT NULL, hasta TEXT NOT NULL, items TEXT NOT NULL, deshecho_en TEXT, deshecho_por TEXT, resumen_deshacer TEXT ); `); export function guardarLote({ creadoPor, ownerId, ownerEmail, ownerName, desde, hasta, items }) { const info = db .prepare( `INSERT INTO agendador_lote (creado_por, creado_en, owner_id, owner_email, owner_name, desde, hasta, items) VALUES (?, ?, ?, ?, ?, ?, ?, ?)` ) .run(creadoPor, new Date().toISOString(), ownerId, ownerEmail, ownerName, desde, hasta, JSON.stringify(items)); return Number(info.lastInsertRowid); } const fila = (r) => ({ ...r, items: JSON.parse(r.items) }); export function getLote(id) { const r = db.prepare("SELECT * FROM agendador_lote WHERE id = ?").get(id); return r ? fila(r) : null; } // Tandas recientes de un ejecutivo (las más recientes primero). export function getLotesRecientes(ownerId) { return db .prepare("SELECT * FROM agendador_lote WHERE owner_id = ? ORDER BY id DESC LIMIT 20") .all(ownerId) .map(fila); } // Tandas de un ejecutivo en una semana (las más recientes primero). export function getLotesSemana(ownerId, desde, hasta) { return db .prepare("SELECT * FROM agendador_lote WHERE owner_id = ? AND desde = ? AND hasta = ? ORDER BY id DESC LIMIT 20") .all(ownerId, desde, hasta) .map(fila); } export function marcarDeshecho(id, por, resumen) { db.prepare("UPDATE agendador_lote SET deshecho_en = ?, deshecho_por = ?, resumen_deshacer = ? WHERE id = ?").run( new Date().toISOString(), por, JSON.stringify(resumen), id ); }