Skip to content

Commit

Permalink
Added cooldown handling to my premises page (#305)
Browse files Browse the repository at this point in the history
When user does not have unit permissions i.e. when viewing external resources, reservation cooldowns are shown as unselectable slots in my premises page.
  • Loading branch information
SanttuA authored Feb 20, 2024
1 parent a8af657 commit 935d6c0
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 10 deletions.
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

0 comments on commit 935d6c0

Please sign in to comment.