Merge pull request #436 from WDI-Ideas/sayli

Sayli
This commit is contained in:
Sayli Raut
2024-08-07 19:15:02 +05:30
committed by GitHub
2 changed files with 286 additions and 412 deletions

View File

@@ -5,9 +5,10 @@ use App\Models\IamPrincipal;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Tymon\JWTAuth\Facades\JWTAuth;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
if (!function_exists('jsonResponseWithSuccessMessageApi')) {
function jsonResponseWithSuccessMessageApi($message, $data = [], $statusCode = 200)
@@ -233,12 +234,75 @@ if (!function_exists('getOpeningHoursOfRestaurant')) {
} else {
$hours = "N/A";
}
return $hours ;
return $hours;
// dd($data);
// return response()->json($data);
}
if (!function_exists('getOperatingHours')) {
function getOperatingHours($placeName, $address)
{
$client = new Client();
$googlePlaceApiKey = config('constants.googlePlaces.api_key');
$cacheKey = 'restaurant_hours_' . $placeName;
$cacheKeyIsRestaurantOpen = 'is_restaurant_open_now_' . $placeName;
if (Cache::has($cacheKey)) {
return [
'operating_hours' => Cache::get($cacheKey),
'is_open_now' => Cache::get($cacheKeyIsRestaurantOpen),
];
}
try {
// Get place_id
$placeResponse = $client->get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', [
'query' => [
'fields' => 'place_id',
'input' => $placeName . ' ' . $address,
'inputtype' => 'textquery',
'key' => $googlePlaceApiKey
]
]);
$placeData = json_decode($placeResponse->getBody(), true);
if (isset($placeData['candidates'][0]['place_id'])) {
$placeId = $placeData['candidates'][0]['place_id'];
// Get 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'];
$isOpenNow = $detailData['result']['opening_hours']['open_now'];
Cache::put($cacheKey, $hours, now()->addHours(2));
Cache::put($cacheKeyIsRestaurantOpen, $isOpenNow, now()->addHours(2));
return [
'operating_hours' => $hours,
'is_open_now' => $isOpenNow,
];
}
}
} catch (\Exception $e) {
Log::error('Restaurant Get service failed : ' . $e->getMessage());
}
return [
'operating_hours' => "N/A",
'is_open_now' => false,
];
}
}
}

View File

