Actualizar worker-homeserve.js

This commit is contained in:
2026-03-06 08:07:57 +00:00
parent 0bbebea010
commit d646a032b2

View File

@@ -117,7 +117,7 @@ async function loginAndProcess(page, creds, jobData) {
await page.waitForTimeout(3000); await page.waitForTimeout(3000);
const loginFail = await findLocatorInFrames(page, 'input[type="password"]'); const loginFail = await findLocatorInFrames(page, 'input[type="password"]');
if (loginFail) throw new Error(`Login fallido en HomeServe para el usuario ${creds.user}. Revise las credenciales en IntegraRepara.`); if (loginFail) throw new Error(`Login fallido en HomeServe para el usuario ${creds.user}. Revise las credenciales.`);
console.log(`>>> 2. Login OK. Navegando al expediente ${jobData.service_number}...`); console.log(`>>> 2. Login OK. Navegando al expediente ${jobData.service_number}...`);
@@ -127,46 +127,60 @@ async function loginAndProcess(page, creds, jobData) {
const changeBtn = await clickFirstThatExists(page, ['input[name="repaso"]']); const changeBtn = await clickFirstThatExists(page, ['input[name="repaso"]']);
if (!changeBtn) { if (!changeBtn) {
const debug = await getDebugInfo(page); throw new Error(`No veo el botón 'repaso'. ¿El siniestro ${jobData.service_number} existe y está abierto?`);
throw new Error(`No veo el botón 'repaso'. ¿El siniestro ${jobData.service_number} existe y está abierto? DEBUG: ${debug}`);
} }
console.log('>>> 3. Accediendo al formulario. Rellenando datos...'); console.log('>>> 3. Accediendo al formulario. Rellenando datos...');
await page.waitForLoadState('domcontentloaded'); await page.waitForLoadState('domcontentloaded');
await page.waitForTimeout(1000); await page.waitForTimeout(1000);
// 🔴 MAGIA: DICCIONARIO TRADUCTOR DE ESTADOS
const HOMESERVE_MAP = {
'CITADO': '307', // En espera de Prof. por fecha de inicio de trabajos
'ESPERA': '303', // En espera de Cliente por aceptacion Presupuesto
'TERMINADO': '345', // En espera de Prof. en realizacion pendiente Terminar
'ANULADO': '352' // Provisional: En espera de Perjudicado por indicaciones
};
// Traduce "CITADO" a "307" antes de buscarlo
let targetCode = jobData.new_status.toUpperCase();
if (HOMESERVE_MAP[targetCode]) {
targetCode = HOMESERVE_MAP[targetCode];
}
const statusOk = await page.evaluate((code) => { const statusOk = await page.evaluate((code) => {
const selects = document.querySelectorAll('select'); const select = document.querySelector('select[name="ESTADO"]');
for (const s of selects) { if (!select) return false;
for (const opt of s.options) { for (const opt of select.options) {
if (opt.value == code || opt.text.includes(code)) { // Busca el código exacto 307
s.value = opt.value; if (opt.value == code || opt.text.toUpperCase().includes(code.toUpperCase())) {
select.value = opt.value;
return true; return true;
} }
} }
}
return false; return false;
}, jobData.new_status); }, targetCode);
if (!statusOk) throw new Error(`No encontré el estado '${jobData.new_status}' en los desplegables de HomeServe.`); if (!statusOk) throw new Error(`No encontré el estado '${jobData.new_status}' (Buscando código interno: ${targetCode}) en el desplegable de HomeServe.`);
if (jobData.appointment_date) { if (jobData.appointment_date) {
const dateFilled = await fillFirstThatExists(page, ['input[name="FECSIG"]'], jobData.appointment_date); const dateFilled = await fillFirstThatExists(page, ['input[name="FECSIG"]'], jobData.appointment_date);
if (!dateFilled) console.warn('⚠️ No encontré el recuadro para la fecha, pero continuaré.'); if (!dateFilled) console.warn('⚠️ No encontré el recuadro para la fecha.');
} }
const obsFilled = await fillFirstThatExists(page, ['textarea[name="Observaciones"]'], jobData.observation); const obsFilled = await fillFirstThatExists(page, ['textarea[name="Observaciones"]'], jobData.observation);
if (!obsFilled) throw new Error('No encontré el recuadro de Observaciones en la web de HomeServe.'); if (!obsFilled) throw new Error('No encontré el recuadro de Observaciones en la web de HomeServe.');
// 🔴 NUEVO LOCALIZADOR PARA EL CHECKBOX (Sacado de tu HTML)
if (jobData.inform_client) { if (jobData.inform_client) {
const labels = ['informado al cliente', 'informado al Cliente']; const informCheck = await findLocatorInFrames(page, 'input[name="INFORMO"]');
for (const txt of labels) { if (informCheck && !(await informCheck.locator.first().isChecked())) {
const hit = await findLocatorInFrames(page, `label:has-text("${txt}") >> input[type="checkbox"]`); await informCheck.locator.first().check();
if (hit && !(await hit.locator.first().isChecked())) await hit.locator.first().check();
} }
} }
const saveBtn = await clickFirstThatExists(page, ['input[type="submit"]', 'input[value="Guardar"]', 'button:has-text("Guardar")']); // 🔴 NUEVO LOCALIZADOR PARA EL BOTÓN GUARDAR (Sacado de tu HTML)
const saveBtn = await clickFirstThatExists(page, ['input[name="BTNCAMBIAESTADO"]']);
if (!saveBtn) throw new Error('No encuentro el botón para guardar los cambios en HomeServe.'); if (!saveBtn) throw new Error('No encuentro el botón para guardar los cambios en HomeServe.');
console.log('>>> 4. Guardando cambios en HomeServe...'); console.log('>>> 4. Guardando cambios en HomeServe...');