Adapt query for uc-rent-real-estate-offer
@@ -11,7 +11,6 @@ import {
import { findLatestIntervallEndInJsonLdOrNowAndAddMillis } from "../../app/won-utils.js";
import {
- filterInVicinity,
filterNumericProperty,
filterPrice,
concatenateFilters,
@@ -67,6 +66,8 @@ export const rentRealEstateOffer = {
const numberOfRooms = draftContent && draftContent.numberOfRooms;
const floorSize = draftContent && draftContent.floorSize;
+ let filter;
+ if (location && location.lat && location.lng) {
const filters = [
{
// to select is-branch
@@ -76,8 +77,21 @@ export const rentRealEstateOffer = {
},
operations: [
`${resultName} a won:Need.`,
+ `${resultName} a won:RealEstateRentDemand.`,
`${resultName} won:seeks ?seeks.`,
- location && "?seeks (s:location|won:hasLocation) ?location.",
+ "?seeks (won:hasLocation|s:location) ?location.",
+ "?location s:geo ?location_geo.",
+ "?location_geo s:latitude ?location_lat;",
+ "s:longitude ?location_lon;",
+ `bind (abs(xsd:decimal(?location_lat) - ${
+ location.lat
+ }) as ?latDiffRaw)`,
+ `bind (abs(xsd:decimal(?location_lon) - ${
+ location.lng
+ }) as ?lonDiff)`,
+ "bind (if ( ?latDiffRaw > 180, 360 - ?latDiffRaw, ?latDiffRaw ) as ?latDiff)",
+ "bind ( ?latDiff * ?latDiff + ?lonDiff * ?lonDiff as ?location_geoDistanceScore)",
+ "bind (?location_geoDistanceScore as ?distScore)",
],
},
rent && filterPrice("?seeks", rent.amount, rent.currency, "rent"),
@@ -90,22 +104,50 @@ export const rentRealEstateOffer = {
"s:numberOfRooms",
"rooms"
),
- filterInVicinity("?location", location),
];
- const concatenatedFilter = concatenateFilters(filters);
+ filter = concatenateFilters(filters);
+ } else {
+ const filters = [
+ {
+ // to select is-branch
+ prefixes: {
+ won: won.defaultContext["won"],
+ sh: won.defaultContext["sh"], //needed for the filterNumericProperty calls
+ },
+ operations: [
+ `${resultName} a won:Need.`,
+ `${resultName} a won:RealEstateRentDemand.``${resultName} won:seeks ?seeks.`,
+ ],
+ },
+ rent && filterPrice("?seeks", rent.amount, rent.currency, "rent"),
+ floorSize &&
+ filterNumericProperty("?seeks", floorSize, "s:floorSize", "size"),
+ numberOfRooms &&
+ filterNumericProperty(
+ "?seeks",
+ numberOfRooms,
+ "s:numberOfRooms",
+ "rooms"
+ ),
+ ];
+
+ filter = concatenateFilters(filters);
+ }
- return sparqlQuery({
- prefixes: concatenatedFilter.prefixes,
+ const generatedQuery = sparqlQuery({
+ prefixes: filter.prefixes,
distinct: true,
variables: [resultName],
- where: concatenatedFilter.operations,
+ where: filter.operations,
orderBy: [
{
order: "ASC",
- variable: "?location_geoDistance",
+ variable: "?distScore",
},
],
});
+
+ return generatedQuery;
},
};
