adapt first step of search request to new DB design

This commit is contained in:
Bilal Catic
2019-09-13 11:08:45 +02:00
parent 3cbd79dcc9
commit 1a874d4d88
3 changed files with 29 additions and 25 deletions

View File

@@ -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 { realEstateTypes, getRealEstateTypeEnum } = require("../helpers/enums");
const getRealEstateTypes = (req, res) => { const getRealEstateTypes = (req, res) => {
@@ -9,31 +9,35 @@ const getRealEstateTypes = (req, res) => {
}; };
const postRealEstateTypes = async (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"; const nextStepPage = req.query.nextStep || "lokacija";
if (request && request.uniqueId) { let nextStepUrl = "";
const nextStepUrl = `/${nextStepPage}/${request.uniqueId}`; if (searchRequest && searchRequest.id) {
request.realEstateType = req.body.realestatetype; nextStepUrl = `/${nextStepPage}/${searchRequest.id}`;
if (!getRealEstateTypeEnum(request.realEstateType).hasGardenSize) { searchRequest.realEstateType = selectedRealEstateType;
request.gardenSize = null; if (!getRealEstateTypeEnum(selectedRealEstateType).hasGardenSize) {
searchRequest.gardenSizeMin = null;
searchRequest.gardenSizeMax = null;
} }
await request.save(); await request.save();
res.redirect(nextStepUrl);
} else { } else {
db.RealEstateRequest.create({ try {
realEstateType: req.body.realestatetype const newSearchRequest = await createSearchRequest({
}) realEstateType: selectedRealEstateType
.then(result => {
const nextStepUrl = `/${nextStepPage}/${result.uniqueId}`;
res.redirect(nextStepUrl);
})
.catch(e => {
res.send(e);
}); });
nextStepUrl = `/${nextStepPage}/${newSearchRequest.id}`;
} catch (error) {
console.log(error);
nextStepUrl = `/`;
} }
}
res.redirect(nextStepUrl);
}; };
module.exports = { module.exports = {

View File

@@ -27,10 +27,10 @@ const { redirect } = require("../controllers/redirect");
const router = express.Router(); const router = express.Router();
router.get("/", welcome); 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.post("/vrstanekretnine", postRealEstateTypes);
router.get("/lokacija/:request_id", getLocation); router.get("/lokacija/:request_id", getLocation);

View File

@@ -14,13 +14,13 @@
</li> </li>
<% } %> <% } %>
</ul> </ul>
<input type="hidden" name="realestatetype" id="realestatetype" /> <input type="hidden" name="realEstateType" id="realEstateType" />
</div> </div>
</form> </form>
<script> <script>
function saveAndSubmit(id) { function saveAndSubmit(id) {
$("#realestatetype").val(id); $("#realEstateType").val(id);
$("#form-real-estate-type").submit(); $("#form-real-estate-type").submit();
} }
</script> </script>