@@ -30,22 +30,9 @@ class RestaurantApiServices
try {
// Fetch all active restaurants
$restaurants = ManageRestaurant::with('closedRestaurant')
->select(
'id',
'name',
'image',
'address',
'short_id',
'latitude',
'longtitude',
'state_xid'
)
->select('id', 'name', 'image', 'address', 'short_id', 'latitude', 'longtitude', 'state_xid')
->where('is_active', '1')->get();
$client = new Client();
$promises = [];
$googlePlaceApiKey = config('constants.googlePlaces.api_key');
foreach ($restaurants as &$restaurant) {
$restaurant['image'] = ListingImageUrl('restaurant_images', $restaurant['image']);
$isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
@@ -53,126 +40,58 @@ class RestaurantApiServices
->exists();
$restaurant['is_favourite'] = $isFavourite;
$cacheKey = 'restaurant_hours_' . $restaurant['name'];
$cacheKeyIsRestaurantOpen = 'is_restaurant_open_now_' . $restaurant['name'];
$hoursData = getOperatingHours($restaurant['name'], $restaurant['address']);
$restaurant['operating_hours'] = $hoursData['operating_hours'];
$restaurant['is_restaurant_open_now_as_per_google'] = $hoursData['is_open_now'];
if (Cache::has($cacheKey)) {
$restaurant['operating_hours'] = Cache::get($cacheKey);
$restaurant['is_restaurant_open_now_as_per_google'] = Cache::get($cacheKeyIsRestaurantOpen);
} else {
// Prepare the first request to get the place_id
$promises[$restaurant['name']] = $client->getAsync('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', [
'query' => [
'fields' => 'place_id',
'input' => $restaurant['name'] .' ' . $restaurant['address'],
'inputtype' => 'textquery',
'key' => $googlePlaceApiKey
]
]);
}
}
// Execute all the first requests concurrently
$results = Utils::settle($promises)->wait();
$detailPromises = [];
foreach ($restaurants as &$restaurant) {
if (isset($results[$restaurant['name']]['value'])) {
$response = $results[$restaurant['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[$restaurant['name']] = $client->getAsync('https://maps.googleapis.com/maps/api/place/details/json', [
'query' => [
'fields' => 'opening_hours',
'open_now',
'place_id' => $placeId,
'key' => $googlePlaceApiKey
]
]);
} else {
$restaurant['operating_hours'] = "N/A";
$restaurant['is_restaurant_open_now_as_per_google'] = false;
}
}
}
// Execute all the second requests concurrently
$detailResults = Utils::settle($detailPromises)->wait();
foreach ($restaurants as &$restaurant) {
if (isset($detailResults[$restaurant['name']]['value'])) {
$response = $detailResults[$restaurant['name']]['value'];
$data = json_decode($response->getBody(), true);
if (isset($data['result']['opening_hours']['weekday_text']) && isset($data['result']['opening_hours']['open_now'])) {
$hours = $data['result']['opening_hours']['weekday_text'];
$isOpenNow = $data['result']['opening_hours']['open_now'];
Cache::put('restaurant_hours_' . $restaurant['name'], $hours, now()->addHours(2));
Cache::put('is_restaurant_open_now_' . $restaurant['name'], $isOpenNow, now()->addHours(2));
$restaurant['operating_hours'] = $hours;
$restaurant['is_restaurant_open_now_as_per_google'] = $isOpenNow;
} else {
$restaurant['operating_hours'] = "N/A";
$restaurant['is_restaurant_open_now_as_per_google'] = false;
}
}
// Calculate next redemption time
$lastRedeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
->where('manage_restaurants_xid', $restaurant->id)
->where('manage_restaurants_xid', $restaurant->id)
->where('is_redeem', 1)
->orderBy('redeem_date', 'desc')
->first();
$restTime = RestaurantTimeInterval::select('time_hours')->where('manage_restaurants_xid', $restaurant->id)->first();
$stateTime = TimeInterval::select('time_hours')->where('manage_state_xid', $restaurant->state_xid)->first();
$restTimeHours = $restTime ? $restTime->time_hours : 0;
$stateTimeHours = $stateTime ? $stateTime->time_hours : 0;
$restaurant['next_redeem_time'] = "Now";
$currentTime = Carbon::now();
if ($lastRedeem) {
$lastRedeemTime = Carbon::parse($lastRedeem->redeem_date);
$restAllowedRedeemTime = $lastRedeemTime->copy()->addHours(intval($restTimeHours));
$stateAllowedRedeemTime = $lastRedeemTime->copy()->addHours(intval($stateTimeHours));
if ($currentTime < $restAllowedRedeemTime) {
$restaurant['next_redeem_time'] = $restAllowedRedeemTime->toDateTimeString();
} else if ($currentTime < $stateAllowedRedeemTime) {
$restaurant['next_redeem_time'] = $stateAllowedRedeemTime->toDateTimeString();
}
}
// Include logic for preventing redemption in the same state
if ($restaurant['next_redeem_time'] === "Now") {
// Fetch the last redeem time for any restaurant in the same state
$lastStateRedeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
->where('state_xid', $restaurant->state_xid)
->where('is_redeem', 1)
->orderBy('redeem_date', 'desc')
->first();
$restTime = RestaurantTimeInterval::select('time_hours')->where('manage_restaurants_xid', $restaurant->id)->first();
$stateTime = TimeInterval::select('time_hours')->where('manage_state_xid', $restaurant->state_xid)->first();
if ($lastStateRedeem) {
$lastStateRedeemTime = Carbon::parse($lastStateRedeem->redeem_date);
$stateAllowedRedeemTime = $lastStateRedeemTime->copy()->addHours(intval($stateTimeHours));
$restTimeHours = $restTime ? $restTime->time_hours : 0;
$stateTimeHours = $stateTime ? $stateTime->time_hours : 0;
$restaurant['next_redeem_time'] = "Now";
$currentTime = Carbon::now();
if ($lastRedeem) {
$lastRedeemTime = Carbon::parse($lastRedeem->redeem_date);
$restAllowedRedeemTime = $lastRedeemTime->copy()->addHours(intval($restTimeHours));
$stateAllowedRedeemTime = $lastRedeemTime->copy()->addHours(intval($stateTimeHours));
if ($currentTime < $restAllowedRedeemTime) {
$restaurant['next_redeem_time'] = $restAllowedRedeemTime->toDateTimeString();
} else if ($currentTime < $stateAllowedRedeemTime) {
if ($currentTime < $stateAllowedRedeemTime) {
$restaurant['next_redeem_time'] = $stateAllowedRedeemTime->toDateTimeString();
}
}
// Include logic for preventing redemption in the same state
if ($restaurant['next_redeem_time'] === "Now") {
// Fetch the last redeem time for any restaurant in the same state
$lastStateRedeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
->where('state_xid', $restaurant->state_xid)
->where('is_redeem', 1)
->orderBy('redeem_date', 'desc')
->first();
if ($lastStateRedeem) {
$lastStateRedeemTime = Carbon::parse($lastStateRedeem->redeem_date);
$stateAllowedRedeemTime = $lastStateRedeemTime->copy()->addHours(intval($stateTimeHours));
if ($currentTime < $stateAllowedRedeemTime) {
$restaurant['next_redeem_time'] = $stateAllowedRedeemTime->toDateTimeString();
}
}
}
}
} }
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $restaurants, 200);
} catch (Exception $ex) {
@@ -184,6 +103,7 @@ class RestaurantApiServices
public function addToFavourite($customerIamId, $request)
{
try {
@@ -218,7 +138,6 @@ class RestaurantApiServices
}
public function listFavRestaurant($customerIamId)
{
try {
@@ -232,81 +151,16 @@ class RestaurantApiServices
->whereIn('id', $customerFavouriteRestaurants)
->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;
$cacheKeyIsRestaurantOpen = 'is_restaurant_open_now_' . $res->name;
if (Cache::has($cacheKey)) {
$res['operating_hours'] = Cache::get($cacheKey);
$res['is_restaurant_open_now_as_per_google'] = Cache::get($cacheKeyIsRestaurantOpen);
} 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 . ' ' . $res->address,
'inputtype' => 'textquery',
'key' => $googlePlaceApiKey
]
]);
}
}
// Fetch operating hours and open status using the helper function
$data = getOperatingHours($res->name, $res->address);
// 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";
$res['is_restaurant_open_now_as_per_google'] = false;
}
}
}
// 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'];
$isOpenNow = $data['result']['opening_hours']['open_now'];
Cache::put('restaurant_hours_' . $res->name, $hours, now()->addHours(2));
Cache::put('is_restaurant_open_now_' . $res->name, $isOpenNow, now()->addHours(2));
$res['operating_hours'] = $hours;
$res['is_restaurant_open_now_as_per_google'] = $isOpenNow;
} else {
$res['operating_hours'] = "N/A";
$res['is_restaurant_open_now_as_per_google'] = false;
}
}
$res['operating_hours'] = $data['operating_hours'];
$res['is_restaurant_open_now_as_per_google'] = $data['is_open_now'];
}
return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), $restaurants, 200);
@@ -317,11 +171,161 @@ class RestaurantApiServices
}
//
// public function DetailRestaurant($customerIamId, $id)
// {
// try {
// $rest = ManageRestaurant::with('closedRestaurant')->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);
// $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();
// $restTime = RestaurantTimeInterval::where('manage_restaurants_xid', $rest->id)->first();
// $restTimeHours = $restTime ? $restTime->time_hours : 0;
// // Initialize Guzzle HTTP client
// $client = new Client();
// $googlePlaceApiKey = config('constants.googlePlaces.api_key');
// // Cache key for operating hours
// $cacheKey = 'restaurant_hours_' . $rest->name;
// $cacheKeyIsRestaurantOpen = 'is_restaurant_open_now_' . $rest->name;
// if (Cache::has($cacheKey)) {
// $rest->operating_hours = Cache::get($cacheKey);
// $rest->is_restaurant_open_now_as_per_google = Cache::get($cacheKeyIsRestaurantOpen);
// } 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 . ' ' . $rest->address,
// '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', [
// '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'];
// $isOpenNow = $detailData['result']['opening_hours']['open_now'];
// Cache::put($cacheKey, $hours, now()->addHours(2));
// Cache::put($cacheKeyIsRestaurantOpen, $isOpenNow, now()->addHours(2));
// $rest->operating_hours = $hours;
// $rest->is_restaurant_open_now_as_per_google = $isOpenNow;
// } else {
// $rest->operating_hours = "N/A";
// $rest->is_restaurant_open_now_as_per_google = false;
// }
// } else {
// $rest->operating_hours = "N/A";
// $rest->is_restaurant_open_now_as_per_google = false;
// }
// }
// if ($redeem) {
// $rest->is_Redeemed = true;
// $rest->redeem_date = \Carbon\Carbon::parse($redeem->redeem_date)->addHours(intval($restTimeHours))->toDateTimeString();
// } else {
// $rest->is_Redeemed = false;
// $rest->redeem_date = null;
// }
// // Calculate next redemption time
// $lastRedeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
// ->where('manage_restaurants_xid', $rest->id)
// ->where('is_redeem', 1)
// ->orderBy('redeem_date', 'desc')
// ->first();
// $stateTime = TimeInterval::select('time_hours')->where('manage_state_xid', $rest->state_xid)->first();
// $stateTimeHours = $stateTime ? $stateTime->time_hours : 0;
// $currentTime = Carbon::now();
// $nextRedeemTime = "Now";
// if ($lastRedeem) {
// $lastRedeemTime = Carbon::parse($lastRedeem->redeem_date);
// $restAllowedRedeemTime = $lastRedeemTime->copy()->addHours(intval($restTimeHours));
// $stateAllowedRedeemTime = $lastRedeemTime->copy()->addHours(intval($stateTimeHours));
// if ($currentTime < $restAllowedRedeemTime) {
// $nextRedeemTime = $restAllowedRedeemTime->toDateTimeString();
// } else if ($currentTime < $stateAllowedRedeemTime) {
// $nextRedeemTime = $stateAllowedRedeemTime->toDateTimeString();
// }
// }
// // Include logic for preventing redemption in the same state
// if ($nextRedeemTime === "Now") {
// $lastStateRedeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
// ->where('state_xid', $rest->state_xid)
// ->where('is_redeem', 1)
// ->orderBy('redeem_date', 'desc')
// ->first();
// if ($lastStateRedeem) {
// $lastStateRedeemTime = Carbon::parse($lastStateRedeem->redeem_date);
// $stateAllowedRedeemTime = $lastStateRedeemTime->copy()->addHours(intval($stateTimeHours));
// if ($currentTime < $stateAllowedRedeemTime) {
// $nextRedeemTime = $stateAllowedRedeemTime->toDateTimeString();
// }
// }
// }
// $rest->next_redeem_time = $nextRedeemTime;
// }
// 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);
// }
// }
public function DetailRestaurant($customerIamId, $id)
{
try {
$rest = ManageRestaurant::with('closedRestaurant')->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')
$rest = ManageRestaurant::with('closedRestaurant')
->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();
@@ -332,7 +336,6 @@ class RestaurantApiServices
$isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
->where('restaurant_xid', $rest->id)
->exists();
$rest->is_favourite = $isFavourite;
$redeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
@@ -340,66 +343,15 @@ class RestaurantApiServices
->where('is_redeem', "1")
->first();
$hoursData = getOperatingHours($rest->name, $rest->address);
$rest->operating_hours = $hoursData['operating_hours'];
$rest->is_restaurant_open_now_as_per_google = $hoursData['is_open_now'];
$restTime = RestaurantTimeInterval::where('manage_restaurants_xid', $rest->id)->first();
$restTimeHours = $restTime ? $restTime->time_hours : 0;
// Initialize Guzzle HTTP client
$client = new Client();
$googlePlaceApiKey = config('constants.googlePlaces.api_key');
// Cache key for operating hours
$cacheKey = 'restaurant_hours_' . $rest->name;
$cacheKeyIsRestaurantOpen = 'is_restaurant_open_now_' . $rest->name;
if (Cache::has($cacheKey)) {
$rest->operating_hours = Cache::get($cacheKey);
$rest->is_restaurant_open_now_as_per_google = Cache::get($cacheKeyIsRestaurantOpen);
} 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 . ' ' . $rest->address,
'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', [
'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'];
$isOpenNow = $detailData['result']['opening_hours']['open_now'];
Cache::put($cacheKey, $hours, now()->addHours(2));
Cache::put($cacheKeyIsRestaurantOpen, $isOpenNow, now()->addHours(2));
$rest->operating_hours = $hours;
$rest->is_restaurant_open_now_as_per_google = $isOpenNow;
} else {
$rest->operating_hours = "N/A";
$rest->is_restaurant_open_now_as_per_google = false;
}
} else {
$rest->operating_hours = "N/A";
$rest->is_restaurant_open_now_as_per_google = false;
}
}
if ($redeem) {
$rest->is_Redeemed = true;
$rest->redeem_date = \Carbon\Carbon::parse($redeem->redeem_date)->addHours(intval($restTimeHours))->toDateTimeString();
@@ -408,7 +360,6 @@ class RestaurantApiServices
$rest->redeem_date = null;
}
// Calculate next redemption time
$lastRedeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
->where('manage_restaurants_xid', $rest->id)
->where('is_redeem', 1)
@@ -455,6 +406,7 @@ class RestaurantApiServices
$rest->next_redeem_time = $nextRedeemTime;
}
if (!$rest) {
return jsonResponseWithErrorMessage(__('auth.restaurant_data_not_found'), 404);
}
@@ -467,10 +419,6 @@ class RestaurantApiServices
}
public function removeFromFavourite($customerIamId, $request)
{
try {
@@ -504,10 +452,12 @@ class RestaurantApiServices
public function searchFromFavourite($customerIamId, $request)
{
try {
// Get list of favorite restaurant IDs
$customerFavouriteRestaurants = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
->pluck('restaurant_xid')
->toArray();
// Fetch the restaurant details
$restaurantsQuery = ManageRestaurant::with('closedRestaurant')->where('is_active', '1')
->whereIn('id', $customerFavouriteRestaurants);
@@ -525,83 +475,14 @@ 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;
$cacheKeyIsRestaurantOpen = 'is_restaurant_open_now_' . $res->name;
$data = getOperatingHours($res->name, $res->address);
if (Cache::has($cacheKey)) {
$res['operating_hours'] = Cache::get($cacheKey);
$res['is_restaurant_open_now_as_per_google'] = Cache::get($cacheKeyIsRestaurantOpen);
} 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 . ' ' . $res->address,
'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";
$res['is_restaurant_open_now_as_per_google'] = false;
}
}
}
// 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'];
$isOpenNow = $data['result']['opening_hours']['open_now'];
Cache::put('restaurant_hours_' . $res->name, $hours, now()->addHours(2));
Cache::put('is_restaurant_open_now_' . $res->name, $isOpenNow, now()->addHours(2));
$res['operating_hours'] = $hours;
$res['is_restaurant_open_now_as_per_google'] = $isOpenNow;
} else {
$res['operating_hours'] = "N/A";
$res['is_restaurant_open_now_as_per_google'] = false;
}
}
$res['operating_hours'] = $data['operating_hours'];
$res['is_restaurant_open_now_as_per_google'] = $data['is_open_now'];
}
return jsonResponseWithSuccessMessageApi(__('auth.restaurant_search'), $restaurants, 200);
@@ -820,94 +701,23 @@ class RestaurantApiServices
$restaurants = $restaurantsQuery->get();
$client = new Client();
$promises = [];
$googlePlaceApiKey = config('constants.googlePlaces.api_key');
foreach ($restaurants as &$restaurant) {
$restaurant['image'] = ListingImageUrl('restaurant_images', $restaurant['image']);
$isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
->where('restaurant_xid', $restaurant['id'])
->exists();
$restaurant['is_favourite'] = $isFavourite;
$cacheKey = 'restaurant_hours_' . $restaurant['name'];
$cacheKeyIsRestaurantOpen = 'is_restaurant_open_now_' . $restaurant['name'];
$data = getOperatingHours($restaurant->name, $restaurant->address);
if (Cache::has($cacheKey)) {
$restaurant['operating_hours'] = Cache::get($cacheKey);
$restaurant['is_restaurant_open_now_as_per_google'] = Cache::get($cacheKeyIsRestaurantOpen);
} else {
// Prepare the first request to get the place_id
$promises[$restaurant['name']] = $client->getAsync('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', [
'query' => [
'fields' => 'place_id',
'input' => $restaurant['name'] . ' ' . $restaurant['address'],
'inputtype' => 'textquery',
'key' => $googlePlaceApiKey
]
]);
}
}
// Execute all the first requests concurrently
$results = Utils::settle($promises)->wait();
$detailPromises = [];
foreach ($restaurants as &$restaurant) {
if (isset($results[$restaurant['name']]['value'])) {
$response = $results[$restaurant['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[$restaurant['name']] = $client->getAsync('https://maps.googleapis.com/maps/api/place/details/json', [
'query' => [
'fields' => 'opening_hours',
'place_id' => $placeId,
'key' => $googlePlaceApiKey
]
]);
} else {
$restaurant['operating_hours'] = "N/A";
$restaurant['is_restaurant_open_now_as_per_google'] = false;
}
}
}
// Execute all the second requests concurrently
$detailResults = Utils::settle($detailPromises)->wait();
foreach ($restaurants as &$restaurant) {
if (isset($detailResults[$restaurant['name']]['value'])) {
$response = $detailResults[$restaurant['name']]['value'];
$data = json_decode($response->getBody(), true);
if (isset($data['result']['opening_hours']['weekday_text'])) {
$hours = $data['result']['opening_hours']['weekday_text'];
$isOpenNow = $data['result']['opening_hours']['open_now'];
Cache::put('restaurant_hours_' . $restaurant['name'], $hours, now()->addHours(2));
Cache::put('is_restaurant_open_now_' . $restaurant['name'], $isOpenNow, now()->addHours(2));
$restaurant['operating_hours'] = $hours;
$restaurant['is_restaurant_open_now_as_per_google'] = $isOpenNow;
} else {
$restaurant['operating_hours'] = "N/A";
$restaurant['is_restaurant_open_now_as_per_google'] = false;
}
}
$restaurant['operating_hours'] = $data['operating_hours'];
$restaurant['is_restaurant_open_now_as_per_google'] = $data['is_open_now'];
}
return jsonResponseWithSuccessMessageApi(__('auth.restaurant_search'), $restaurants, 200);
} catch (Exception $ex) {
Log::error('Search from restaurant service failed: ' . $ex->getMessage());
Log::error('Search restaurant service failed: ' . $ex->getMessage());
return response()->json([
'message' => __('auth.something_went_wrong')
], 500);