some timetravels

This commit is contained in:
Timo Volkmann 2020-06-25 18:27:52 +02:00
parent 1cda6bfe70
commit 2b0c030980

View File

@ -18,31 +18,7 @@ module.exports = async function (data, from, to, q) {
const dates = validateDates(from, to)
// for calculating average if traveldates are in more than one month
const travelPeriods = travelPeriodsFromDates(dates)
const boundaryStatic = {
max: {
accommodation_costs: 500,
food_costs: 100,
alcohol_costs: 100,
water_costs: 100,
local_transportation_costs: 100,
entertainment_costs: 100,
average_per_day_costs: 1000,
avg_price_relative: 100
},
min: {
accommodation_costs: 0,
food_costs: 0,
alcohol_costs: 0,
water_costs: 0,
local_transportation_costs: 0,
entertainment_costs: 0,
average_per_day_costs: 0,
avg_price_relative: 0
}
}
// CALCULATE PROPERTIES FOR EACH REGION
regionsArr.forEach(reg => {
@ -129,18 +105,33 @@ function calculateAverage(scores) {
}
function travelPeriodsFromDates(dates) {
let start = moment(`${dates.from.year}-${dates.from.month}-${dates.from.day}`, 'YYYY-MM-DD')
let end = moment(`${dates.to.year}-${dates.to.month}-${dates.to.day}`, 'YYYY-MM-DD')
console.log('start:', moment(start));
console.log('end:', moment(end));
// console.log('start:', moment(start).toISOString());
// console.log('end:', moment(end).toISOString());
// console.log('start:', moment(dates.from));
// console.log('end:', moment(dates.to));
console.log();
console.log();
console.log();
let travelPeriods = []
if (dates.from.month === dates.to.month && dates.from.year === dates.to.year) {
if (start.month() === end.month() && start.year() === end.year()) {
let period = {
month: dates.from.month,
days: dates.to.day - dates.from.day
month: start.month()+1,
days: end.date() - start.date()
}
travelPeriods.push(period)
} else {
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()))
for (var m = moment(start); m.isSameOrBefore(moment(end).endOf("month")); m.add(1, 'months')) {
console.log(m);
travelPeriods.push(createPeriod(start, end, m.month()+1, m.year()))
}
}
return travelPeriods
@ -160,7 +151,7 @@ function validateDates(from, to) {
let dateTo = new Date(to)
fromAndTo.to.day = dateTo.getDate()
fromAndTo.to.month = dateTo.getMonth() + 1
fromAndTo.to.year = dateFrom.getFullYear()
fromAndTo.to.year = dateTo.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;
@ -172,24 +163,30 @@ function validateDates(from, to) {
fromAndTo.to.day = Number(to.split("-")[2])
if (!from.match(re) || !to.match(re)) throw new Error("ERR: invalid parameter:", from, to)
}
if (moment(fromAndTo.from).add(23, 'hours').isAfter(moment(fromAndTo.to))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
console.log(moment(`${fromAndTo.from.year}-${fromAndTo.from.month}-${fromAndTo.from.day}`, 'YYYY-MM-DD'))
console.log(moment(`${fromAndTo.to.year}-${fromAndTo.to.month}-${fromAndTo.to.day}`, 'YYYY-MM-DD'))
if (moment(`${fromAndTo.from.year}-${fromAndTo.from.month}-${fromAndTo.from.day}`, 'YYYY-MM-DD').add(23, 'hours').isAfter(moment(`${fromAndTo.to.year}-${fromAndTo.to.month}-${fromAndTo.to.day}`, 'YYYY-MM-DD'))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
return fromAndTo
}
function createPeriod(from, to, currentMonth, currentYear) {
function createPeriod(start, end, currentMonth, currentYear) {
let period = {}
if (currentMonth === from.month && currentYear === from.year) {
console.log(start, end, currentMonth, currentYear);
if (currentMonth === start.month() + 1 && currentYear === start.year()) {
console.log('first month')
period = {
month: currentMonth,
days: 32 - from.day
days: 32 - start.date()
}
} else if (currentMonth === to.month) {
} else if (currentMonth === end.month() + 1) {
console.log('end month')
period = {
month: currentMonth,
days: to.day
days: end.date()
}
} else {
console.log('middle month')
period = {
month: currentMonth,
days: 30