Last active
May 14, 2024 14:15
-
-
Save wouterraateland/030f6bfb189dc10fe2b200bb68c94cf5 to your computer and use it in GitHub Desktop.
Search example for Bookingmood (TypeScript)
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
const API_KEY = "YOUR_API_KEY"; | |
// Append additional options here (fetch using /v1/attribute_options) | |
const OPTION_MAP = [ | |
{ name: "Videography", id: "a32963c2-8c80-4ed1-914d-726796ca1eea" }, | |
{ name: "Photography", id: "ec26c3c2-b07c-4b99-b3b7-c1a60fdbf7f6" }, | |
{ name: "Austin", id: "f71b772e-3c57-476e-9204-d1b5bde20b8f" }, | |
{ name: "San Antonio", id: "24583a14-2c9f-4dc8-a0e2-4dc5b0e8fd72" }, | |
{ name: "New York City", id: "3cc28053-1d65-4aee-95b4-c9348486bcf1" }, | |
]; | |
async function searchAvailableProducts( | |
interval: { start: string; end: string }, | |
mediaTypes: Array<string>, | |
location: string | null, | |
) { | |
// Search product availability on some interval | |
const searchResults = await fetch("https://api.bookingmood.com/v1/search", { | |
method: "POST", | |
headers: { Authorization: `Bearer ${API_KEY}` }, | |
body: JSON.stringify({ | |
interval, | |
option_ids: [location, ...mediaTypes].flatMap((name) => | |
OPTION_MAP.filter((option) => option.name === name).map( | |
(option) => option.id, | |
), | |
), | |
}), | |
}).then((response) => response.json()); | |
// Find the first available product | |
const product_ids = searchResults | |
.filter((result) => result.match) | |
.map((result) => result.productId); | |
// Fetch products | |
const products = await fetch( | |
`https://api.bookingmood.com/v1/products?select=*&id=in.(${product_ids.join(",")})`, | |
{ headers: { Authorization: `Bearer ${API_KEY}` } }, | |
).then((response) => response.json()); | |
return products; | |
} | |
// Usage | |
export async function GET() { | |
const products = await searchAvailableProducts( | |
{ start: "2024-06-03", end: "2024-06-04" }, | |
["Videography"], | |
"Austin", | |
); | |
return Response.json(products); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment