@@ -31,22 +31,82 @@ const searchHotel = async (body) => {
31
31
32
32
const searchResults = await transform ( response . data , hotelAvailTransformTemplate ) ;
33
33
34
- for ( const accommodation of searchResults . accommodations ) {
35
- accommodation . otherPolicies = reduceToObjectByKey ( accommodation . otherPolicies ) ;
36
- accommodation . otherPolicies = reduceObjectToProperty ( accommodation . otherPolicies , '_value_' ) ;
37
-
38
- for ( const roomType of accommodation . roomTypes ) {
34
+
35
+ // Go through the Room Stays to build the offers and gather the room types
36
+ var accomodationRoomTypes = { } ;
37
+ var offers = { } ;
38
+ searchResults . _roomStays_ . forEach ( roomStay => {
39
+
40
+ // Create the accommodation key
41
+ var accommodationReference = `${ roomStay . _provider_ } .${ roomStay . _hotelCode_ } ` ;
42
+
43
+ // Handle the room types
44
+ for ( const roomType of roomStay . _roomTypes_ ) {
45
+ // Reduce the policies
39
46
roomType . policies = reduceToObjectByKey ( roomType . policies ) ;
40
47
roomType . policies = reduceObjectToProperty ( roomType . policies , '_value_' ) ;
48
+
49
+ // Add the room type to the dict that will be used when building accomodation
50
+ if ( ! ( accomodationRoomTypes [ accommodationReference ] ) ) {
51
+ accomodationRoomTypes [ accommodationReference ] = { } ;
52
+ }
53
+ accomodationRoomTypes [ accommodationReference ] [ roomType . _id_ ] = roomType ;
54
+ delete ( accomodationRoomTypes [ accommodationReference ] [ roomType . _id_ ] . _id_ ) ;
41
55
}
42
- accommodation . roomTypes = reduceToObjectByKey ( accommodation . roomTypes ) ;
43
- accommodation . ratePlans = reduceToObjectByKey ( accommodation . ratePlans ) ;
44
- }
45
56
46
- searchResults . accommodations = reduceAcomodation ( searchResults . accommodations ) ;
57
+ // Handle the Rate Plans
58
+ searchResults . pricePlans = reduceToObjectByKey ( roomStay . _ratePlans_ ) ;
59
+ delete ( roomStay . _ratePlans_ ) ;
60
+
61
+ // Build the offers by parsing the room rates
62
+ roomStay . _roomRates_ . forEach ( roomRate => {
47
63
48
- // Create the offers
49
- searchResults . offers = reduceRoomStays ( searchResults . _roomStays_ ) ;
64
+ // Build the offer key
65
+ var offerKey = `${ accommodationReference } .${ roomRate . ratePlanReference } .${ roomRate . roomTypeReference } ` ;
66
+
67
+ // Build the PricePlanReference
68
+ var pricePlanReference = {
69
+ accommodation : accommodationReference ,
70
+ roomType : roomRate . roomTypeReference ,
71
+ } ;
72
+ pricePlansReferences = { } ;
73
+ pricePlansReferences [ roomRate . ratePlanReference ] = pricePlanReference ;
74
+
75
+ // Build the offer
76
+ var offer = {
77
+ // Reference from other elements
78
+ pricePlansReferences : pricePlansReferences ,
79
+
80
+ // Build price
81
+ price : {
82
+ currency : roomRate . price . currency ,
83
+ public : roomRate . price . _afterTax_ ,
84
+ taxes : new Number ( roomRate . price . _afterTax_ ) - new Number ( roomRate . price . _beforeTax_ ) ,
85
+ } ,
86
+ } ;
87
+
88
+ // Add the offer to the dict
89
+ offers [ offerKey ] = offer ;
90
+ } ) ;
91
+ } ) ;
92
+
93
+ // Parse the accomodations
94
+ for ( var accommodation of searchResults . accommodations ) {
95
+
96
+ // Build the accomodation reference key
97
+ var accommodationReference = `${ accommodation . _provider_ } .${ accommodation . _id_ } ` ;
98
+
99
+ // Reduce the policies
100
+ accommodation . otherPolicies = reduceToObjectByKey ( accommodation . otherPolicies ) ;
101
+ accommodation . otherPolicies = reduceObjectToProperty ( accommodation . otherPolicies , '_value_' ) ;
102
+
103
+ // Add the room types gathered from Room Rates
104
+ accommodation . roomTypes = accomodationRoomTypes [ accommodationReference ] ;
105
+
106
+ }
107
+ searchResults . accommodations = reduceAcomodation ( searchResults . accommodations ) ;
108
+
109
+ searchResults . offers = offers ;
50
110
delete ( searchResults . _roomStays_ ) ;
51
111
52
112
return searchResults ;
0 commit comments