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

feat: Add validThroughBeforeStartDate #604

Merged
merged 7 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -112,7 +112,7 @@ export function mustNotBeOpenBookingInAdvanceUnavailable(offer: import("../types
/**
* @type {OfferConstraint}
*/
export function mustHaveBeInsideValidFromBeforeStartDateWindow(offer: import("../types/Offer").Offer, opportunity: import("../types/Opportunity").Opportunity, options: import("../types/Options").Options): boolean;
export function mustBeInsideBookingWindowIfOneExists(offer: import("../types/Offer").Offer, opportunity: import("../types/Opportunity").Opportunity, options: import("../types/Options").Options): boolean;
/**
* For a session, get `organizer`. For a facility, get `provider`.
* These can be used interchangeably as `organizer` is either a Person or an Organization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TestDataShape } from './TestDataShape';

export type OpportunityConstraint = (opportunity: Opportunity, options?: Options) => boolean;

export type OfferConstraint = (offer: Offer, opportunity: Opportunity, options?: Options) => boolean;
export type OfferConstraint = (offer: Offer, opportunity?: Opportunity, options?: Options) => boolean;
nickevansuk marked this conversation as resolved.
Show resolved Hide resolved

export type TestDataShapeFactory = (options: Options) => TestDataShape;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
createCriteria,
remainingCapacityMustBeAtLeastTwo,
mustNotBeOpenBookingInAdvanceUnavailable,
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
sellerMustAllowOpenBooking,
} = require('./criteriaUtils');
const {
Expand Down Expand Up @@ -32,7 +32,7 @@ const TestOpportunityBookable = createCriteria({
],
[
'Must be within validFromBeforeStartDate window',
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
],
],
testDataShape: (options) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
mustRequireAdditionalDetails,
remainingCapacityMustBeAtLeastTwo,
mustNotBeOpenBookingInAdvanceUnavailable,
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
mustNotRequireAttendeeDetails,
sellerMustAllowOpenBooking,
} = require('./criteriaUtils');
Expand All @@ -32,7 +32,7 @@ const TestOpportunityBookableAdditionalDetails = createCriteria({
],
[
'Must be within validFromBeforeStartDate window',
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
],
[
'Must require additional details',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
mustRequireAttendeeDetails,
remainingCapacityMustBeAtLeastTwo,
mustNotBeOpenBookingInAdvanceUnavailable,
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
mustNotRequireAdditionalDetails,
sellerMustAllowOpenBooking,
} = require('./criteriaUtils');
Expand All @@ -29,7 +29,7 @@ const TestOpportunityBookableAttendeeDetails = createCriteria({
],
[
'Must be within validFromBeforeStartDate window',
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
],
[
'Must require attendee details',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
createCriteria,
remainingCapacityMustBeAtLeastTwo,
mustNotBeOpenBookingInAdvanceUnavailable,
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
sellerMustAllowOpenBooking,
endDateMustBeInThePast,
eventStatusMustNotBeCancelledOrPostponed,
Expand Down Expand Up @@ -45,7 +45,7 @@ const TestOpportunityBookableInPast = createCriteria({
],
[
'Must be within validFromBeforeStartDate window',
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
],
[
'Must not require attendee details',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createCriteria, getRemainingCapacity, mustNotBeOpenBookingInAdvanceUnavailable, mustHaveBeInsideValidFromBeforeStartDateWindow } = require('./criteriaUtils');
const { createCriteria, getRemainingCapacity, mustNotBeOpenBookingInAdvanceUnavailable, mustBeInsideBookingWindowIfOneExists } = require('./criteriaUtils');
const { quantitativeValue, shapeConstraintRecipes } = require('../testDataShape');
const { InternalCriteriaFutureScheduledAndDoesNotRequireDetails } = require('./internal/InternalCriteriaFutureScheduledAndDoesNotRequireDetails');

Expand Down Expand Up @@ -31,7 +31,7 @@ const TestOpportunityBookableNoSpaces = createCriteria({
],
[
'Must be within validFromBeforeStartDate window',
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
],
],
testDataShape: (options) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
createCriteria,
remainingCapacityMustBeAtLeastTwo,
mustNotBeOpenBookingInAdvanceUnavailable,
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
sellerMustAllowOpenBooking,
mustNotRequireAttendeeDetails,
mustNotRequireAdditionalDetails,
Expand Down Expand Up @@ -51,7 +51,7 @@ const TestOpportunityBookableNonFreePrepaymentUnavailable = createCriteria({
],
[
'Must be within validFromBeforeStartDate window',
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
],
[
'Only paid bookable offers with openBookingPrepayment unavailable',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
createCriteria,
getRemainingCapacity,
mustNotBeOpenBookingInAdvanceUnavailable,
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
sellerMustAllowOpenBooking,
} = require('./criteriaUtils');
const {
Expand Down Expand Up @@ -43,7 +43,7 @@ const TestOpportunityBookableOneSpace = createCriteria({
],
[
'Must be within validFromBeforeStartDate window',
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
],
],
testDataShape: (options) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getDateAfterWhichBookingsCanBeMade, remainingCapacityMustBeAtLeastTwo, createCriteria, mustNotBeOpenBookingInAdvanceUnavailable } = require('./criteriaUtils');
const { getDateAfterWhichBookingsCanBeMade, getDateBeforeWhichBookingsCanBeMade, remainingCapacityMustBeAtLeastTwo, createCriteria, mustNotBeOpenBookingInAdvanceUnavailable } = require('./criteriaUtils');
const { dateRange, shapeConstraintRecipes } = require('../testDataShape');
const { InternalCriteriaFutureScheduledAndDoesNotRequireDetails } = require('./internal/InternalCriteriaFutureScheduledAndDoesNotRequireDetails');

Expand All @@ -11,13 +11,15 @@ const { InternalCriteriaFutureScheduledAndDoesNotRequireDetails } = require('./i
*/
function mustHaveBookingWindowAndBeOutsideOfIt(offer, opportunity, options) {
const dateAfterWhichBookingsCanBeMade = getDateAfterWhichBookingsCanBeMade(offer, opportunity);
if (dateAfterWhichBookingsCanBeMade == null) {
const dateBeforeWhichBookingsCanBeMade = getDateBeforeWhichBookingsCanBeMade(offer, opportunity);
if (dateAfterWhichBookingsCanBeMade == null && dateBeforeWhichBookingsCanBeMade == null) {
return false; // has no booking window
}
/* If, within 2 hours, the booking window would be reached, it may be possible for this to happen
during the test run. So, to be on the safe side, we only accept Opportunities whose booking window
starts at least 2 hours in the future. */
return options.harvestStartTimeTwoHoursLater < dateAfterWhichBookingsCanBeMade;
return (dateAfterWhichBookingsCanBeMade !== null && options.harvestStartTimeTwoHoursLater < dateAfterWhichBookingsCanBeMade)
|| (dateBeforeWhichBookingsCanBeMade !== null && options.harvestStartTime > dateBeforeWhichBookingsCanBeMade);
}

/**
Expand Down Expand Up @@ -51,6 +53,9 @@ const TestOpportunityBookableOutsideValidFromBeforeStartDate = createCriteria({
'oa:validFromBeforeStartDate': dateRange({
minDate: options.harvestStartTimeTwoHoursLater.toISO(),
}),
'oa:validThroughBeforeStartDate': dateRange({
maxDate: options.harvestStartTime.toISO(),
}),
},
}),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { TestOpportunityBookable } = require('./TestOpportunityBookable');
const { createCriteria, getDateAfterWhichBookingsCanBeMade } = require('./criteriaUtils');
const { createCriteria, getDateAfterWhichBookingsCanBeMade, getDateBeforeWhichBookingsCanBeMade } = require('./criteriaUtils');
const { dateRange } = require('../testDataShape');

/**
Expand All @@ -11,18 +11,23 @@ const { dateRange } = require('../testDataShape');
*/
function mustHaveBookingWindowAndBeWithinIt(offer, opportunity, options) {
const dateAfterWhichBookingsCanBeMade = getDateAfterWhichBookingsCanBeMade(offer, opportunity);
if (dateAfterWhichBookingsCanBeMade == null) {
const dateBeforeWhichBookingsCanBeMade = getDateBeforeWhichBookingsCanBeMade(offer, opportunity);
if (dateAfterWhichBookingsCanBeMade == null && dateBeforeWhichBookingsCanBeMade == null) {
return false; // has no booking window
}
return options.harvestStartTime > dateAfterWhichBookingsCanBeMade;
/* If, within 2 hours, the end of the booking window would be reached, it may be possible for this to happen
during the test run. So, to be on the safe side, we only accept Opportunities whose booking window
ends at least 2 hours in the future. */
return (dateAfterWhichBookingsCanBeMade == null || options.harvestStartTime > dateAfterWhichBookingsCanBeMade)
&& (dateBeforeWhichBookingsCanBeMade == null || options.harvestStartTimeTwoHoursLater < dateBeforeWhichBookingsCanBeMade);
}

const TestOpportunityBookableWithinValidFromBeforeStartDate = createCriteria({
name: 'TestOpportunityBookableWithinValidFromBeforeStartDate',
opportunityConstraints: [],
offerConstraints: [
[
'Must have booking window (`validFromBeforeStartDate`) and be within it',
'Must have booking window (`validFromBeforeStartDate` or `validThroughBeforeStartDate`) and be within it',
mustHaveBookingWindowAndBeWithinIt,
],
],
Expand All @@ -34,6 +39,10 @@ const TestOpportunityBookableWithinValidFromBeforeStartDate = createCriteria({
maxDate: options.harvestStartTime.toISO(),
// This differs from TestOpportunityBookable as it does not allow null values
}),
'oa:validThroughBeforeStartDate': dateRange({
minDate: options.harvestStartTimeTwoHoursLater.toISO(),
// This differs from TestOpportunityBookable as it does not allow null values
}),
},
}),
});
Expand Down
30 changes: 25 additions & 5 deletions packages/test-interface-criteria/src/criteria/criteriaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ function dateMinusDuration(datetimeIso, durationIso) {
*
* @param {Offer} offer
* @param {Opportunity} opportunity
* @returns {DateTime | null} null if there is no booking window defined.
* @returns {DateTime | null} null if there is no booking window lower limit defined.
*/
function getDateAfterWhichBookingsCanBeMade(offer, opportunity) {
if (!offer || !offer.validFromBeforeStartDate) {
Expand All @@ -287,6 +287,21 @@ function getDateAfterWhichBookingsCanBeMade(offer, opportunity) {
return dateMinusDuration(opportunity.startDate, offer.validFromBeforeStartDate);
}

/**
* Get the date that the startDate - validThroughBeforeStartDate window starts
*
* @param {Offer} offer
* @param {Opportunity} opportunity
* @returns {DateTime | null} null if there is no booking window upper limit defined.
*/
function getDateBeforeWhichBookingsCanBeMade(offer, opportunity) {
if (!offer || !offer.validThroughBeforeStartDate) {
return null; // has no booking window
}

return dateMinusDuration(opportunity.startDate, offer.validThroughBeforeStartDate);
}

/**
* @type {OfferConstraint}
*/
Expand Down Expand Up @@ -374,10 +389,14 @@ function mustNotBeOpenBookingInAdvanceUnavailable(offer) {
/**
* @type {OfferConstraint}
*/
function mustHaveBeInsideValidFromBeforeStartDateWindow(offer, opportunity, options) {
function mustBeInsideBookingWindowIfOneExists(offer, opportunity, options) {
const dateAfterWhichBookingsCanBeMade = getDateAfterWhichBookingsCanBeMade(offer, opportunity);
if (dateAfterWhichBookingsCanBeMade == null) { return true; } // no booking window - therefore bookable at any time
return options.harvestStartTime > dateAfterWhichBookingsCanBeMade;
const dateBeforeWhichBookingsCanBeMade = getDateBeforeWhichBookingsCanBeMade(offer, opportunity);
/* If, within 2 hours, the end of the booking window would be reached, it may be possible for this to happen
during the test run. So, to be on the safe side, we only accept Opportunities whose booking window
ends at least 2 hours in the future. */
return (dateAfterWhichBookingsCanBeMade == null || options.harvestStartTime > dateAfterWhichBookingsCanBeMade)
&& (dateBeforeWhichBookingsCanBeMade == null || options.harvestStartTimeTwoHoursLater < dateBeforeWhichBookingsCanBeMade);
}

/**
Expand Down Expand Up @@ -476,6 +495,7 @@ module.exports = {
getType,
getRemainingCapacity,
getDateAfterWhichBookingsCanBeMade,
getDateBeforeWhichBookingsCanBeMade,
getDateBeforeWhichCancellationsCanBeMade,
hasCapacityLimitOfOne,
remainingCapacityMustBeAtLeastTwo,
Expand All @@ -486,7 +506,7 @@ module.exports = {
endDateMustBeInThePast,
eventStatusMustNotBeCancelledOrPostponed,
mustNotBeOpenBookingInAdvanceUnavailable,
mustHaveBeInsideValidFromBeforeStartDateWindow,
mustBeInsideBookingWindowIfOneExists,
getOrganizerOrProvider,
mustBeOutsideCancellationWindow,
mustNotAllowFullRefund,
Expand Down
2 changes: 1 addition & 1 deletion packages/test-interface-criteria/src/types/Criteria.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TestDataShape } from './TestDataShape';

export type OpportunityConstraint = (opportunity: Opportunity, options?: Options) => boolean;

export type OfferConstraint = (offer: Offer, opportunity: Opportunity, options?: Options) => boolean;
export type OfferConstraint = (offer: Offer, opportunity?: Opportunity, options?: Options) => boolean;

export type TestDataShapeFactory = (options: Options) => TestDataShape;

Expand Down
Loading