Actualizar worker-homeserve.js
This commit is contained in:
@@ -117,7 +117,7 @@ async function loginAndProcess(page, creds, jobData) {
|
||||
await page.waitForTimeout(3000);
|
||||
|
||||
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}...`);
|
||||
|
||||
@@ -127,46 +127,60 @@ async function loginAndProcess(page, creds, jobData) {
|
||||
|
||||
const changeBtn = await clickFirstThatExists(page, ['input[name="repaso"]']);
|
||||
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? DEBUG: ${debug}`);
|
||||
throw new Error(`No veo el botón 'repaso'. ¿El siniestro ${jobData.service_number} existe y está abierto?`);
|
||||
}
|
||||
|
||||
console.log('>>> 3. Accediendo al formulario. Rellenando datos...');
|
||||
await page.waitForLoadState('domcontentloaded');
|
||||
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 selects = document.querySelectorAll('select');
|
||||
for (const s of selects) {
|
||||
for (const opt of s.options) {
|
||||
if (opt.value == code || opt.text.includes(code)) {
|
||||
s.value = opt.value;
|
||||
const select = document.querySelector('select[name="ESTADO"]');
|
||||
if (!select) return false;
|
||||
for (const opt of select.options) {
|
||||
// Busca el código exacto 307
|
||||
if (opt.value == code || opt.text.toUpperCase().includes(code.toUpperCase())) {
|
||||
select.value = opt.value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
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);
|
||||
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) {
|
||||
const labels = ['informado al cliente', 'informado al Cliente'];
|
||||
for (const txt of labels) {
|
||||
const hit = await findLocatorInFrames(page, `label:has-text("${txt}") >> input[type="checkbox"]`);
|
||||
if (hit && !(await hit.locator.first().isChecked())) await hit.locator.first().check();
|
||||
const informCheck = await findLocatorInFrames(page, 'input[name="INFORMO"]');
|
||||
if (informCheck && !(await informCheck.locator.first().isChecked())) {
|
||||
await informCheck.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.');
|
||||
|
||||
console.log('>>> 4. Guardando cambios en HomeServe...');
|
||||
|
||||
Reference in New Issue
Block a user