diff --git a/.gitignore b/.gitignore index b24fc61..d0441a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules/ .env .idea/ +.eslintrc +.vscode/ \ No newline at end of file diff --git a/app/helpers/db/realEstate.js b/app/helpers/db/realEstate.js index 9dd4dfe..c7709db 100644 --- a/app/helpers/db/realEstate.js +++ b/app/helpers/db/realEstate.js @@ -2,7 +2,6 @@ const db = require("../../models/index"); const sequelize = require("sequelize"); const Op = sequelize.Op; - const bulkUpsertRealEstates = async realEstateData => { try { const fieldsToUpdateIfDuplicate = [ @@ -131,42 +130,36 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { //General queries contain only attributes that are defined for every searchreq - //Query for case of complete ads + //Query for case of complete ads const query = { - adType, - realEstateType, - price: { - [Op.lte]: priceMax, - [Op.gte]: priceMin - }, - area: { - [Op.lte]: sizeMax, - [Op.gte]: sizeMin - }, - accessRoadType: { - [Op.or]: { - [Op.eq]: 'ANY', - [Op.eq]: accessRoadType - } - }, - [Op.and]: geoSearchQueryPart - } - + adType, + realEstateType, + price: { + [Op.lte]: priceMax, + [Op.gte]: priceMin + }, + area: { + [Op.lte]: sizeMax, + [Op.gte]: sizeMin + }, + [Op.and]: geoSearchQueryPart + }; + //Query for case of incomplete ads const queryIncludeIncomplete = { adType, realEstateType, price: { - [Op.or] : { - [Op.and] : { + [Op.or]: { + [Op.and]: { [Op.lte]: priceMax, [Op.gte]: priceMin }, - [Op.is] : null + [Op.is]: null } }, area: { - [Op.or] : { + [Op.or]: { [Op.and]: { [Op.lte]: sizeMax, [Op.gte]: sizeMin @@ -174,133 +167,129 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { [Op.is]: null } }, - accessRoadType: { - [Op.or]: { - [Op.eq]: 'ANY', - [Op.eq]: accessRoadType, - [Op.is]: null - } - }, [Op.and]: geoSearchQueryPart - } + }; //Every other attribute is checked separately and included in query only if it is defined if (gardenSizeMax && gardenSizeMin) { query.gardenSize = { [Op.lte]: gardenSizeMax, [Op.gte]: gardenSizeMin - } + }; queryIncludeIncomplete.gardenSize = { - [Op.or] : { + [Op.or]: { [Op.and]: { [Op.lte]: gardenSizeMax, [Op.gte]: gardenSizeMin }, [Op.is]: null } - } + }; } if (numberOfRoomsMin && numberOfRoomsMax) { query.numberOfRooms = { [Op.lte]: numberOfRoomsMax, [Op.gte]: numberOfRoomsMin - } + }; queryIncludeIncomplete.numberOfRooms = { - [Op.or] : { + [Op.or]: { [Op.and]: { [Op.lte]: numberOfRoomsMax, [Op.gte]: numberOfRoomsMin }, [Op.is]: null } - } + }; } - + if (numberOfFloorsMin && numberOfFloorsMax) { query.numberOfFloors = { [Op.lte]: numberOfFloorsMax, [Op.gte]: numberOfFloorsMin - } + }; queryIncludeIncomplete.numberOfFloors = { - [Op.or] : { + [Op.or]: { [Op.and]: { [Op.lte]: numberOfFloorsMax, [Op.gte]: numberOfFloorsMin }, [Op.is]: null } - } + }; } if (floorMin && floorMax) { query.floor = { [Op.lte]: floorMax, [Op.gte]: floorMin - } + }; queryIncludeIncomplete.floor = { - [Op.or] : { + [Op.or]: { [Op.and]: { [Op.lte]: floorMax, [Op.gte]: floorMin }, [Op.is]: null } - } + }; } if (balcony) { query.balcony = { [Op.eq]: balcony - } + }; queryIncludeIncomplete.balcony = { [Op.or]: { [Op.eq]: balcony, [Op.is]: null } - } + }; } - + if (newBuilding) { query.newBuilding = { [Op.eq]: newBuilding - } + }; queryIncludeIncomplete.newBuilding = { [Op.or]: { [Op.eq]: newBuilding, [Op.is]: null } - } + }; } - + if (elevator) { query.elevator = { [Op.eq]: elevator - } + }; queryIncludeIncomplete.elevator = { [Op.or]: { [Op.eq]: elevator, [Op.is]: null } - } + }; + } + + if (accessRoadType !== "ANY") { + query.accessRoadType = { + [Op.eq]: accessRoadType + }; + queryIncludeIncomplete.accessRoadType = { + [Op.or]: { + [Op.eq]: accessRoadType, + [Op.is]: null + } + }; } const order = [["updatedAt", "desc"]]; - if(!includeIncompleteAds) { - return await db.RealEstate.findAll({ - where: query, - limit: maxResults, - order - }); - } else { - return await db.RealEstate.findAll({ - where: queryIncludeIncomplete, - limit: maxResults, - order - }); - } - + return db.RealEstate.findAll({ + where: includeIncompleteAds ? queryIncludeIncomplete : query, + limit: maxResults, + order + }); }; module.exports = { diff --git a/app/helpers/db/searchRequest.js b/app/helpers/db/searchRequest.js index 54faeb3..bbb7cc0 100644 --- a/app/helpers/db/searchRequest.js +++ b/app/helpers/db/searchRequest.js @@ -5,11 +5,9 @@ const Op = sequelize.Op; const getSearchRequest = async searchRequestId => { try { - return await db.SearchRequest.findByPk(searchRequestId); - } catch (error) { - console.log("searchrequest.js",error); + console.log("searchrequest.js", error); return null; } }; @@ -116,30 +114,30 @@ const findSearchRequestsForRealEstate = async realEstate => { if (accessRoadType) { query.accessRoadType = { [Op.or]: { - [Op.eq]: 'ANY', + [Op.eq]: "ANY", [Op.eq]: accessRoadType } - } + }; } if (balcony) { query.balcony = { [Op.eq]: balcony - } + }; } if (newBuilding) { query.newBuilding = { [Op.eq]: newBuilding - } + }; } if (elevator) { query.elevator = { [Op.eq]: elevator - } + }; } - + return await db.SearchRequest.findAll({ where: query }); }; diff --git a/package-lock.json b/package-lock.json index b18ae09..9661459 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1052,6 +1052,14 @@ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, + "eslint-plugin-prettier": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-3.1.2.tgz", + "integrity": "sha512-GlolCC9y3XZfv3RQfwGew7NnuFDKsfI4lbvRK+PIIo23SFH+LemGs4cKwzAaRa+Mdb+lQO/STaIayno8T5sJJA==", + "requires": { + "prettier-linter-helpers": "^1.0.0" + } + }, "etag": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", @@ -1271,6 +1279,11 @@ "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" }, + "fast-diff": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.2.0.tgz", + "integrity": "sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==" + }, "fast-json-stable-stringify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", @@ -3179,6 +3192,19 @@ "integrity": "sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw=", "dev": true }, + "prettier": { + "version": "1.19.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz", + "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==" + }, + "prettier-linter-helpers": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz", + "integrity": "sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==", + "requires": { + "fast-diff": "^1.1.2" + } + }, "process-nextick-args": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", diff --git a/package.json b/package.json index 7b5441f..75a7cc4 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "compression": "^1.7.4", "dotenv": "^7.0.0", "ejs": "^2.6.1", + "eslint-plugin-prettier": "^3.1.2", "express": "^4.16.4", "express-ejs-layouts": "^2.5.0", "express-layout": "^0.1.0", @@ -44,6 +45,7 @@ "node-fetch": "^2.3.0", "node-schedule": "^1.3.2", "pg": "^7.10.0", + "prettier": "^1.19.1", "react-step-wizard": "^5.1.0", "sequelize": "^5.18.4", "sequelize-cli": "^5.5.0"