Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cooldown handling to my premises page #305

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createSelector } from 'reselect';
import { showReservationInfoModal } from 'actions/uiActions';
import AvailabilityTimeline from './AvailabilityTimeline';
import utils from '../utils';
import { isStaffForResource } from '../../../../../utils/resourceUtils';

export function selector() {
function dateSelector(state, props) { return props.date; }
Expand All @@ -22,6 +23,20 @@ export function selector() {
resourceIdSelector,
(resources, id) => resources[id]
);

const hasStaffRightsSelector = createSelector(
resourceSelector,
resource => isStaffForResource(resource)
);

const timeRestrictionsSelector = createSelector(
resourceSelector,
resource => {
const { minPeriod, maxPeriod, cooldown } = resource;
return { minPeriod, maxPeriod, cooldown };
}
);

const reservationsSelector = createSelector(
resourceSelector,
dateSelector,
Expand All @@ -37,8 +52,11 @@ export function selector() {
reservationsSelector,
dateSelector,
resourceIdSelector,
(reservations, date, resourceId) => utils.getTimelineItems(
moment(date), reservations, resourceId
timeRestrictionsSelector,
hasStaffRightsSelector,
(reservations, date, resourceId,
timeRestrictions, hasStaffRights) => utils.getTimelineItems(
moment(date), reservations, resourceId, timeRestrictions, hasStaffRights
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ describe('shared/availability-view/AvailabilityTimelineContainer', () => {
expect(actual[9]).toEqual({ key: '9', type: 'reservation', data: reservations[2] });
expect(actual[10].data.isSelectable).toBe(false);
});

test('contains cooldown and staff rights info', () => {
const state = getState();
const selection = {
begin: '2016-01-01T10:00:00',
end: '2016-01-01T10:30:00',
resourceId: 'resource-1',
};
const props = { id: 'resource-1', date: '2016-01-01', selection };
const actual = selector()(state, props).items;
expect(actual[0].data.hasStaffRights).toBeDefined();
expect(actual[0].data.isWithinCooldown).toBeDefined();
});
});
});
});
44 changes: 40 additions & 4 deletions app/shared/availability-view/TimelineGroups/TimelineGroup/utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@

import some from 'lodash/some';
import moment from 'moment';
import Moment from 'moment';
import { extendMoment } from 'moment-range';

import { slotSize, slotWidth, slotMargin } from 'constants/SlotConstants';

const moment = extendMoment(Moment);

function getTimeSlotWidth({ startTime, endTime } = {}) {
const diff = endTime ? endTime.diff(startTime, 'minutes') : slotSize;
const slots = Math.floor(diff / slotSize);

return (slotWidth * slots) - slotMargin;
}

function getTimelineItems(date, reservations, resourceId) {
function getTimelineItems(date, reservations, resourceId, timeRestrictions, hasStaffRights) {
const { cooldown } = timeRestrictions;
// skip getting cooldowns if user has perms
const cooldownRanges = hasStaffRights ? [] : getCooldownRanges(reservations, cooldown);
const items = [];
let reservationPointer = 0;
let timePointer = date.clone().startOf('day');
Expand All @@ -28,16 +34,24 @@ function getTimelineItems(date, reservations, resourceId) {
timePointer = moment(reservation.end);
reservationPointer += 1;
} else {
const beginMoment = timePointer.format();
const endMoment = timePointer.clone().add(slotSize, 'minutes').format();
// skip cooldown check if user has perms
const isWithinCooldown = hasStaffRights ? false
: isSlotWithinCooldown(beginMoment, endMoment, cooldownRanges);

items.push({
key: String(items.length),
type: 'reservation-slot',
data: {
begin: timePointer.format(),
end: timePointer.clone().add(slotSize, 'minutes').format(),
begin: beginMoment,
end: endMoment,
resourceId,
// isSelectable: false by default to improve selector performance by allowing
// addSelectionData to make some assumptions.
isSelectable: false,
isWithinCooldown,
hasStaffRights
},
});
timePointer.add(slotSize, 'minutes');
Expand All @@ -58,6 +72,7 @@ function markItemSelectable(item, isSelectable, openingHours, ext, after) {
isSelectable
&& moment().isSameOrBefore(item.data.end)
&& (!openingHours || isInsideOpeningHours(item, openingHours))
&& !(item.data.isWithinCooldown && !item.data.hasStaffRights)
);
const isExternalAndBeforeAfter = !ext && moment(item.data.begin).isSameOrBefore(after);
if (isExternalAndBeforeAfter) {
Expand Down Expand Up @@ -104,6 +119,27 @@ function addSelectionData(selection, resource, items) {
});
}

function getCooldownRanges(reservations, cooldown) {
if (reservations && cooldown && cooldown !== '00:00:00') {
return reservations.map(reservation => moment.range(
moment(reservation.begin).subtract(moment.duration(cooldown)),
moment(reservation.end).add(moment.duration(cooldown))
));
}
return [];
}

function isSlotWithinCooldown(begin, end, cooldownRanges) {
const slotRange = moment.range(begin, end);
for (let index = 0; index < cooldownRanges.length; index += 1) {
const cooldownRange = cooldownRanges[index];
if (cooldownRange.overlaps(slotRange)) {
return true;
}
}
return false;
}

export default {
addSelectionData,
getTimelineItems,
Expand Down
Loading
Loading