-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.js
More file actions
129 lines (116 loc) · 3.56 KB
/
query.js
File metadata and controls
129 lines (116 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const MongoClient = require("mongodb").MongoClient,
url = "mongodb://localhost:27017/";
if (!process.argv[2] || !process.argv[3] || !process.argv[4]) {
throw new Error("[dataOd] [dataDo] [liczbaDni]");
}
const dateFrom = new Date(process.argv[2]);
const dateTo = new Date(process.argv[3]);
const daysToSearch = Number(process.argv[4]);
if (Math.ceil((dateTo - dateFrom) / 86400400) < daysToSearch - 1) {
throw new Error(
"Liczba dni wyszukiwanych jest mniejsza niż różnica pomiędzy jedną a drugą dataą"
);
}
MongoClient.connect(url, { useUnifiedTopology: true }, async (err, db) => {
if (err) throw err;
const dbo = db.db("database");
dbo
.collection("offers")
.find({})
.toArray(function (err, offers) {
const selectedOffers = [];
offers.forEach((offer) => {
if (offer.readyFrom > dateTo || offer.readyTo < dateFrom) {
return;
}
const days = [];
for (
var d = new Date(offer.readyFrom);
d <= new Date(offer.readyTo);
d.setDate(d.getDate() + 1)
) {
days.push(new Date(d));
}
offer.reservations.sort((a, b) => a.from - b.from);
offer.reservations.forEach((reservation) => {
days.forEach((day, index) => {
if (day >= reservation.from && day <= reservation.to) {
days[index] = null;
}
});
});
days.forEach((day, index) => {
if (day < dateFrom || day > dateTo) {
days[index] = null;
}
});
let count = 1;
let startIndex = 0;
while (days[startIndex] === null && days[startIndex] !== undefined) {
startIndex++;
}
loop: for (let h = startIndex + 1; h < days.length; h++) {
if (count === daysToSearch) {
const to = new Date(days[startIndex]);
to.setDate(to.getDate() + daysToSearch - 1);
selectedOffers.push({
id: offer.id,
idObject: offer.idObject,
from: days[startIndex],
to: to,
});
count = 1;
startIndex++;
while (
days[startIndex] === null &&
days[startIndex] !== undefined
) {
startIndex++;
}
h = startIndex;
if (days[startIndex] === undefined) {
break loop;
}
} else if (days[h] === null) {
count = 0;
startIndex = h;
while (
days[startIndex] === null &&
days[startIndex] !== undefined
) {
startIndex++;
}
} else if (days[h] !== null) {
count++;
}
}
if (count === daysToSearch) {
const to = new Date(days[startIndex]);
to.setDate(to.getDate() + daysToSearch - 1);
selectedOffers.push({
id: offer.id,
idObject: offer.idObject,
from: days[startIndex],
to,
});
}
})
selectedOffers.forEach((offer) => {
console.log(
`${offer.idObject} ${offer.id} ${dateToYYYYMMDD(
offer.from
)} ${dateToYYYYMMDD(offer.to)}`
);
});
db.close();
});
});
function dateToYYYYMMDD(date) {
var d = new Date(date),
month = "" + (d.getMonth() + 1),
day = "" + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = "0" + month;
if (day.length < 2) day = "0" + day;
return [year, month, day].join("-");
}