11import { getUniqueId } from "./utils"
22import QuickCache from "./quickcache"
33
4+ const FLAG_ALLOW_UNKNOWN_ERROR = false
5+
46function getWeather ( { location, date } ) {
57
68 if ( ! location ) return { error : 'Invalid location' , message : 'Please specify the location' }
79 if ( ! date ) return { error : 'Invalid date' , message : 'Please specify the date' }
810
9- if ( QuickCache . weather . some ( ( qc ) => qc . key === [ location , date ] . join ( '_' ) ) ) {
11+ /* if(QuickCache.weather.some((qc) => qc.key === [location, date].join('_'))) {
1012 const stored_data = QuickCache.weather.find((qc) => qc.key === [location, date].join('_'))
1113 if(stored_data) {
1214 return JSON.parse(stored_data.value)
1315 }
16+ }*/
17+
18+ if ( QuickCache . exist ( 'weather' , location , date ) ) {
19+ const stored_data = QuickCache . retrieve ( 'weather' , location , date )
20+ if ( stored_data ) return stored_data
1421 }
1522
16- const temperature = Math . floor ( 30 * Math . random ( ) )
23+ const temperature = Math . floor ( 25 * Math . random ( ) )
1724
1825 const conditions = [ 'Sunny' , 'Cloudy' , 'Rainy' ]
1926 const index = Math . floor ( conditions . length * Math . random ( ) )
2027
2128 const weather_data = { location, date, temperature, unit : 'celsius' , condition : conditions [ index ] }
2229
23- QuickCache . weather . push ( { key : [ location , date ] . join ( '_' ) , value : JSON . stringify ( weather_data ) } )
30+ //QuickCache.weather.push({ key: [location, date].join('_'), value: JSON.stringify(weather_data)})
31+ QuickCache . save ( 'weather' , JSON . stringify ( weather_data ) , location , date )
2432
2533 return weather_data
2634}
2735
2836function getEvents ( { location, date } ) {
2937
30- //let chance = Math.round(15 * Math.random())
38+ if ( FLAG_ALLOW_UNKNOWN_ERROR ) {
39+
40+ let chance = Math . round ( 15 * Math . random ( ) )
41+
42+ if ( chance === 13 ) {
43+ return { error : 'Unknown error' , message : 'Failed to get events in specified location and date' , location, date }
44+ }
3145
32- /*
33- if(chance === 13) {
34- return { error: 'Unknown error', message: 'Failed to get events in specified location and date', location, date }
3546 }
36- */
3747
3848 if ( ! location ) return { error : 'Invalid location' , message : 'Please specify the location' }
3949 if ( ! date ) return { error : 'Invalid date' , message : 'Please specify the date' }
4050
51+ /*
4152 if(QuickCache.events.some((qc) => qc.key === [location, date].join('_'))) {
4253 const stored_data = QuickCache.events.find((qc) => qc.key === [location, date].join('_'))
4354 if(stored_data) {
4455 return JSON.parse(stored_data.value)
4556 }
4657 }
58+ */
59+
60+ if ( QuickCache . exist ( 'events' , location , date ) ) {
61+ const stored_data = QuickCache . retrieve ( 'events' , location , date )
62+ if ( stored_data ) return stored_data
63+ }
4764
4865 const events = [ 'Outdoor Fiesta' , 'Sumo Exhibition' , 'Art Festival' , 'Street Dance Parade' , 'Farm Marche' , 'Folk Concert' , 'Soul Food Festival' , 'Earth Day' , 'Ramen Festival' , 'Jazz Festival' ]
4966 const index = Math . floor ( events . length * Math . random ( ) )
5067
5168 const events_data = { location, date, event : events [ index ] }
5269
53- QuickCache . events . push ( { key : [ location , date ] . join ( '_' ) , value : JSON . stringify ( events_data ) } )
70+ //QuickCache.events.push({ key: [location, date].join('_'), value: JSON.stringify(events_data)})
71+ QuickCache . save ( 'events' , JSON . stringify ( events_data ) , location , date )
5472
5573 return events_data
5674
@@ -60,21 +78,28 @@ function getEvent({ location, date, event }) {
6078
6179 let chance = Math . round ( 15 * Math . random ( ) )
6280
63- /*
64- if(chance === 13) {
65- return { error: 'Unknown error', message: 'Failed to get event information', event, location, date }
81+ if ( FLAG_ALLOW_UNKNOWN_ERROR ) {
82+
83+ if ( chance === 13 ) {
84+ return { error : 'Unknown error' , message : 'Failed to get event information' , event, location, date }
85+ }
86+
6687 }
67- */
6888
6989 if ( ! location ) return { error : 'Invalid location' , message : 'Please specify the location' }
7090 if ( ! date ) return { error : 'Invalid date' , message : 'Please specify the date' }
7191 if ( ! event ) return { error : 'Invalid event' , message : 'Please specify the event name' }
7292
73- if ( QuickCache . event . some ( ( qc ) => qc . key === [ location , date , event ] . join ( '_' ) ) ) {
93+ /* if(QuickCache.event.some((qc) => qc.key === [location, date, event].join('_'))) {
7494 const stored_data = QuickCache.event.find((qc) => qc.key === [location, date, event].join('_'))
7595 if(stored_data) {
7696 return JSON.parse(stored_data.value)
7797 }
98+ }*/
99+
100+ if ( QuickCache . exist ( 'event' , location , date , event ) ) {
101+ const stored_data = QuickCache . retrieve ( 'event' , location , date , event )
102+ if ( stored_data ) return stored_data
78103 }
79104
80105 const times = [ '10:00 - 18:00' , '11:00 - 15:00' , '13:00 - 18:00' , '15:00 - 20:00' , '18:00 - 22:00' ]
@@ -98,7 +123,8 @@ function getEvent({ location, date, event }) {
98123
99124 const event_data = { location, date, event, time : stime , place : splace , links, images }
100125
101- QuickCache . event . push ( { key : [ location , date , event ] . join ( '_' ) , value : JSON . stringify ( event_data ) } )
126+ //QuickCache.event.push({ key: [location, date, event].join('_'), value: JSON.stringify(event_data)})
127+ QuickCache . save ( 'event' , JSON . stringify ( event_data ) , location , date , event )
102128
103129 return event_data
104130
@@ -108,19 +134,26 @@ function searchHotel({ location }) {
108134
109135 let chance = Math . round ( 15 * Math . random ( ) )
110136
111- /*
112- if(chance === 13) {
113- return { error: 'Unknown error', message: 'Failed to search for hotels in specified location', location }
137+ if ( FLAG_ALLOW_UNKNOWN_ERROR ) {
138+
139+ if ( chance === 13 ) {
140+ return { error : 'Unknown error' , message : 'Failed to search for hotels in specified location' , location }
141+ }
142+
114143 }
115- */
116144
117145 if ( ! location ) return { error : 'Invalid location' , message : 'Please specify the location' }
118146
119- if ( QuickCache . hotels . some ( ( qc ) => qc . key === location ) ) {
147+ /* if(QuickCache.hotels.some((qc) => qc.key === location)) {
120148 const stored_data = QuickCache.hotels.find((qc) => qc.key === location)
121149 if(stored_data) {
122150 return JSON.parse(stored_data.value)
123151 }
152+ }*/
153+
154+ if ( QuickCache . exist ( 'hotels' , location ) ) {
155+ const stored_data = QuickCache . retrieve ( 'hotels' , location )
156+ if ( stored_data ) return stored_data
124157 }
125158
126159 chance = Math . round ( 10 * Math . random ( ) )
@@ -148,7 +181,8 @@ function searchHotel({ location }) {
148181
149182 const hotels_data = { location, items, message : `Found ${ items . length } hotels` }
150183
151- QuickCache . hotels . push ( { key : location , value : JSON . stringify ( hotels_data ) } )
184+ //QuickCache.hotels.push({ key: location, value: JSON.stringify(hotels_data)})
185+ QuickCache . save ( 'hotels' , JSON . stringify ( hotels_data ) , location )
152186
153187 return hotels_data
154188
@@ -157,15 +191,22 @@ function searchHotel({ location }) {
157191function getHotel ( { location, hotel } ) {
158192 let chance = Math . round ( 15 * Math . random ( ) )
159193
160- /*
161- if(chance === 13) {
162- return { error: 'Unknown error', message: 'Failed to get hotel information', location, hotel }
194+ if ( FLAG_ALLOW_UNKNOWN_ERROR ) {
195+
196+ if ( chance === 13 ) {
197+ return { error : 'Unknown error' , message : 'Failed to get hotel information' , location, hotel }
198+ }
199+
163200 }
164- */
165201
166202 if ( ! location ) return { error : 'Invalid location' , message : 'Please specify the location or branch' }
167203 if ( ! hotel ) return { error : 'Invalid hotel name' , message : 'Please specify the name of hotel' }
168204
205+ if ( QuickCache . exist ( 'hotel' , location , hotel ) ) {
206+ const stored_data = QuickCache . retrieve ( 'hotel' , location , hotel )
207+ if ( stored_data ) return stored_data
208+ }
209+
169210 chance = Math . round ( 10 * Math . random ( ) )
170211 if ( chance === 8 ) {
171212 return { location, hotel, message : 'Hotel information not found' }
@@ -216,8 +257,11 @@ function getHotel({ location, hotel }) {
216257 chance = Math . floor ( 10 * Math . random ( ) )
217258 const images = [ { alt : hotel , src : chance > 5 ? 'https://i.postimg.cc/jjc1LSrH/d5592424-e3f0-4dfa-afb2-2dcc7308e321.jpg' : 'https://i.postimg.cc/Xv4hjytN/dea57a4a-532b-43d2-85bb-0e0172d8c594.jpg' } ]
218259
219- return { location, hotel, description, price : price . toLocaleString ( ) , amenities, website, images }
260+ const hotel_data = { location, hotel, description, price : price . toLocaleString ( ) , amenities, website, images }
220261
262+ QuickCache . save ( 'hotel' , JSON . stringify ( hotel_data ) , location , hotel )
263+
264+ return hotel_data
221265}
222266
223267function reserveHotel ( {
@@ -230,13 +274,15 @@ function reserveHotel({
230274 roomType,
231275 specialRequests
232276} ) {
233- //let chance = Math.round(15 * Math.random())
277+ if ( FLAG_ALLOW_UNKNOWN_ERROR ) {
278+
279+ let chance = Math . round ( 15 * Math . random ( ) )
280+
281+ if ( chance === 13 ) {
282+ return { error : 'Unknown error' , message : 'Failed to make hotel reservation. Please try again.' , hotel, location }
283+ }
234284
235- /*
236- if(chance === 13) {
237- return { error: 'Unknown error', message: 'Failed to make hotel reservation. Please try again.', hotel, location }
238285 }
239- */
240286
241287 if ( ! location ) return { error : 'Invalid location' , message : 'Please specify the location or branch' }
242288 if ( ! hotel ) return { error : 'Invalid hotel name' , message : 'Please specify the name of hotel' }
@@ -250,9 +296,11 @@ function reserveHotel({
250296 return { status : 'No name provided' , message : 'Please ask user provide your full name' , hotel, location, numberOfGuests, checkInDate, checkOutDate, roomType, specialRequests }
251297 }
252298
253- return {
299+ const reservationId = getUniqueId ( )
300+
301+ const reservation_data = {
254302 status : 'Reservation successful' ,
255- reservationId : getUniqueId ( ) ,
303+ reservationId : reservationId ,
256304 message : 'Your reservation has been completed. Please present your reservationId at the front desk.' ,
257305 hotel,
258306 location,
@@ -264,18 +312,39 @@ function reserveHotel({
264312 specialRequests
265313 }
266314
315+ QuickCache . save ( 'reservation' , JSON . stringify ( reservation_data ) , location , hotel , reservationId )
316+
317+ return reservation_data
318+
267319}
268320
269321function getReservation ( { reservationId, hotel, location } ) {
270- //let chance = Math.round(15 * Math.random())
322+
323+ let chance = Math . round ( 15 * Math . random ( ) )
324+
325+ if ( FLAG_ALLOW_UNKNOWN_ERROR ) {
326+
327+ if ( chance === 13 ) {
328+ return { status : 'Server is busy' , message : 'Failed to get hotel reservation. Please try again later.' , reservationId, hotel, location }
329+ }
271330
272- /*
273- if(chance === 13) {
274- return { status: 'Server is busy', message: 'Failed to get hotel reservation. Please try again later.', reservationId, hotel, location }
275331 }
276- */
277332
278- return { status : "Server is busy" , message : "Please try again later." }
333+ if ( ! location ) return { error : 'Invalid location' , message : 'Please specify the location or branch' }
334+ if ( ! hotel ) return { error : 'Invalid hotel name' , message : 'Please specify the name of hotel' }
335+ if ( ! reservationId ) return { error : 'Invalid reservation id' , message : 'Please specify the reservation id provided' }
336+
337+ chance = Math . round ( 10 * Math . random ( ) )
338+ if ( chance === 8 ) {
339+ return { status : "Server is busy" , message : "Please try again later." , reservationId, hotel, location }
340+ }
341+
342+ if ( QuickCache . exist ( 'reservation' , location , hotel , reservationId ) ) {
343+ const stored_data = QuickCache . retrieve ( 'reservation' , location , hotel , reservationId )
344+ if ( stored_data ) return stored_data
345+ }
346+
347+ return { status : "Reservation not found" , message : "There is no record found." , reservationId, hotel, location }
279348
280349}
281350
0 commit comments