adapt first step of search request to new DB design
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const db = require("../models/index");
|
||||
const { currentSearchRequest } = require("../helpers/url");
|
||||
const { createSearchRequest } = require("../helpers/db/searchRequest");
|
||||
|
||||
const { currentRERequest } = require("../helpers/url");
|
||||
const { realEstateTypes, getRealEstateTypeEnum } = require("../helpers/enums");
|
||||
|
||||
const getRealEstateTypes = (req, res) => {
|
||||
@@ -9,31 +9,35 @@ const getRealEstateTypes = (req, res) => {
|
||||
};
|
||||
|
||||
const postRealEstateTypes = async (req, res) => {
|
||||
const request = await currentRERequest(req);
|
||||
const searchRequest = await currentSearchRequest(req);
|
||||
|
||||
//TODO: check if selected real estate type is valid
|
||||
const selectedRealEstateType = req.body.realEstateType || null;
|
||||
|
||||
const nextStepPage = req.query.nextStep || "lokacija";
|
||||
|
||||
if (request && request.uniqueId) {
|
||||
const nextStepUrl = `/${nextStepPage}/${request.uniqueId}`;
|
||||
request.realEstateType = req.body.realestatetype;
|
||||
if (!getRealEstateTypeEnum(request.realEstateType).hasGardenSize) {
|
||||
request.gardenSize = null;
|
||||
let nextStepUrl = "";
|
||||
if (searchRequest && searchRequest.id) {
|
||||
nextStepUrl = `/${nextStepPage}/${searchRequest.id}`;
|
||||
searchRequest.realEstateType = selectedRealEstateType;
|
||||
if (!getRealEstateTypeEnum(selectedRealEstateType).hasGardenSize) {
|
||||
searchRequest.gardenSizeMin = null;
|
||||
searchRequest.gardenSizeMax = null;
|
||||
}
|
||||
await request.save();
|
||||
|
||||
res.redirect(nextStepUrl);
|
||||
} else {
|
||||
db.RealEstateRequest.create({
|
||||
realEstateType: req.body.realestatetype
|
||||
})
|
||||
.then(result => {
|
||||
const nextStepUrl = `/${nextStepPage}/${result.uniqueId}`;
|
||||
res.redirect(nextStepUrl);
|
||||
})
|
||||
.catch(e => {
|
||||
res.send(e);
|
||||
try {
|
||||
const newSearchRequest = await createSearchRequest({
|
||||
realEstateType: selectedRealEstateType
|
||||
});
|
||||
|
||||
nextStepUrl = `/${nextStepPage}/${newSearchRequest.id}`;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
nextStepUrl = `/`;
|
||||
}
|
||||
}
|
||||
res.redirect(nextStepUrl);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -27,10 +27,10 @@ const { redirect } = require("../controllers/redirect");
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", welcome);
|
||||
router.get("/vrstanekretnine/:request_id", getRealEstateTypes);
|
||||
router.get("/vrstanekretnine", getRealEstateTypes);
|
||||
|
||||
router.post("/vrstanekretnine/:request_id", postRealEstateTypes);
|
||||
router.get("/vrstanekretnine/:searchRequestId", getRealEstateTypes);
|
||||
router.get("/vrstanekretnine", getRealEstateTypes);
|
||||
router.post("/vrstanekretnine/:searchRequestId", postRealEstateTypes);
|
||||
router.post("/vrstanekretnine", postRealEstateTypes);
|
||||
|
||||
router.get("/lokacija/:request_id", getLocation);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<!--suppress HtmlUnknownAnchorTarget -->
|
||||
<% include partials/navBar %>
|
||||
<% include partials/navBar %>
|
||||
|
||||
<form method="POST" id="form-real-estate-type">
|
||||
<div class="row center-align">
|
||||
@@ -14,13 +14,13 @@
|
||||
</li>
|
||||
<% } %>
|
||||
</ul>
|
||||
<input type="hidden" name="realestatetype" id="realestatetype" />
|
||||
<input type="hidden" name="realEstateType" id="realEstateType" />
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
function saveAndSubmit(id) {
|
||||
$("#realestatetype").val(id);
|
||||
$("#realEstateType").val(id);
|
||||
$("#form-real-estate-type").submit();
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user