Skip to content

Commit

Permalink
Fix to overnight calendar continous reservation check (#330)
Browse files Browse the repository at this point in the history
Previously when overnight start and end time were the same time, continous reservation check would falsely detect next and previous day selections to break continous selection. This change fixes the issue.
  • Loading branch information
SanttuA authored May 30, 2024
1 parent 7069883 commit a9799cd
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/shared/overnight-calendar/overnightUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ export function isSelectionContinous({

dates[0] = setDatesTime(dates[0], overnightStartTime).toDate();
dates[dates.length - 1] = setDatesTime(dates[dates.length - 1], overnightEndTime).toDate();
if (overnightStartTime === overnightEndTime) {
dates[0].setMinutes(dates[0].getMinutes() + 1);
dates[dates.length - 1].setMinutes(dates[dates.length - 1].getMinutes() - 1);
}

for (let index = 0; index < dates.length; index += 1) {
const date = dates[index];
Expand All @@ -573,6 +577,8 @@ export function isSelectionContinous({
export function createDateArray(startDate, endDate) {
const start = new Date(startDate);
const end = new Date(endDate);
start.setHours(0, 0, 0, 0);
end.setHours(0, 0, 0, 0);
const dateArray = [];

while (start <= end) {
Expand Down
112 changes: 112 additions & 0 deletions app/shared/overnight-calendar/tests/overnightUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,24 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
end: '2024-04-29T09:00:00+03:00'
})
];
const reservationsB = [
Reservation.build({
begin: '2024-04-27T13:00:00+03:00',
end: '2024-04-29T13:00:00+03:00'
})
];
const reservations2 = [
Reservation.build({
begin: '2024-04-27T13:00:00+03:00',
end: '2024-04-28T09:00:00+03:00'
})
];
const reservations2B = [
Reservation.build({
begin: '2024-04-27T13:00:00+03:00',
end: '2024-04-28T13:00:00+03:00'
})
];
const openingHours = [
{ date: '2024-04-19', closes: null, opens: null },
{ date: '2024-04-20', closes: '2024-04-20T20:00:00+03:00', opens: '2024-04-20T06:00:00+03:00' },
Expand All @@ -807,6 +819,7 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
];
const overnightStartTime = '13:00:00';
const overnightEndTime = '09:00:00';
const overnightEndTime2 = '13:00:00';

test('returns true when no reservations or closed days in selection', () => {
const startDate = moment('2024-04-23').toDate();
Expand All @@ -823,6 +836,15 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
startDate, endDate, reservations, openingHours, overnightStartTime, overnightEndTime
}))
.toBe(true);
expect(isSelectionContinous({
startDate,
endDate,
reservations: reservationsB,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(true);
expect(isSelectionContinous({
startDate: startDate2,
endDate: endDate2,
Expand All @@ -832,6 +854,15 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
overnightEndTime
}))
.toBe(true);
expect(isSelectionContinous({
startDate: startDate2,
endDate: endDate2,
reservations: reservations2B,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(true);
expect(isSelectionContinous({
startDate: startDate3,
endDate: endDate3,
Expand All @@ -841,6 +872,15 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
overnightEndTime
}))
.toBe(true);
expect(isSelectionContinous({
startDate: startDate3,
endDate: endDate3,
reservations: reservations2B,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(true);
});
test('returns false when reservations or closed days in selection', () => {
const startDate1 = moment('2024-04-23').toDate();
Expand All @@ -862,6 +902,15 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
overnightEndTime
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate1,
endDate: endDate1,
reservations: reservationsB,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate1,
endDate: endDate1,
Expand All @@ -871,6 +920,15 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
overnightEndTime
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate1,
endDate: endDate1,
reservations: reservations2B,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate2,
endDate: endDate2,
Expand All @@ -880,6 +938,15 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
overnightEndTime
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate2,
endDate: endDate2,
reservations: reservationsB,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate2,
endDate: endDate2,
Expand All @@ -889,6 +956,15 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
overnightEndTime
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate2,
endDate: endDate2,
reservations: reservations2B,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate3,
endDate: endDate3,
Expand All @@ -898,6 +974,15 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
overnightEndTime
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate3,
endDate: endDate3,
reservations: reservationsB,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate4,
endDate: endDate4,
Expand All @@ -907,9 +992,36 @@ describe('app/shared/overnight-calendar/overnightUtils', () => {
overnightEndTime
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate4,
endDate: endDate4,
reservations: reservations2B,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate5,
endDate: endDate5,
reservations: reservations2,
openingHours,
overnightStartTime,
overnightEndTime
}))
.toBe(false);
expect(isSelectionContinous({
startDate: startDate5,
endDate: endDate5,
reservations: reservations2B,
openingHours,
overnightStartTime,
overnightEndTime: overnightEndTime2
}))
.toBe(false);
expect(isSelectionContinous({
startDate: moment('2024-04-26').hours(13).toDate(),
endDate: moment('2024-04-28').toDate(),
reservations: reservations2,
openingHours,
overnightStartTime,
Expand Down

0 comments on commit a9799cd

Please sign in to comment.