-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model for reservation search page
- Loading branch information
Showing
5 changed files
with
210 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
export enum Park { | ||
FortLangleyNHS, | ||
SMONEĆTEN, | ||
} | ||
|
||
export enum Org { | ||
ParksCanada, | ||
BCParks, | ||
} | ||
|
||
export enum Group { | ||
Frontcountry = 0, | ||
Backcountry = 1, | ||
Accommodations = 2, | ||
DayUse = 3, | ||
} | ||
|
||
export enum Category { | ||
Campsite = 0, | ||
Accommodation = 1, | ||
GroupCampsite = 2, | ||
BackcountryCampsite = 5, | ||
BackcountryZone = 7, | ||
} | ||
|
||
export enum EquipmentType { | ||
TentOrVehicle = "-32768", | ||
TentsOnly = "-32767", | ||
} | ||
|
||
export enum TentOrVehicleSubtype { | ||
SmallTent = "-32768", | ||
MediumTent = "-32767", | ||
LargeTent = "-32766", | ||
VanOrPickup = "-32765", | ||
} | ||
|
||
export interface TentOrVehicleSpec { | ||
equipment: EquipmentType.TentOrVehicle; | ||
subtype: TentOrVehicleSubtype[]; | ||
} | ||
|
||
export type EquipmentSpec = TentOrVehicleSpec; | ||
|
||
export interface ParkInfo { | ||
org: Org; | ||
group: Group; | ||
category: Category; | ||
mapId: string; | ||
resourceLocationId: string; | ||
equipment?: EquipmentSpec; | ||
} | ||
|
||
function getParkInfo(park: Park): ParkInfo { | ||
return PARKS[park]; | ||
} | ||
|
||
export interface ParkReserveParams { | ||
park: Park; | ||
checkInDate?: Date; | ||
checkOutDate?: Date; | ||
searchTime?: Date; | ||
} | ||
|
||
export function getReservationUrl({ | ||
park, | ||
checkInDate, | ||
checkOutDate, | ||
searchTime, | ||
}: ParkReserveParams): string { | ||
const { org, group, category, mapId, resourceLocationId } = getParkInfo(park); | ||
const url = new URL(baseUrl(org)); | ||
url.searchParams.set("searchTabGroupId", group.toString()); | ||
url.searchParams.set("bookingCategoryId", category.toString()); | ||
url.searchParams.set("mapId", mapId); | ||
url.searchParams.set("resourceLocationId", resourceLocationId); | ||
|
||
const [t1, t2] = todayAndTomorrow(); | ||
url.searchParams.set( | ||
"startDate", | ||
(checkInDate ?? t1).toLocaleDateString("sv") | ||
); | ||
url.searchParams.set( | ||
"endDate", | ||
(checkOutDate ?? t2).toLocaleDateString("sv") | ||
); | ||
|
||
url.searchParams.set("nights", "1"); // TODO: compute | ||
url.searchParams.set("isReserving", "true"); | ||
url.searchParams.set("partySize", "2"); // TODO: prop param | ||
url.searchParams.set( | ||
"searchTime", | ||
(searchTime ?? new Date()).toLocaleString("sv").replace(" ", "T") | ||
); | ||
url.searchParams.set("flexibleSearch", "[false,false,null,1]"); // TODO how to use this? | ||
url.searchParams.set("filterData", '{"-32756":"[[1],0,0,0]"}'); // TODO how to use this? | ||
return url.toString(); | ||
} | ||
|
||
function baseUrl(org: Org): string { | ||
switch (org) { | ||
case Org.ParksCanada: | ||
return "https://reservation.pc.gc.ca/create-booking/results"; | ||
default: | ||
throw new Error(`${org} not mapped to a reservation website`); | ||
} | ||
} | ||
|
||
const PARKS: Record<Park, ParkInfo> = { | ||
[Park.FortLangleyNHS]: { | ||
org: Org.ParksCanada, | ||
group: Group.Accommodations, | ||
category: Category.Accommodation, | ||
mapId: "-2147483535", | ||
resourceLocationId: "-2147483623", | ||
}, | ||
[Park.SMONEĆTEN]: { | ||
org: Org.ParksCanada, | ||
group: Group.Frontcountry, | ||
category: Category.Campsite, | ||
mapId: "-2147483477", | ||
resourceLocationId: "-2147483601", | ||
equipment: { | ||
equipment: EquipmentType.TentOrVehicle, | ||
subtype: [TentOrVehicleSubtype.SmallTent], | ||
}, | ||
}, | ||
}; | ||
|
||
/** | ||
* Returns today and tomorrow to be used in search params. | ||
* @returns a pair of strings in yyyy-mm-dd format | ||
*/ | ||
function todayAndTomorrow(): [Date, Date] { | ||
const offset = new Date().getTimezoneOffset(); | ||
const today = new Date(new Date().getTime() - offset * 60 * 1000); | ||
const tomorrow = new Date(new Date().getTime() - offset * 60 * 1000); | ||
tomorrow.setDate(today.getDate() + 1); | ||
return [today, tomorrow]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { getReservationUrl, Park } from "./reservations"; | ||
|
||
describe(getReservationUrl.name, () => { | ||
const defaultArgs = { | ||
searchTime: new Date(1704236645000), // 2024-01-02 3:04:05 PM (GMT-08:00) | ||
checkInDate: new Date(1704323045000), // 1 day after t0 | ||
checkOutDate: new Date(1704409445000), // 1 day after t1 | ||
}; | ||
const defaultParams = [ | ||
"&startDate=2024-01-03&endDate=2024-01-04&nights=1", | ||
"&isReserving=true&partySize=2", | ||
"&searchTime=2024-01-02T15%3A04%3A05", | ||
"&flexibleSearch=%5Bfalse%2Cfalse%2Cnull%2C1%5D", | ||
]; | ||
|
||
it("should work for Fort Langley National Historic Site", () => { | ||
const url = getReservationUrl({ | ||
park: Park.FortLangleyNHS, | ||
...defaultArgs, | ||
}); | ||
const expectedUrl = [ | ||
"https://reservation.pc.gc.ca/create-booking/results", | ||
"?searchTabGroupId=2", // accommodation | ||
"&bookingCategoryId=1", // accommodation | ||
"&mapId=-2147483535", | ||
"&resourceLocationId=-2147483623", | ||
...defaultParams, | ||
"&filterData=%7B%22-32756%22%3A%22%5B%5B1%5D%2C0%2C0%2C0%5D%22%7D", | ||
].join(""); | ||
expect(url).toBe(expectedUrl); | ||
}); | ||
|
||
it("should work for SMONEĆTEN", () => { | ||
const url = getReservationUrl({ | ||
park: Park.SMONEĆTEN, | ||
...defaultArgs, | ||
}); | ||
const expectedUrl = [ | ||
"https://reservation.pc.gc.ca/create-booking/results", | ||
"?searchTabGroupId=0", // frontcountry | ||
"&bookingCategoryId=0", // campsite | ||
"&mapId=-2147483477", | ||
"&resourceLocationId=-2147483601", | ||
...defaultParams, | ||
"&filterData=%7B%7D&equipmentId=-32768", | ||
"&subEquipmentId=-32768", | ||
].join(""); | ||
expect(url).toBe(expectedUrl); | ||
}); | ||
}); |