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 2 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 @@ -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
@@ -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);
}
nickevansuk marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand Down Expand Up @@ -51,6 +53,9 @@ const TestOpportunityBookableOutsideValidFromBeforeStartDate = createCriteria({
'oa:validFromBeforeStartDate': dateRange({
minDate: options.harvestStartTimeTwoHoursLater.toISO(),
}),
'oa:validThroughBeforeStartDate': dateRange({
maxDate: options.harvestStartTimeTwoHoursLater.toISO(),
}),
nickevansuk marked this conversation as resolved.
Show resolved Hide resolved
},
}),
});
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
25 changes: 23 additions & 2 deletions packages/test-interface-criteria/src/criteria/criteriaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 defined.
nickevansuk marked this conversation as resolved.
Show resolved Hide resolved
*/
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 @@ -376,8 +391,13 @@ function mustNotBeOpenBookingInAdvanceUnavailable(offer) {
*/
function mustHaveBeInsideValidFromBeforeStartDateWindow(offer, opportunity, options) {
nickevansuk marked this conversation as resolved.
Show resolved Hide resolved
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 (dateAfterWhichBookingsCanBeMade == null && dateBeforeWhichBookingsCanBeMade == null) { return true; } // no booking window - therefore bookable at any time
nickevansuk marked this conversation as resolved.
Show resolved Hide resolved
/* 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 +496,7 @@ module.exports = {
getType,
getRemainingCapacity,
getDateAfterWhichBookingsCanBeMade,
getDateBeforeWhichBookingsCanBeMade,
getDateBeforeWhichCancellationsCanBeMade,
hasCapacityLimitOfOne,
remainingCapacityMustBeAtLeastTwo,
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