diff --git a/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php b/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php index cd78742..d3b93bd 100644 --- a/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php +++ b/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php @@ -156,25 +156,91 @@ class RestaurantApiServices public function listFavRestaurant($customerIamId) - { - try { - $list = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)->get()->toArray(); - $customerFavouriteRestaurants = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId) - ->pluck('restaurant_xid') - ->toArray(); +{ + try { + // Get list of favorite restaurant IDs + $customerFavouriteRestaurants = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId) + ->pluck('restaurant_xid') + ->toArray(); - $restaurants = ManageRestaurant::with('operatingHours')->where('is_active', '1')->whereIn('id', $customerFavouriteRestaurants)->get(); + // Fetch the restaurant details + $restaurants = ManageRestaurant::where('is_active', '1') + ->whereIn('id', $customerFavouriteRestaurants) + ->get(); - foreach ($restaurants as &$res) { - $res['image'] = ListingImageUrl('restaurant_images', $res['image']); + $client = new Client(); + $googlePlaceApiKey = config('constants.googlePlaces.api_key'); + $promises = []; + + foreach ($restaurants as &$res) { + $res['image'] = ListingImageUrl('restaurant_images', $res['image']); + + $cacheKey = 'restaurant_hours_' . $res->name; + if (Cache::has($cacheKey)) { + $res['operating_hours'] = Cache::get($cacheKey); + } else { + // Prepare the first request to get the place_id + $promises[$res->name] = $client->getAsync('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', [ + 'query' => [ + 'fields' => 'place_id', + 'input' => $res->name, + 'inputtype' => 'textquery', + 'key' => $googlePlaceApiKey + ] + ]); } - - return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), $restaurants, 200); - } catch (Exception $ex) { - Log::error('List of Favourite Restaurant service failed : ' . $ex->getMessage()); - return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500); } + + // Execute all the first requests concurrently + $results = Utils::settle($promises)->wait(); + + $detailPromises = []; + foreach ($restaurants as &$res) { + if (isset($results[$res->name]['value'])) { + $response = $results[$res->name]['value']; + $placeData = json_decode($response->getBody(), true); + + if (isset($placeData['candidates'][0]['place_id'])) { + $placeId = $placeData['candidates'][0]['place_id']; + + // Prepare the second request to get the operating hours + $detailPromises[$res->name] = $client->getAsync('https://maps.googleapis.com/maps/api/place/details/json', [ + 'query' => [ + 'fields' => 'opening_hours', + 'place_id' => $placeId, + 'key' => $googlePlaceApiKey + ] + ]); + } else { + $res['operating_hours'] = "N/A"; + } + } + } + + // Execute all the second requests concurrently + $detailResults = Utils::settle($detailPromises)->wait(); + + foreach ($restaurants as &$res) { + if (isset($detailResults[$res->name]['value'])) { + $response = $detailResults[$res->name]['value']; + $data = json_decode($response->getBody(), true); + if (isset($data['result']['opening_hours']['weekday_text'])) { + $hours = $data['result']['opening_hours']['weekday_text']; + Cache::put('restaurant_hours_' . $res->name, $hours, now()->addHours(24)); + $res['operating_hours'] = $hours; + } else { + $res['operating_hours'] = "N/A"; + } + } + } + + return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), $restaurants, 200); + } catch (Exception $ex) { + Log::error('List of Favourite Restaurant service failed : ' . $ex->getMessage()); + return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500); } +} + // public function DetailRestaurant($customerIamId, $id) @@ -308,8 +374,7 @@ class RestaurantApiServices ->pluck('restaurant_xid') ->toArray(); - $restaurantsQuery = ManageRestaurant::with('operatingHours') - ->where('is_active', '1') + $restaurantsQuery = ManageRestaurant::where('is_active', '1') ->whereIn('id', $customerFavouriteRestaurants); $searchData = $request->input('search_data'); @@ -326,10 +391,71 @@ class RestaurantApiServices } $restaurants = $restaurantsQuery->get(); + $client = new Client(); + $googlePlaceApiKey = config('constants.googlePlaces.api_key'); + $promises = []; + foreach ($restaurants as &$res) { $res['image'] = ListingImageUrl('restaurant_images', $res['image']); + + $cacheKey = 'restaurant_hours_' . $res->name; + if (Cache::has($cacheKey)) { + $res['operating_hours'] = Cache::get($cacheKey); + } else { + // Prepare the first request to get the place_id + $promises[$res->name] = $client->getAsync('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', [ + 'query' => [ + 'fields' => 'place_id', + 'input' => $res->name, + 'inputtype' => 'textquery', + 'key' => $googlePlaceApiKey + ] + ]); + } } + // Execute all the first requests concurrently + $results = Utils::settle($promises)->wait(); + + $detailPromises = []; + foreach ($restaurants as &$res) { + if (isset($results[$res->name]['value'])) { + $response = $results[$res->name]['value']; + $placeData = json_decode($response->getBody(), true); + + if (isset($placeData['candidates'][0]['place_id'])) { + $placeId = $placeData['candidates'][0]['place_id']; + + // Prepare the second request to get the operating hours + $detailPromises[$res->name] = $client->getAsync('https://maps.googleapis.com/maps/api/place/details/json', [ + 'query' => [ + 'fields' => 'opening_hours', + 'place_id' => $placeId, + 'key' => $googlePlaceApiKey + ] + ]); + } else { + $res['operating_hours'] = "N/A"; + } + } + } + + // Execute all the second requests concurrently + $detailResults = Utils::settle($detailPromises)->wait(); + + foreach ($restaurants as &$res) { + if (isset($detailResults[$res->name]['value'])) { + $response = $detailResults[$res->name]['value']; + $data = json_decode($response->getBody(), true); + if (isset($data['result']['opening_hours']['weekday_text'])) { + $hours = $data['result']['opening_hours']['weekday_text']; + Cache::put('restaurant_hours_' . $res->name, $hours, now()->addHours(24)); + $res['operating_hours'] = $hours; + } else { + $res['operating_hours'] = "N/A"; + } + } + } return jsonResponseWithSuccessMessageApi(__('auth.restaurant_search'), $restaurants, 200); } catch (Exception $ex) { @@ -341,6 +467,7 @@ class RestaurantApiServices } + public function redeemRestaurant($customerIamId, $request) { try {