-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/addBookedDates
- Loading branch information
Showing
6 changed files
with
188 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,67 @@ | ||
const express = require('express'); | ||
const places = require("../src/data/places.json"); | ||
const constants = require('../src/constants/constants'); | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/:destination/homes", (req, res) => { | ||
const {destination} = req.params; | ||
const {checkIn, checkOut, guests = 1} = req.query; | ||
router.get("/:region/homes", (req, res) => { | ||
const {region} = req.params; | ||
const {guests = 1} = req.query; | ||
const DEFAULT_NO_OF_DAYS_PER_STAY = 1; | ||
|
||
// checkIn and checkOut values are passed as strings. If the given string is not valid it will return 'Invalid Date' | ||
const checkIn = new Date(req?.query?.checkIn); | ||
let checkOut = new Date(req?.query?.checkOut); | ||
const filteredResult = []; | ||
|
||
const filteredDestinations = destination ? places.filter(place => place.destination === destination && place.guests >= guests) : places; | ||
if (filteredDestinations.length) | ||
let filteredDestinations; | ||
if (!region) { | ||
filteredDestinations = places; | ||
} | ||
else { | ||
filteredDestinations = places.filter(place => place?.productSummary?.address?.toLowerCase().includes(region.toLowerCase()) && place?.productSummary?.guests?.value >= guests); | ||
} | ||
|
||
// If checkOut date is not given. By default checkOut date is taken as the immediate next day. | ||
// getDate() Returns NaN if the date is invalid. | ||
if (checkIn && !isNaN(checkIn.getDate()) && (!checkOut || isNaN(checkOut.getDate()))) | ||
{ | ||
checkOut = new Date(checkIn); | ||
checkOut.setDate(checkIn.getDate() + DEFAULT_NO_OF_DAYS_PER_STAY); | ||
} | ||
|
||
if (filteredDestinations.length && checkIn && checkOut) | ||
{ | ||
filteredDestinations.forEach(destination => { | ||
let isPlaceNotAvailable = destination.reservations.some((reservation)=> { | ||
const [reservationCheckIn, reservationCheckOut] = reservation.dates; | ||
|
||
// In a JSON file dates are saved as strings. Hence to perform date operations, they are converted to dates. | ||
const reservationCheckIn = new Date(reservation.dates[0]); | ||
const reservationCheckOut = new Date(reservation.dates[1]); | ||
|
||
if (isNaN(reservationCheckIn.getDate()) || isNaN(reservationCheckOut.getDate())) | ||
{ | ||
console.error(constants.ERROR_IN_RESERVATION_DETAILS); | ||
return (false); | ||
} | ||
|
||
// checkIn/ checkOut cannot be in between another reservation stay. | ||
// Two checkOuts cannot be on the same date. | ||
// Two checkIns cannot be on the same date. | ||
return ((reservationCheckIn <= checkIn && reservationCheckOut > checkIn) || | ||
(reservationCheckIn < checkOut && reservationCheckOut >= checkOut) || | ||
reservationCheckIn === checkIn || | ||
reservationCheckOut === checkOut) | ||
}) | ||
}); | ||
if (isPlaceNotAvailable == false) | ||
filteredResult.push(destination); | ||
}) | ||
res.json(filteredResult); | ||
} | ||
else { | ||
res.json(filteredDestinations); | ||
} | ||
res.json(filteredResult); | ||
|
||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = Object.freeze({ | ||
ERROR_IN_RESERVATION_DETAILS: "Error in reservation checkin/checkout date." | ||
}); | ||
|
Oops, something went wrong.