Actualizar worker-multi-estado.js
This commit is contained in:
parent
0e35df2319
commit
c551bcde8b
|
|
@ -22,7 +22,6 @@ function toServerTimestamp() { return admin.firestore.FieldValue.serverTimestamp
|
||||||
function timeToMultiValue(timeStr) {
|
function timeToMultiValue(timeStr) {
|
||||||
if (!timeStr) return "";
|
if (!timeStr) return "";
|
||||||
const [h, m] = timeStr.split(':').map(Number);
|
const [h, m] = timeStr.split(':').map(Number);
|
||||||
// Formato interno de Multi: segundos desde medianoche
|
|
||||||
return String((h * 3600) + (m * 60));
|
return String((h * 3600) + (m * 60));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,9 +98,9 @@ async function processChangeState(page, db, jobData) {
|
||||||
await page.waitForSelector('select.answer-select', { timeout: 20000 });
|
await page.waitForSelector('select.answer-select', { timeout: 20000 });
|
||||||
await page.waitForTimeout(1500);
|
await page.waitForTimeout(1500);
|
||||||
|
|
||||||
console.log('📝 Rellenando formulario (Modo Preciso)...');
|
console.log('📝 Rellenando formulario...');
|
||||||
|
|
||||||
// 3. MOTIVO (Primer select de la página)
|
// 3. MOTIVO
|
||||||
const reasonSel = page.locator('select.answer-select').first();
|
const reasonSel = page.locator('select.answer-select').first();
|
||||||
await reasonSel.selectOption(String(reasonValue));
|
await reasonSel.selectOption(String(reasonValue));
|
||||||
await forceUpdate(await reasonSel.elementHandle());
|
await forceUpdate(await reasonSel.elementHandle());
|
||||||
|
|
@ -113,31 +112,24 @@ async function processChangeState(page, db, jobData) {
|
||||||
await forceUpdate(await commentBox.elementHandle());
|
await forceUpdate(await commentBox.elementHandle());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. FECHA (AQUÍ ESTABA EL ERROR: AHORA USAMOS .first())
|
// 5. FECHA (Usamos .first() para evitar error de doble fecha)
|
||||||
if (dateStr) {
|
if (dateStr) {
|
||||||
// Había 2 inputs de fecha, cogemos el primero que es el de "Siguiente Acción"
|
|
||||||
const dateInput = page.locator('input[type="date"]').first();
|
const dateInput = page.locator('input[type="date"]').first();
|
||||||
await dateInput.fill(dateStr);
|
await dateInput.fill(dateStr);
|
||||||
await forceUpdate(await dateInput.elementHandle());
|
await forceUpdate(await dateInput.elementHandle());
|
||||||
// Click fuera para asegurar validación
|
|
||||||
await page.click('body');
|
await page.click('body');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. HORA
|
// 6. HORA (Usamos XPath para buscar por valor)
|
||||||
if (timeStr) {
|
if (timeStr) {
|
||||||
const secondsValue = timeToMultiValue(timeStr); // ej: "28800" para 08:00
|
const secondsValue = timeToMultiValue(timeStr);
|
||||||
console.log(`🕒 Intentando poner hora: ${timeStr} (Valor interno: ${secondsValue})`);
|
|
||||||
|
|
||||||
// Búsqueda por XPath (Infalible)
|
|
||||||
const timeSelectHandle = await page.$(`xpath=//select[.//option[@value="${secondsValue}"]]`);
|
const timeSelectHandle = await page.$(`xpath=//select[.//option[@value="${secondsValue}"]]`);
|
||||||
|
|
||||||
if (timeSelectHandle) {
|
if (timeSelectHandle) {
|
||||||
await timeSelectHandle.selectOption(secondsValue);
|
await timeSelectHandle.selectOption(secondsValue);
|
||||||
await forceUpdate(timeSelectHandle);
|
await forceUpdate(timeSelectHandle);
|
||||||
console.log('✅ Hora seleccionada correctamente.');
|
|
||||||
} else {
|
} else {
|
||||||
console.log(`⚠️ ERROR: No encuentro opción para hora ${timeStr}. Probando fallback...`);
|
// Fallback
|
||||||
// Fallback: segundo select de la página
|
|
||||||
const allSelects = await page.$$('select');
|
const allSelects = await page.$$('select');
|
||||||
if (allSelects.length > 1) {
|
if (allSelects.length > 1) {
|
||||||
await allSelects[1].selectOption(secondsValue).catch(() => {});
|
await allSelects[1].selectOption(secondsValue).catch(() => {});
|
||||||
|
|
@ -151,9 +143,8 @@ async function processChangeState(page, db, jobData) {
|
||||||
// 7. GUARDAR
|
// 7. GUARDAR
|
||||||
const btn = page.locator('button.form-container-button-submit');
|
const btn = page.locator('button.form-container-button-submit');
|
||||||
|
|
||||||
// Verificación final antes de clickar
|
|
||||||
if (await btn.isDisabled()) {
|
if (await btn.isDisabled()) {
|
||||||
console.log('⛔ Botón deshabilitado. Re-activando campo comentario...');
|
console.log('⛔ Botón deshabilitado. Intentando reactivar...');
|
||||||
await page.click('textarea[formcontrolname="comment"]');
|
await page.click('textarea[formcontrolname="comment"]');
|
||||||
await page.keyboard.press('Tab');
|
await page.keyboard.press('Tab');
|
||||||
await page.waitForTimeout(1000);
|
await page.waitForTimeout(1000);
|
||||||
|
|
@ -166,10 +157,26 @@ async function processChangeState(page, db, jobData) {
|
||||||
console.log('💾 Click en Guardar...');
|
console.log('💾 Click en Guardar...');
|
||||||
await btn.click();
|
await btn.click();
|
||||||
|
|
||||||
// 8. VERIFICAR RESULTADO
|
// --- 8. DETECCIÓN DE POPUP "SÍ" (NUEVO CÓDIGO) ---
|
||||||
await page.waitForTimeout(4000);
|
// Esperamos 2.5s para ver si salta la alerta roja
|
||||||
|
await page.waitForTimeout(2500);
|
||||||
|
|
||||||
|
// Buscamos el botón usando la clase específica que me diste y el texto "Sí"
|
||||||
|
const confirmBtn = page.locator('button.form-container-button-submit-toast').filter({ hasText: 'Sí' });
|
||||||
|
|
||||||
|
if (await confirmBtn.count() > 0 && await confirmBtn.isVisible()) {
|
||||||
|
console.log('🚨 ¡ALERTA DETECTADA! (Mensaje informativo de SMS).');
|
||||||
|
console.log('👉 Pulsando botón "Sí"...');
|
||||||
|
await confirmBtn.click();
|
||||||
|
await page.waitForTimeout(3000); // Esperar guardado real
|
||||||
|
} else {
|
||||||
|
console.log('ℹ️ No apareció alerta de SMS (o ya se guardó).');
|
||||||
|
}
|
||||||
|
// ----------------------------------------------------
|
||||||
|
|
||||||
|
// 9. VERIFICAR RESULTADO FINAL
|
||||||
|
await page.waitForTimeout(2000);
|
||||||
|
|
||||||
// Captura de mensajes
|
|
||||||
const message = await page.evaluate(() => {
|
const message = await page.evaluate(() => {
|
||||||
const err = document.querySelector('.form-container-error');
|
const err = document.querySelector('.form-container-error');
|
||||||
const ok = document.querySelector('.form-container-success');
|
const ok = document.querySelector('.form-container-success');
|
||||||
|
|
@ -240,7 +247,7 @@ function startWorker(db) {
|
||||||
db.collection(CONFIG.QUEUE_COLLECTION).where('status', '==', 'PENDING').onSnapshot(s => {
|
db.collection(CONFIG.QUEUE_COLLECTION).where('status', '==', 'PENDING').onSnapshot(s => {
|
||||||
s.docChanges().forEach(c => { if(c.type==='added') { queue.push(c.doc.id); run(); } });
|
s.docChanges().forEach(c => { if(c.type==='added') { queue.push(c.doc.id); run(); } });
|
||||||
});
|
});
|
||||||
console.log('🚀 Worker Multiasistencia Estados (FINAL CORREGIDO) LISTO.');
|
console.log('🚀 Worker Multiasistencia (V3 - CONFIRMACIÓN SÍ) LISTO.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = initFirebase();
|
const db = initFirebase();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue