This commit is contained in:
sayliraut
2024-07-15 17:25:58 +05:30
parent f15deee72b
commit ad40740362

View File

@@ -14,8 +14,13 @@ use App\Models\RestaurantTimeInterval;
use App\Models\TimeInterval;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\Utils;
class RestaurantApiServices
@@ -23,29 +28,103 @@ class RestaurantApiServices
public function getCoordinates($customerIamId)
{
try {
$restaurants = ManageRestaurant::with('operatingHours','closedRestaurant')->select(
'id',
'name',
'image',
'address',
'short_id',
'latitude',
'longtitude'
)
$perPage = request()->get('per_page', 10000);
$restaurants = ManageRestaurant::with('closedRestaurant')
->select(
'id',
'name',
'image',
'address',
'short_id',
'latitude',
'longtitude'
)
->where('is_active', '1')
->get()
->toArray();
->paginate($perPage);
// 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;
// $restaurant['operating_hours'] = getOpeningHoursOfRestaurant($restaurant['name']); // Will update later
// }
$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;
// $restaurant['operating_hours'] = getOpeningHoursOfRestaurant($restaurant['name']);// will update later
$cacheKey = 'restaurant_hours_' . $restaurant['name'];
if (Cache::has($cacheKey)) {
$restaurant['operating_hours'] = Cache::get($cacheKey);
} 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'],
'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";
}
}
}
// 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'];
Cache::put('restaurant_hours_' . $restaurant['name'], $hours, now()->addHours(24));
$restaurant['operating_hours'] = $hours;
} else {
$restaurant['operating_hours'] = "N/A";
}
}
}
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $restaurants, 200);
} catch (Exception $ex) {
Log::error('Restaurant Get service failed : ' . $ex->getMessage());
@@ -113,7 +192,7 @@ class RestaurantApiServices
public function DetailRestaurant($customerIamId, $id)
{
try {
$rest = ManageRestaurant::with('operatingHours')->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();
$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);
@@ -136,7 +215,7 @@ class RestaurantApiServices
$restTimeHours = $restTime->time_hours;
//this below code is updated by hritik on 12-07-2024 by adding restaurant opening hours dynamically from Google
// $rest->operating_hours = getOpeningHoursOfRestaurant($rest->name); //will update later
$rest->operating_hours = getOpeningHoursOfRestaurant($rest->name); //will update later
// $greaterTime = max($timeIntervalHours, $restTimeHours);
@@ -249,7 +328,7 @@ class RestaurantApiServices
if ($restaurantExist) {
return jsonResponseWithErrorMessageApi(__('auth.restaurant_already_redeemed'), 402);
return jsonResponseWithErrorMessageApi(__('auth.restaurant_already_redeemed'), 400);
}
$stateLimitation = TimeInterval::where('manage_state_xid', $restaurant->state_xid)->first();
@@ -270,7 +349,7 @@ class RestaurantApiServices
->count();
if ($redeemCountState >= $stateMaxLimitation) {
return jsonResponseWithErrorMessageApi(__('auth.state_limit_reached'), 402);
return jsonResponseWithErrorMessageApi(__('auth.state_limit_reached'), 404);
}
// Calculate the restaurant interval start date
@@ -283,7 +362,7 @@ class RestaurantApiServices
->count();
if ($redeemCountRestaurant >= $restaurantMaxLimitation) {
return jsonResponseWithErrorMessageApi(__('auth.restaurant_limit_reached'), 402);
return jsonResponseWithErrorMessageApi(__('auth.restaurant_limit_reached'), 404);
}
// Get the last redeem time
@@ -312,14 +391,14 @@ class RestaurantApiServices
$remainingTime = $currentTime->diff($stateAllowedRedeemTime);
$hours = $remainingTime->h;
$minutes = $remainingTime->i;
return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet') . " {$hours} hours and {$minutes} minutes remaining.", 402);
return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet') . " {$hours} hours and {$minutes} minutes remaining.", 404);
}
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.", 402);
return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet') . " {$hours} hours and {$minutes} minutes remaining.", 404);
}
}