hometify/bak/completePropertyData1.js

58 lines
2.1 KiB
JavaScript

// utils/completePropertyData.js
import Fuse from 'fuse.js';
export const completePropertyData = (uniqueProperties, allProperties) => {
// Creamos una copia de allProperties con sólo los dataValues
const allDataValues = allProperties.map(prop => prop.dataValues);
const options = {
keys: ['title'], // Buscamos similitud en el título
threshold: 0.6, // Umbral de similitud
includeScore: true // Incluye la puntuación de similitud
};
const fuse = new Fuse(allDataValues, options);
const enrichedProperties = uniqueProperties.map(property => {
// Buscamos propiedades similares por título para completar datos
const result = fuse.search(property.title).filter((res) => {
// Filtramos resultados que no sean la misma propiedad y sean similares en título
return res.item.id !== property.id && res.score < options.threshold;
});
// Combinamos las propiedades similares para completar datos
let enrichedProperty = { ...property };
result.forEach((res) => {
const item = res.item;
enrichedProperty = {
...enrichedProperty,
title: getLongest(enrichedProperty.title, item.title),
url: enrichedProperty.url || item.url,
price: enrichedProperty.price || item.price,
rooms: enrichedProperty.rooms || item.rooms,
area: enrichedProperty.area || item.area,
level: enrichedProperty.level || item.level,
description: getLongest(enrichedProperty.description, item.description),
pic: enrichedProperty.pic || item.pic,
baths: enrichedProperty.baths || item.baths,
neighborhood: enrichedProperty.neighborhood || item.neighborhood,
phone: enrichedProperty.phone || item.phone,
createdAt: enrichedProperty.createdAt || item.createdAt,
updatedAt: enrichedProperty.updatedAt || item.updatedAt
};
});
return enrichedProperty;
});
return enrichedProperties;
};
// Función auxiliar para obtener el texto más largo o el que no sea nulo
const getLongest = (a, b) => {
if (!a) return b;
if (!b) return a;
return a.length >= b.length ? a : b;
};