From bab6c3324a4a507fb14a6c0e985dea2437ad6fbd Mon Sep 17 00:00:00 2001 From: sayliraut Date: Tue, 16 Jul 2024 12:50:13 +0530 Subject: [PATCH] changes --- app/Http/Helpers/Webhelper.php | 46 ++---- app/Jobs/FetchOperatingHours.php | 68 +++++++++ .../CustomerAPIs/RestaurantApiServices.php | 142 +++++++++--------- bootstrap/app.php | 1 + 4 files changed, 149 insertions(+), 108 deletions(-) create mode 100644 app/Jobs/FetchOperatingHours.php diff --git a/app/Http/Helpers/Webhelper.php b/app/Http/Helpers/Webhelper.php index 7d87aee..5abd564 100644 --- a/app/Http/Helpers/Webhelper.php +++ b/app/Http/Helpers/Webhelper.php @@ -8,11 +8,7 @@ use Illuminate\Support\Facades\Http; use Tymon\JWTAuth\Facades\JWTAuth; use GuzzleHttp\Client; -/** - * Created By : sayli raut - * Created at : 24 Jan 2024 - * Use : Json response with success message for API - */ + if (!function_exists('jsonResponseWithSuccessMessageApi')) { function jsonResponseWithSuccessMessageApi($message, $data = [], $statusCode = 200) { @@ -33,11 +29,7 @@ if (!function_exists('jsonResponseWithSuccessMessageApi')) { } } -/** - * Created By : sayli raut - * Created at : 24 jan 2024 - * Use : Json response with error message for API - */ + if (!function_exists('jsonResponseWithErrorMessageApi')) { function jsonResponseWithErrorMessageApi($errorMessage, $statusCode = 500) { @@ -57,11 +49,7 @@ if (!function_exists('jsonResponseWithErrorMessageApi')) { } } -/** - * Created by : sayli raut - * Created at : 24 Jan 2024 - * Use : To return error json response for admin - */ + if (!function_exists('jsonResponseWithErrorMessage')) { function jsonResponseWithErrorMessage($errorMessage) { @@ -76,11 +64,7 @@ if (!function_exists('jsonResponseWithErrorMessage')) { } } -/** - * Created by : sayli raut - * Created at : 24 Jan 2024 - * Use : To return success json response for admin - */ + if (!function_exists('jsonResponseWithSuccessMessage')) { function jsonResponseWithSuccessMessage($message, $data = []) { @@ -99,11 +83,7 @@ if (!function_exists('jsonResponseWithSuccessMessage')) { } } -/** - * Created by : Pradyumn Dwivedi - * Created On : 11-May-2022 - * Uses: This function will be used to full search data in api. - */ + if (!function_exists('fullSearchQuery')) { function fullSearchQuery($query, $word, $columns) { @@ -117,11 +97,7 @@ if (!function_exists('fullSearchQuery')) { } } -/** - * Created by : sayli raut - * Created at : 24 Feb 2024 - * Use : To check and validate to customer token - */ + if (!function_exists('readHeaderToken')) { function readHeaderToken() { @@ -144,11 +120,7 @@ if (!function_exists('readHeaderToken')) { } } -/** - * Created by : sayli raut - * Created at : 24 jan 2024 - * Use : To check and validate login restaurant user token - */ + if (!function_exists('readRestHeaderToken')) { function readRestHeaderToken() { @@ -193,7 +165,7 @@ if (!function_exists('readRestHeaderToken')) { if (!function_exists('getOpeningHoursOfRestaurant')) { function getOpeningHoursOfRestaurant($restaurantName) { - + $googlePlaceApiKey = config('constants.googlePlaces.api_key'); // Your webhook secret key // dd($googlePlaceApiKey); @@ -209,7 +181,7 @@ if (!function_exists('getOpeningHoursOfRestaurant')) { ] ]); $placeData = json_decode($response->getBody(), true); - + if (isset($placeData['candidates'][0]['place_id'])) { $placeId = $placeData['candidates'][0]['place_id']; // return $placeId; diff --git a/app/Jobs/FetchOperatingHours.php b/app/Jobs/FetchOperatingHours.php new file mode 100644 index 0000000..775ecdb --- /dev/null +++ b/app/Jobs/FetchOperatingHours.php @@ -0,0 +1,68 @@ +restaurantName = $restaurantName; + $this->googlePlaceApiKey = $googlePlaceApiKey; + } + + public function handle() + { + $client = new Client(); + $url = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json'; + + $response = $client->get($url, [ + 'query' => [ + 'fields' => 'place_id', + 'input' => $this->restaurantName, + 'inputtype' => 'textquery', + 'key' => $this->googlePlaceApiKey + ] + ]); + $placeData = json_decode($response->getBody(), true); + + if (isset($placeData['candidates'][0]['place_id'])) { + $placeId = $placeData['candidates'][0]['place_id']; + + $url = 'https://maps.googleapis.com/maps/api/place/details/json'; + $response = $client->get($url, [ + 'query' => [ + 'fields' => 'opening_hours', + 'place_id' => $placeId, + 'key' => $this->googlePlaceApiKey + ] + ]); + + $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_' . $this->restaurantName, $hours, now()->addHours(24)); + } else { + Cache::put('restaurant_hours_' . $this->restaurantName, "N/A", now()->addHours(24)); + } + } else { + Cache::put('restaurant_hours_' . $this->restaurantName, "N/A", now()->addHours(24)); + } + } +} diff --git a/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php b/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php index 2828db8..f708778 100644 --- a/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php +++ b/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php @@ -16,7 +16,7 @@ use Carbon\Carbon; use Exception; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; - +use App\Jobs\FetchOperatingHours; use GuzzleHttp\Client; use GuzzleHttp\Promise; use GuzzleHttp\Promise\Utils; @@ -177,96 +177,96 @@ class RestaurantApiServices } } -// -public function DetailRestaurant($customerIamId, $id) -{ - try { - $rest = ManageRestaurant::select('id', 'short_id', 'name', 'description', 'restaurant_id', 'address', 'image', 'bio', 'try_on_1', 'try_on_2', 'try_on_3', 'try_on_4', 'exclusion', 'latitude', 'longtitude', 'state_xid') - ->where('short_id', $id) - ->where('is_active', '1') - ->first(); + // + public function DetailRestaurant($customerIamId, $id) + { + try { + $rest = ManageRestaurant::select('id', 'short_id', 'name', 'description', 'restaurant_id', 'address', 'image', 'bio', 'try_on_1', 'try_on_2', 'try_on_3', 'try_on_4', 'exclusion', 'latitude', 'longtitude', 'state_xid') + ->where('short_id', $id) + ->where('is_active', '1') + ->first(); - if ($rest) { - $rest->image = ListingImageUrl('restaurant_images', $rest->image); + if ($rest) { + $rest->image = ListingImageUrl('restaurant_images', $rest->image); - $isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId) - ->where('restaurant_xid', $rest->id) - ->exists(); + $isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId) + ->where('restaurant_xid', $rest->id) + ->exists(); $rest->is_favourite = $isFavourite; $redeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId) - ->where('manage_restaurants_xid', $rest->id) - ->where('is_redeem', "1") - ->first(); + ->where('manage_restaurants_xid', $rest->id) + ->where('is_redeem', "1") + ->first(); - $restTime = RestaurantTimeInterval::where('manage_restaurants_xid', $rest->id)->first(); - $restTimeHours = $restTime->time_hours; + $restTime = RestaurantTimeInterval::where('manage_restaurants_xid', $rest->id)->first(); + $restTimeHours = $restTime->time_hours; - // Initialize Guzzle HTTP client - $client = new Client(); - $googlePlaceApiKey = config('constants.googlePlaces.api_key'); + // Initialize Guzzle HTTP client + $client = new Client(); + $googlePlaceApiKey = config('constants.googlePlaces.api_key'); - // Cache key for operating hours - $cacheKey = 'restaurant_hours_' . $rest->name; - if (Cache::has($cacheKey)) { - $rest->operating_hours = Cache::get($cacheKey); - } else { - // Prepare the first request to get the place_id - $placeResponse = $client->get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', [ - 'query' => [ - 'fields' => 'place_id', - 'input' => $rest->name, - 'inputtype' => 'textquery', - 'key' => $googlePlaceApiKey - ] - ]); - $placeData = json_decode($placeResponse->getBody(), true); - - if (isset($placeData['candidates'][0]['place_id'])) { - $placeId = $placeData['candidates'][0]['place_id']; - - // Prepare the second request to get the operating hours - $detailResponse = $client->get('https://maps.googleapis.com/maps/api/place/details/json', [ + // Cache key for operating hours + $cacheKey = 'restaurant_hours_' . $rest->name; + if (Cache::has($cacheKey)) { + $rest->operating_hours = Cache::get($cacheKey); + } else { + // Prepare the first request to get the place_id + $placeResponse = $client->get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', [ 'query' => [ - 'fields' => 'opening_hours', - 'place_id' => $placeId, + 'fields' => 'place_id', + 'input' => $rest->name, + 'inputtype' => 'textquery', 'key' => $googlePlaceApiKey ] ]); - $detailData = json_decode($detailResponse->getBody(), true); + $placeData = json_decode($placeResponse->getBody(), true); - if (isset($detailData['result']['opening_hours']['weekday_text'])) { - $hours = $detailData['result']['opening_hours']['weekday_text']; - Cache::put($cacheKey, $hours, now()->addHours(24)); - $rest->operating_hours = $hours; + if (isset($placeData['candidates'][0]['place_id'])) { + $placeId = $placeData['candidates'][0]['place_id']; + + // Prepare the second request to get the operating hours + $detailResponse = $client->get('https://maps.googleapis.com/maps/api/place/details/json', [ + 'query' => [ + 'fields' => 'opening_hours', + 'place_id' => $placeId, + 'key' => $googlePlaceApiKey + ] + ]); + $detailData = json_decode($detailResponse->getBody(), true); + + if (isset($detailData['result']['opening_hours']['weekday_text'])) { + $hours = $detailData['result']['opening_hours']['weekday_text']; + Cache::put($cacheKey, $hours, now()->addHours(24)); + $rest->operating_hours = $hours; + } else { + $rest->operating_hours = "N/A"; + } } else { $rest->operating_hours = "N/A"; } + } + + if ($redeem) { + $rest->is_Redeemed = true; + $rest->redeem_date = \Carbon\Carbon::parse($redeem->redeem_date)->addHours($restTimeHours)->toDateTimeString(); } else { - $rest->operating_hours = "N/A"; + $rest->is_Redeemed = false; + $rest->redeem_date = null; } } - if ($redeem) { - $rest->is_Redeemed = true; - $rest->redeem_date = \Carbon\Carbon::parse($redeem->redeem_date)->addHours($restTimeHours)->toDateTimeString(); - } else { - $rest->is_Redeemed = false; - $rest->redeem_date = null; + if (!$rest) { + return jsonResponseWithErrorMessage(__('auth.restaurant_data_not_found'), 404); } - } - if (!$rest) { - return jsonResponseWithErrorMessage(__('auth.restaurant_data_not_found'), 404); + return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $rest, 200); + } catch (Exception $e) { + Log::error("Error fetching restaurant data: " . $e->getMessage()); + return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); } - - return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $rest, 200); - } catch (Exception $e) { - Log::error("Error fetching restaurant data: " . $e->getMessage()); - return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); } -} @@ -360,7 +360,7 @@ public function DetailRestaurant($customerIamId, $id) if ($restaurantExist) { - return jsonResponseWithErrorMessageApi(__('auth.restaurant_already_redeemed'), 400); + return jsonResponseWithErrorMessageApi(__('auth.restaurant_already_redeemed'), 403); } $stateLimitation = TimeInterval::where('manage_state_xid', $restaurant->state_xid)->first(); @@ -381,7 +381,7 @@ public function DetailRestaurant($customerIamId, $id) ->count(); if ($redeemCountState >= $stateMaxLimitation) { - return jsonResponseWithErrorMessageApi(__('auth.state_limit_reached'), 404); + return jsonResponseWithErrorMessageApi(__('auth.state_limit_reached'), 403); } // Calculate the restaurant interval start date @@ -394,7 +394,7 @@ public function DetailRestaurant($customerIamId, $id) ->count(); if ($redeemCountRestaurant >= $restaurantMaxLimitation) { - return jsonResponseWithErrorMessageApi(__('auth.restaurant_limit_reached'), 404); + return jsonResponseWithErrorMessageApi(__('auth.restaurant_limit_reached'), 403); } // Get the last redeem time @@ -423,14 +423,14 @@ public function DetailRestaurant($customerIamId, $id) $remainingTime = $currentTime->diff($stateAllowedRedeemTime); $hours = $remainingTime->h; $minutes = $remainingTime->i; - return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet') . " {$hours} hours and {$minutes} minutes remaining.", 404); + return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet') . " {$hours} hours and {$minutes} minutes remaining.", 403); } if ($currentTime < $restAllowedRedeemTime) { $remainingTime = $currentTime->diff($restAllowedRedeemTime); $hours = $remainingTime->h; $minutes = $remainingTime->i; - return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet') . " {$hours} hours and {$minutes} minutes remaining.", 404); + return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet') . " {$hours} hours and {$minutes} minutes remaining.", 403); } } diff --git a/bootstrap/app.php b/bootstrap/app.php index d91bae7..a70d0a6 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -38,6 +38,7 @@ return Application::configure(basePath: dirname(__DIR__)) ->withSchedule(function (Schedule $schedule) { $schedule->command('app:reinstate-restaurant')->everyMinute(); $schedule->command('app:sent-schedule-notification')->everyMinute(); + $schedule->command('queue:work')->everyMinute(); }) ->withExceptions(function (Exceptions $exceptions) { //