Fixes fixed bug of the search when searching over the turn of the year
This commit is contained in:
parent
dc6da042f9
commit
fa73c0a1e0
@ -102,64 +102,72 @@ module.exports = function (dbConn) {
|
||||
}
|
||||
|
||||
function travelPeriodsFromDates(dates) {
|
||||
//console.log(dates);
|
||||
|
||||
let travelPeriods = []
|
||||
if (dates.from.month === dates.to.month) {
|
||||
|
||||
if (dates.from.month === dates.to.month && dates.from.year === dates.to.year) {
|
||||
let period = {
|
||||
month: dates.from.month,
|
||||
days: dates.to.day - dates.from.day
|
||||
}
|
||||
travelPeriods.push(period)
|
||||
|
||||
} else {
|
||||
for (let i = dates.from.month; i <= dates.to.month; i++) {
|
||||
let period = {}
|
||||
if (i === dates.from.month) {
|
||||
period = {
|
||||
month: i,
|
||||
days: 32 - dates.from.day
|
||||
}
|
||||
} else if (i === dates.to.month) {
|
||||
period = {
|
||||
month: i,
|
||||
days: dates.to.day
|
||||
}
|
||||
} else {
|
||||
period = {
|
||||
month: i,
|
||||
days: 30
|
||||
}
|
||||
}
|
||||
travelPeriods.push(period)
|
||||
for (var m = moment(dates.from).subtract(1, 'months'); m.isSameOrBefore(moment(dates.to).subtract(1, 'months').endOf("month")); m.add(1, 'months')) {
|
||||
travelPeriods.push(createPeriod(dates.from, dates.to, m.month() + 1, m.year()))
|
||||
}
|
||||
}
|
||||
return travelPeriods
|
||||
}
|
||||
|
||||
function createPeriod(from, to, currentMonth, currentYear) {
|
||||
let period = {}
|
||||
if (currentMonth === from.month && currentYear === from.year) {
|
||||
period = {
|
||||
month: currentMonth,
|
||||
days: 32 - from.day
|
||||
}
|
||||
} else if (currentMonth === to.month) {
|
||||
period = {
|
||||
month: currentMonth,
|
||||
days: to.day
|
||||
}
|
||||
} else {
|
||||
period = {
|
||||
month: currentMonth,
|
||||
days: 30
|
||||
}
|
||||
}
|
||||
return period
|
||||
}
|
||||
|
||||
function validateDates(from, to) {
|
||||
let fromAndTo = {
|
||||
from: {},
|
||||
to: {}
|
||||
}
|
||||
|
||||
if (_.isNumber(from) && _.isNumber(to)) {
|
||||
let dateFrom = new Date(from)
|
||||
fromAndTo.from.day = dateFrom.getDate()
|
||||
fromAndTo.from.month = dateFrom.getMonth() + 1
|
||||
fromAndTo.from.year = dateFrom.getFullYear()
|
||||
let dateTo = new Date(to)
|
||||
fromAndTo.to.day = dateTo.getDate()
|
||||
fromAndTo.to.month = dateTo.getMonth() + 1
|
||||
if (moment(dateFrom).add(23, 'hours').isAfter(moment(dateTo))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||
fromAndTo.to.year = dateFrom.getFullYear()
|
||||
} else {
|
||||
// this block to still support old query syntax, validating from and to parameter
|
||||
let re = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/i;
|
||||
fromAndTo.from.year = Number(from.split("-")[0])
|
||||
fromAndTo.to.year = Number(to.split("-")[0])
|
||||
fromAndTo.from.month = Number(from.split("-")[1])
|
||||
fromAndTo.to.month = Number(to.split("-")[1])
|
||||
fromAndTo.from.day = Number(from.split("-")[2])
|
||||
fromAndTo.to.day = Number(to.split("-")[2])
|
||||
if (!from.match(re) || !to.match(re)) throw new Error("ERR: invalid parameter:", from, to)
|
||||
if (moment(from, 'YYYY-MM-DD').add(23, 'hours').isAfter(moment(to, 'YYYY-MM-DD'))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||
}
|
||||
|
||||
if (moment(fromAndTo.from).add(23, 'hours').isAfter(moment(fromAndTo.to))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||
return fromAndTo
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user