-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapters.ts
More file actions
70 lines (69 loc) · 1.8 KB
/
adapters.ts
File metadata and controls
70 lines (69 loc) · 1.8 KB
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
/**
* Adapters map external API shapes to our PropertySummary domain model.
*/
import type { PropertySummary } from "./types";
import { PropertySummarySchema } from "./schemas";
interface RawPropertyInput {
id?: string | number;
propertyId?: string | number;
_id?: string | number;
addressLine?: string;
address?: {
fullStreetLine?: string;
street?: string;
city?: string;
state?: string;
zipCode?: string;
};
city?: string;
state?: string;
zip?: string;
price?: number;
metadata?: {
listPrice?: number;
listDate?: string;
};
details?: {
beds?: number;
fullBaths?: number;
sqft?: number;
lotSqft?: number;
};
beds?: number;
baths?: number;
sqft?: number;
lotSqft?: number;
media?: {
images?: Array<{ url?: string }>;
};
primary_photo?: string;
badges?: unknown;
aiScore?: number;
createdAt?: string;
}
/**
* Map any backend property object to PropertySummary. Extend as needed.
*/
export function toPropertySummary(input: RawPropertyInput): PropertySummary {
const candidate = {
id: String(input.id ?? input.propertyId ?? input._id),
addressLine:
input.addressLine ??
input.address?.fullStreetLine ??
input.address?.street ??
"Unknown address",
city: input.city ?? input.address?.city ?? "",
state: input.state ?? input.address?.state ?? "",
zip: input.zip ?? input.address?.zipCode ?? "",
price: input.price ?? input.metadata?.listPrice,
beds: input.details?.beds ?? input.beds,
baths: input.details?.fullBaths ?? input.baths,
sqft: input.details?.sqft ?? input.sqft,
lotSqft: input.details?.lotSqft ?? input.lotSqft,
imageUrl: input.media?.images?.[0]?.url ?? input.primary_photo ?? undefined,
badges: input.badges,
aiScore: input.aiScore,
createdAt: input.createdAt ?? input.metadata?.listDate,
};
return PropertySummarySchema.parse(candidate);
}