/
Mapper.ts
123 lines (108 loc) · 3.74 KB
/
Mapper.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { camelizeKeys } from 'humps';
import { Location } from '../Geo';
import { Bounds } from '../Geo/Bounds';
import { MainMedia } from '../Media/Media';
import { Place } from './Place';
import { Description, PlaceDetail, Reference, Tag } from './PlaceDetail';
import { PlaceGeometry } from './PlaceGeometry';
import { PlaceOpeningHours } from './PlaceOpeningHours';
import { PlaceReview } from './PlaceReview';
import { PlaceReviewsData } from './PlaceReviewsData';
const defaultPhotoSize = '300x300';
export const mapPlaceApiResponseToPlaces = (places: any): Place[] => {
return places.map((place) => {
return mapPlace(place, null);
});
};
export const mapPlaceDetailedApiResponseToPlace = (place: any, photoSize: string): Place => {
const detail = mapPlaceDetail(place, photoSize);
return mapPlace(place, detail);
};
export const mapPlaceDetailedBatchApiResponseToPlaces = (places: any, photoSize: string): Place[] => {
return places.map((place) => {
const detail = mapPlaceDetail(place, photoSize);
return mapPlace(place, detail);
});
};
export const mapPlace = (place, detail: PlaceDetail | null) => {
return {
id: place.id,
level: place.level,
rating: place.rating,
location: place.location as Location,
quadkey: place.quadkey,
name: place.name,
nameSuffix: place.name_suffix,
boundingBox: place.bounding_box as Bounds,
perex: place.perex,
url: place.url,
thumbnailUrl: place.thumbnail_url,
marker: place.marker,
categories: place.categories,
parents: place.parent_ids,
starRating: place.star_rating,
starRatingUnofficial: place.star_rating_unofficial,
customerRating: place.customer_rating,
detail
} as Place;
};
const mapPlaceDetail = (place, photoSize): PlaceDetail => {
const tags: Tag[] = place.tags.map((tag) => (camelizeKeys(tag) as Tag));
const description: Description | null = place.description ? camelizeKeys(place.description) as Description : null;
const references: Reference[] = place.references.map((reference) => camelizeKeys(reference));
const resultPlaceDetail = {
tags,
address: place.address,
admission: place.admission,
description,
email: place.email,
duration: place.duration,
openingHours: place.opening_hours,
phone: place.phone,
media: mapMainMediaToMedia(camelizeKeys(place.main_media), photoSize),
references
} as PlaceDetail;
if (place.owner_id) {
resultPlaceDetail.ownerId = place.owner_id;
}
return resultPlaceDetail;
};
export const mapMainMediaToMedia = (mainMedia, photoSize: string): MainMedia => {
const mappedMedia: MainMedia = {
square: null,
videoPreview: null,
portrait: null,
landscape: null
};
if (mainMedia) {
Object.keys(mainMedia.usage).forEach((key) => {
const mediaId = mainMedia.usage[key];
mappedMedia[key] = mainMedia.media.reduce((acc, item) => {
if (item.id === mediaId) {
item.urlWithSize = item.urlTemplate.replace(/{size}/i, photoSize || defaultPhotoSize);
return item;
}
return acc;
}, null);
});
}
return mappedMedia as MainMedia;
};
export const mapPlaceGeometryApiResponseToPlaceGeometry = (placeGeometry: any): PlaceGeometry => ({
geometry: placeGeometry.geometry as GeoJSON.GeoJsonObject,
isShape: placeGeometry.is_shape
} as PlaceGeometry
);
export const mapPlaceOpeningHours = (placeOpeningHours: any): PlaceOpeningHours => {
return placeOpeningHours as PlaceOpeningHours;
};
export const mapPlaceReview = (placeReview: any): PlaceReview => {
return camelizeKeys(placeReview) as PlaceReview;
};
export const mapPlaceReviewsData = (placeReviewsData: any): PlaceReviewsData => {
return {
rating: placeReviewsData.rating,
currentUserHasReview: placeReviewsData.current_user_has_review,
reviews: placeReviewsData.reviews.map((placeReview: any) => mapPlaceReview(placeReview))
} as PlaceReviewsData;
};