-
Notifications
You must be signed in to change notification settings - Fork 4
/
plans.ts
90 lines (76 loc) · 2.4 KB
/
plans.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Item, ItemId, MatchedItem, SearchFunction } from '@/app/items/types'
import HousePlans, {
HousePlanSearchResponseData,
SearchParams,
SearchResponseData,
} from '../sdks/houseplans'
/**
* Global cache of floor plans returned from the API
*/
const PLANS = new Map<ItemId, Item>()
interface PlanMatchedItem extends MatchedItem {
bathrooms: HousePlanSearchResponseData['bathrooms']
bedrooms: HousePlanSearchResponseData['bedrooms']
squareFeet: HousePlanSearchResponseData['squarefeet']
stories: HousePlanSearchResponseData['stories']
}
const HOUSE_PLANS_CLIENT = new HousePlans({
debug: true,
})
/**
* Returns the top floor plans that match the filter parameters
* @param filterParams Parameters to filter the plans by
* @returns the matched plans
*/
export const searchPlans: SearchFunction<SearchParams> = async (
filterParams,
) => {
console.log('Searching for Floor plans:', filterParams)
let results: SearchResponseData['results'] = []
try {
results = (await HOUSE_PLANS_CLIENT.plans(filterParams)).results
} catch (error) {
console.error('Error searching for plans:', error, filterParams)
}
const items = results.map(
(plan): PlanMatchedItem => ({
bathrooms: plan.bathrooms,
bedrooms: plan.bedrooms,
id: plan.number,
squareFeet: plan.squarefeet,
stories: plan.stories,
// Trimming the plan from the title is key for the assistant properly
// displaying the plan ID in the content we need to parse
title: plan.thumbnailAlt.replace(`#${plan.number}`, '').trim(),
}),
)
return { items }
}
/**
* Get the plans for the given item IDs
*/
export const getPlans = async (itemIds: string[]): Promise<Item[]> => {
if (itemIds.length === 0) {
return []
}
const missingItemIds = itemIds.filter((itemId) => !PLANS.has(itemId))
// If there are any missing plans, grab them and add them to the global cache
if (missingItemIds.length > 0) {
for (const itemId of missingItemIds) {
try {
const plan = await HOUSE_PLANS_CLIENT.plan(itemId)
PLANS.set(itemId, {
id: itemId,
imageUrl: plan.image,
title: plan.name,
url: plan.offers.url,
})
} catch (error) {
console.error(`Error getting plans for ${itemId}:`, error)
}
}
}
return itemIds
.map((itemId) => PLANS.get(itemId))
.filter((item): item is Item => Boolean(item))
}