redeem logic
This commit is contained in:
@@ -8,7 +8,10 @@ use App\Models\IamPrincipal;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Carbon\Carbon;
|
||||
use App\Models\ManageRestaurant;
|
||||
use App\Models\ManageState;
|
||||
use App\Models\RedeemRestaurant;
|
||||
use App\Models\RestaurantTimeInterval;
|
||||
use App\Models\TimeInterval;
|
||||
|
||||
class ReinstateRestaurant extends Command
|
||||
{
|
||||
@@ -36,43 +39,62 @@ class ReinstateRestaurant extends Command
|
||||
$recordsToUpdate = RedeemRestaurant::where('is_redeem', 1)->get();
|
||||
|
||||
foreach ($recordsToUpdate as $record) {
|
||||
//find restaurant
|
||||
$managerestaurant = ManageRestaurant::where('id', $record->manage_restaurants_xid)->first();
|
||||
$restTime = RestaurantTimeInterval::select('time_hours')->where('manage_restaurants_xid', $managerestaurant->id)->first();
|
||||
$stateTime = TimeInterval::select('time_hours')->where('manage_state_xid', $managerestaurant->state_xid)->first();
|
||||
|
||||
$customerData = IamPrincipal::where('id', $record->iam_principal_xid)->where('notification_status', 1)->where('principal_type_xid', 3)->first(); //fetch customer
|
||||
$restTimeHours = $restTime ? $restTime->time_hours : 0;
|
||||
$stateTimeHours = $stateTime ? $stateTime->time_hours : 0;
|
||||
|
||||
Log::info('Rest time interval time_hours: ' . $restTimeHours);
|
||||
Log::info('State time interval time_hours: ' . $stateTimeHours);
|
||||
|
||||
$customerData = IamPrincipal::where('id', $record->iam_principal_xid)
|
||||
->where('notification_status', 1)
|
||||
->where('principal_type_xid', 3)
|
||||
->first();
|
||||
|
||||
if ($managerestaurant && $managerestaurant->is_active == 1) {
|
||||
$redeemDate = Carbon::parse($record->redeem_date);
|
||||
|
||||
$fourHourPlusTimeOfRecord = $redeemDate->copy()->addHours(4);
|
||||
// Calculate the state time plus record time
|
||||
$stateHourPlusTimeOfRecord = $redeemDate->copy()->addHours($stateTimeHours);
|
||||
Log::info('state time' . $stateHourPlusTimeOfRecord);
|
||||
|
||||
$currentTime = Carbon::now();
|
||||
|
||||
if ($currentTime > $fourHourPlusTimeOfRecord) {
|
||||
$record->update([
|
||||
'is_redeem' => 0,
|
||||
// 'redeem_date' => null,
|
||||
'count' => $record->count + 1,
|
||||
'is_redeemption_undone' => 0,
|
||||
'redeemption_undone_date' => null,
|
||||
]);
|
||||
$restImage = ListingImageUrl('restaurant_images', $managerestaurant->image);
|
||||
$title = "Your " . $managerestaurant->name . " Reinstate successfully";
|
||||
$message = "Your " . $managerestaurant->name . " Reinstate successfully";
|
||||
$content_type = 'Restaurant Reinstate';
|
||||
$imageUrl = $restImage;
|
||||
if ($currentTime > $stateHourPlusTimeOfRecord) {
|
||||
// Only proceed if state time condition is met
|
||||
$restHourPlusTimeOfRecord = $redeemDate->copy()->addHours($restTimeHours);
|
||||
Log::info('Restaurant time ' . $restHourPlusTimeOfRecord);
|
||||
|
||||
onesignalhelper::sendNotificationApi(
|
||||
$customerData->one_signal_player_id,
|
||||
$title,
|
||||
$message,
|
||||
$content_type,
|
||||
$imageUrl,
|
||||
$id = null
|
||||
);
|
||||
Log::info('Reinstate of record done at ' . now());
|
||||
|
||||
onesignalhelper::StoreNotificationDetails($customerData->id, $content_type, $title, $restImage);
|
||||
if ($currentTime > $restHourPlusTimeOfRecord) {
|
||||
$record->update([
|
||||
'is_redeem' => 0,
|
||||
'count' => $record->count + 1,
|
||||
'is_redeemption_undone' => 0,
|
||||
'redeemption_undone_date' => null,
|
||||
]);
|
||||
|
||||
$restImage = ListingImageUrl('restaurant_images', $managerestaurant->image);
|
||||
$title = "Your " . $managerestaurant->name . " Reinstate successfully";
|
||||
$message = "Your " . $managerestaurant->name . " Reinstate successfully";
|
||||
$content_type = 'Restaurant Reinstate';
|
||||
$imageUrl = $restImage;
|
||||
|
||||
onesignalhelper::sendNotificationApi(
|
||||
$customerData->one_signal_player_id,
|
||||
$title,
|
||||
$message,
|
||||
$content_type,
|
||||
$imageUrl,
|
||||
$id = null
|
||||
);
|
||||
Log::info('Reinstate of record done at ' . now());
|
||||
|
||||
onesignalhelper::StoreNotificationDetails($customerData->id, $content_type, $title, $restImage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ use App\Models\IamPrincipalRestaurantRole;
|
||||
use App\Models\ManageRestaurant;
|
||||
use App\Models\RedeemRestaurant;
|
||||
use App\Helpers\onesignalhelper;
|
||||
use App\Models\RestaurantTimeInterval;
|
||||
use App\Models\TimeInterval;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
@@ -220,13 +223,13 @@ class RestaurantApiServices
|
||||
$restaurantId = $request->id;
|
||||
|
||||
$restaurant = ManageRestaurant::with('operatingHours')->where('short_id', $restaurantId)->first();
|
||||
|
||||
if (!$restaurant) {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.restaurant_not_found'), 404);
|
||||
}
|
||||
|
||||
// Check if the restaurant has already been redeemed
|
||||
$restaurantExist = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)->where('iam_principal_xid', $customerIamId)
|
||||
$restaurantExist = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)
|
||||
->where('iam_principal_xid', $customerIamId)
|
||||
->where('is_redeem', 1)
|
||||
->first();
|
||||
|
||||
@@ -234,15 +237,69 @@ class RestaurantApiServices
|
||||
return jsonResponseWithErrorMessageApi(__('auth.restaurant_already_redeemed'), 400);
|
||||
}
|
||||
|
||||
// Check if there's an existing entry for the restaurant and update it
|
||||
$restexist = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)->where('iam_principal_xid', $customerIamId)->first();
|
||||
if ($restexist) {
|
||||
$restexist->is_redeem = 1;
|
||||
$restexist->redeem_date = now();
|
||||
$restexist->is_redeemption_undone = 0;
|
||||
$restexist->redeemption_undone_date = null;
|
||||
$restexist->save();
|
||||
return jsonResponseWithSuccessMessageApi(__('success.restaurant_redeem'), $restexist, 200);
|
||||
$stateLimitation = TimeInterval::where('manage_state_xid', $restaurant->state_xid)->first();
|
||||
$stateMaxLimitation = $stateLimitation ? $stateLimitation->quantity : 0;
|
||||
$stateMaxIntervalLimitation = $stateLimitation ? $stateLimitation->time_interval : "month";
|
||||
|
||||
$restaurantLimitation = RestaurantTimeInterval::where('manage_restaurants_xid', $restaurant->id)->first();
|
||||
$restaurantMaxLimitation = $restaurantLimitation ? $restaurantLimitation->quantity : 0;
|
||||
$restaurantMaxIntervalLimitation = $restaurantLimitation ? $restaurantLimitation->time_interval : "month";
|
||||
|
||||
|
||||
// Calculate the state interval start date
|
||||
$stateIntervalStartDate = $this->calculateIntervalStartDate($stateMaxIntervalLimitation);
|
||||
|
||||
// Count the redeems within the state interval
|
||||
$redeemCountState = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)
|
||||
->where('redeem_date', '>=', $stateIntervalStartDate)
|
||||
->count();
|
||||
|
||||
if ($redeemCountState >= $stateMaxLimitation) {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.state_limit_reached'), 400);
|
||||
}
|
||||
|
||||
// Calculate the restaurant interval start date
|
||||
$restaurantIntervalStartDate = $this->calculateIntervalStartDate($restaurantMaxIntervalLimitation);
|
||||
|
||||
// Count the redeems within the restaurant interval
|
||||
$redeemCountRestaurant = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)
|
||||
->where('redeem_date', '>=', $restaurantIntervalStartDate)
|
||||
->count();
|
||||
|
||||
if ($redeemCountRestaurant >= $restaurantMaxLimitation) {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.restaurant_limit_reached'), 400);
|
||||
}
|
||||
|
||||
// Get the last redeem time
|
||||
$lastRedeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
|
||||
->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;
|
||||
|
||||
Log::info('Rest time interval time_hours: ' . $restTimeHours);
|
||||
Log::info('State time interval time_hours: ' . $stateTimeHours);
|
||||
|
||||
if ($lastRedeem) {
|
||||
$lastRedeemTime = Carbon::parse($lastRedeem->redeem_date);
|
||||
$currentTime = Carbon::now();
|
||||
|
||||
// Calculate the allowed redeem time based on rest and state time intervals
|
||||
$allowedRedeemTime = $lastRedeemTime->copy()->addHours($stateTimeHours);
|
||||
|
||||
if ($currentTime < $allowedRedeemTime) {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet'), 400);
|
||||
}
|
||||
|
||||
$allowedRedeemTime = $lastRedeemTime->copy()->addHours($restTimeHours);
|
||||
|
||||
if ($currentTime < $allowedRedeemTime) {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet'), 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new redeem entry if it doesn't exist
|
||||
@@ -255,8 +312,6 @@ class RestaurantApiServices
|
||||
$redeemRestaurant->redeemption_undone_date = null;
|
||||
$redeemRestaurant->save();
|
||||
|
||||
|
||||
|
||||
$imagePath = ListingImageUrl('restaurant_images', $restaurant->image);
|
||||
|
||||
$customerTitle = "Your Redemption was successful for " . $restaurant->name;
|
||||
@@ -264,11 +319,20 @@ class RestaurantApiServices
|
||||
$customerContentType = 'Voucher_Redemption';
|
||||
$customerImageUrl = $imagePath;
|
||||
|
||||
$customerData = IamPrincipal::where('id', $customerIamId)->where('notification_status', 1)->where('principal_type_xid', 3)->first();
|
||||
$restaurants = IamPrincipalRestaurantRole::select('id', 'principal_xid')->where('restaurant_xid', $restaurant->id)->get();
|
||||
$customerData = IamPrincipal::where('id', $customerIamId)
|
||||
->where('notification_status', 1)
|
||||
->where('principal_type_xid', 3)
|
||||
->first();
|
||||
|
||||
foreach ($restaurants as $restaurant) {
|
||||
$restUser = IamPrincipal::where('id', $restaurant->principal_xid)->where('notification_status', 1)->where('principal_type_xid', 4)->first();
|
||||
$restaurants = IamPrincipalRestaurantRole::select('id', 'principal_xid')
|
||||
->where('restaurant_xid', $restaurant->id)
|
||||
->get();
|
||||
|
||||
foreach ($restaurants as $restaurantRole) {
|
||||
$restUser = IamPrincipal::where('id', $restaurantRole->principal_xid)
|
||||
->where('notification_status', 1)
|
||||
->where('principal_type_xid', 4)
|
||||
->first();
|
||||
|
||||
if ($restUser) {
|
||||
$restImagePath = ListingImageUrl('restaurant_images', $restaurant->image);
|
||||
@@ -278,7 +342,7 @@ class RestaurantApiServices
|
||||
$restImageUrl = $restImagePath;
|
||||
|
||||
// Sending notification to restaurant
|
||||
$pushNotificationToRestaurant = onesignalhelper::restSendNotificationApi(
|
||||
onesignalhelper::restSendNotificationApi(
|
||||
$restUser->one_signal_player_id,
|
||||
$restTitle,
|
||||
$restMessage,
|
||||
@@ -292,7 +356,7 @@ class RestaurantApiServices
|
||||
|
||||
if ($customerData) {
|
||||
// Sending notification to customer
|
||||
$pushNotificationToCustomer = onesignalhelper::sendNotificationApi(
|
||||
onesignalhelper::sendNotificationApi(
|
||||
$customerData->one_signal_player_id,
|
||||
$customerTitle,
|
||||
$customerMessage,
|
||||
@@ -300,9 +364,9 @@ class RestaurantApiServices
|
||||
$customerImageUrl,
|
||||
$id = null
|
||||
);
|
||||
|
||||
onesignalhelper::StoreNotificationDetails($customerData->id, $customerContentType, $customerTitle, $customerImageUrl);
|
||||
}
|
||||
|
||||
return jsonResponseWithSuccessMessageApi(__('success.restaurant_redeem'), $redeemRestaurant, 200);
|
||||
} catch (Exception $ex) {
|
||||
Log::error('Restaurant Redeem service failed: ' . $ex->getMessage());
|
||||
@@ -310,6 +374,22 @@ class RestaurantApiServices
|
||||
}
|
||||
}
|
||||
|
||||
private function calculateIntervalStartDate($interval)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
|
||||
switch ($interval) {
|
||||
case 'day':
|
||||
return $now->copy()->subDay();
|
||||
case 'week':
|
||||
return $now->copy()->subWeek();
|
||||
case 'month':
|
||||
default:
|
||||
return $now->copy()->subMonth();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function searchRestaurant($customerIamId, $request)
|
||||
{
|
||||
|
||||
@@ -110,5 +110,8 @@ return [
|
||||
'invalid_referral_code' => 'The provided referral code is invalid.',
|
||||
'limitation_over' => 'The redemption limit for this referral code has been reached.',
|
||||
'already_used_code' => 'This referral code has already been used.',
|
||||
'redeem_not_allowed_yet' => 'Redeem not allowed yet',
|
||||
'state_limit_reached' => 'Limit reached',
|
||||
'restaurant_limit_reached' => 'Limit reached',
|
||||
|
||||
];
|
||||
|
||||
@@ -8,7 +8,7 @@ use App\Http\Controllers\APIs\Customer_API\FeedbackApiController;
|
||||
use App\Http\Controllers\APIs\Customer_API\NotificationController;
|
||||
use App\Http\Controllers\APIs\Customer_API\RestaurantControllerApi;
|
||||
use App\Http\Controllers\APIs\Customer_API\RulesControllerAPI;
|
||||
use App\Http\Controllers\ReferralCodeController;
|
||||
use App\Http\Controllers\Admin\ReferralCodeController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user