diff --git a/app/Console/Commands/SentScheduleNotification.php b/app/Console/Commands/SentScheduleNotification.php index 7dad744..b05452b 100644 --- a/app/Console/Commands/SentScheduleNotification.php +++ b/app/Console/Commands/SentScheduleNotification.php @@ -9,6 +9,7 @@ use Carbon\Carbon; use Exception; use DateTime; use App\Helpers\onesignalhelper; +use App\Models\Subscriptions; use Illuminate\Support\Facades\Log; class SentScheduleNotification extends Command @@ -32,45 +33,82 @@ class SentScheduleNotification extends Command */ - public function handle() - { - $getAllPushNotifications = NotificationDetails::where('is_schedule', 1) - ->where('is_active', 0) - ->orderByDesc('id') - ->get(); + public function handle() + { + $getAllPushNotifications = NotificationDetails::where('is_schedule', 1) + ->where('is_active', 1) + ->orderByDesc('id') + ->get(); - $currentDateTime = new DateTime(); + $currentDateTime = new DateTime(); - foreach ($getAllPushNotifications as $inAppNotificationItem) { + foreach ($getAllPushNotifications as $inAppNotificationItem) { - $storedDateTime = new DateTime($inAppNotificationItem->delivery_schedule); + $storedDateTime = new DateTime($inAppNotificationItem->delivery_schedule); - $currentTime = $currentDateTime->format('Y-m-d H:i'); - $storedTime = $storedDateTime->format('Y-m-d H:i'); + $currentTime = $currentDateTime->format('Y-m-d H:i'); + $storedTime = $storedDateTime->format('Y-m-d H:i'); - if ($currentTime == $storedTime) { - $title = $inAppNotificationItem->type; - $description = $inAppNotificationItem->description; - $imagePath = $inAppNotificationItem->image; + if ($currentTime == $storedTime) { + $title = $inAppNotificationItem->type; + $description = $inAppNotificationItem->description; + $imagePath = $inAppNotificationItem->image; + $stateIds = json_decode($inAppNotificationItem->state_ids, true); + $userType = $inAppNotificationItem->user_type; - $principalData = IamPrincipal::find($inAppNotificationItem->principal_xid); + if ($userType == 1) { + // Subscribed users + $iamPrincipals = Subscriptions::select('iam_principal_xid') + ->where('next_payment_date', '>=', now()) + ->get(); + $iamPrincipalIds = $iamPrincipals->pluck('iam_principal_xid'); + $users = IamPrincipal::whereIn('id', $iamPrincipalIds) + ->where('is_active', 1) + ->where('notification_status', 1) + ->where('principal_type_xid', 3) + ->whereIn('state_xid', $stateIds) + ->get(); + } elseif ($userType == 2) { + // Unsubscribed users + $allPrincipalIds = IamPrincipal::where('principal_type_xid', 3)->pluck('id'); + $subscribedIds = Subscriptions::select('iam_principal_xid') + ->where('next_payment_date', '>=', now()) + ->pluck('iam_principal_xid'); + $unsubscribedIds = $allPrincipalIds->diff($subscribedIds); + $users = IamPrincipal::whereIn('id', $unsubscribedIds) + ->where('is_active', 1) + ->where('notification_status', 1) + ->whereIn('state_xid', $stateIds) + ->get(); + } elseif ($userType == 3) { + // Both subscribed and unsubscribed users + $users = IamPrincipal::where('is_active', 1) + ->where('notification_status', 1) + ->where('principal_type_xid', 3) + ->whereIn('state_xid', $stateIds) + ->get(); + } - if ($principalData && $principalData->one_signal_player_id) { - OneSignalHelper::sendNotificationApi( - $principalData->one_signal_player_id, - $title, - $description, - 'Dashboard Notification', - $imagePath, - $id = null - ); + foreach ($users as $user) { + if ($user->one_signal_player_id) { + OneSignalHelper::sendNotificationApi( + $user->one_signal_player_id, + $title, + $description, + 'Dashboard Notification', + $imagePath, + $id = null + ); - Log::info("INAPP scheduled notification sent successfully"); + Log::info("INAPP scheduled notification sent successfully to user ID: {$user->id}"); + onesignalhelper::StoreNotificationDetails($user->id, 'Notification', $title, $imagePath); + + $inAppNotificationItem->is_active = 0; + $inAppNotificationItem->save(); + } + } + } + } + } - $inAppNotificationItem->is_active = 1; - $inAppNotificationItem->save(); - } - } - } - } } diff --git a/app/Http/Controllers/Admin/ManageNotificationsController.php b/app/Http/Controllers/Admin/ManageNotificationsController.php index 51cffb8..2554012 100644 --- a/app/Http/Controllers/Admin/ManageNotificationsController.php +++ b/app/Http/Controllers/Admin/ManageNotificationsController.php @@ -27,6 +27,7 @@ class ManageNotificationsController extends Controller if ($activeQuery == 4) { // for customer $notifications = NotificationDetails::with('Notification') ->where('is_active', 1) + ->where('is_schedule', 0) ->whereHas('Notification', function ($query) { $query->where('principal_type_xid', 3); }) @@ -36,6 +37,7 @@ class ManageNotificationsController extends Controller } else if ($activeQuery == 3) { // for restaurant $notifications = NotificationDetails::with('Notification') ->where('is_active', 1) + ->where('is_schedule', 0) ->whereHas('Notification', function ($query) { $query->where('principal_type_xid', 4); }) @@ -47,6 +49,7 @@ class ManageNotificationsController extends Controller } else { $notificationsOfType3 = NotificationDetails::with('Notification') ->where('is_active', 1) + ->where('is_schedule', 0) ->whereHas('Notification', function ($query) { $query->where('principal_type_xid', 3); }) @@ -56,6 +59,7 @@ class ManageNotificationsController extends Controller $notificationsOfType4 = NotificationDetails::with('Notification') ->where('is_active', 1) + ->where('is_schedule', 0) ->whereHas('Notification', function ($query) { $query->where('principal_type_xid', 4); }) @@ -64,6 +68,7 @@ class ManageNotificationsController extends Controller ->get(); $notifications = $notificationsOfType3->merge($notificationsOfType4); + } return view('Admin.pages.manage_notification.manage_notification', compact('notifications')); @@ -86,7 +91,7 @@ class ManageNotificationsController extends Controller * Use : To add notification . */ - public function store_notificaton_data(Request $request) + public function store_notification_data(Request $request) { try { $request->validate([ @@ -95,27 +100,20 @@ class ManageNotificationsController extends Controller DB::beginTransaction(); - if (isset($request->image)) { - $image = $request->image; - $image_db = null; - } else { - $image = null; - $image_db = $request->image; + $imagePath = null; + if ($request->hasFile('image')) { + $image = $request->file('image'); + $imagePath = saveSingleImageWithoutCrop($image, 'notification_images'); + $imagePath = ListingImageUrl('notification_images', $imagePath); } - $tnormalImage = saveSingleImageWithoutCrop($image, 'notification_images', $image_db); - $imagePath = ListingImageUrl('notification_images', $tnormalImage); - $states = $request->states; - $dateTime = now(); $formattedDateTime = $dateTime->format('Y-m-d H:i:s'); $iamPrincipals = Subscriptions::select('iam_principal_xid') ->where('next_payment_date', '>=', $formattedDateTime) ->get(); - $iamPrincipalIds = $iamPrincipals->pluck('iam_principal_xid'); - $subscribe = IamPrincipal::whereIn('id', $iamPrincipalIds) ->where('is_active', 1) ->where('notification_status', 1) @@ -129,23 +127,20 @@ class ManageNotificationsController extends Controller $allCustomerOneSignalIds = $subscribe->pluck('id'); $UserData = IamPrincipal::whereIn('id', $allCustomerOneSignalIds)->get(); - foreach ($UserData as $customerIdItem) { - // user_type 1 = subscribed user - if ($request->schedule_radio1 == 1 && $request->schedule_date) { - // Scheduled Notification - NotificationDetails::create([ - 'principal_xid' => $customerIdItem->id, - 'description' => $request->description, - 'type' => $request->title, - 'image' => $imagePath, - 'date_added' => $request->schedule_date, - 'is_schedule' => 1, - 'delivery_schedule' => $request->schedule_date, - 'is_active' => 0, - ]); - $scheduled = true; - } else { - // Immediate Notification + if ($request->schedule_radio1 == 1 && $request->schedule_date) { + NotificationDetails::create([ + 'user_type' => $request->user_type, + 'description' => $request->description, + 'type' => $request->title, + 'image' => $imagePath, + 'date_added' => $request->schedule_date, + 'is_schedule' => 1, + 'delivery_schedule' => $request->schedule_date, + 'state_ids' => json_encode($states), + ]); + $scheduled = true; + } else { + foreach ($UserData as $customerIdItem) { if ($customerIdItem->one_signal_player_id) { onesignalhelper::sendNotificationApi( $customerIdItem->one_signal_player_id, @@ -160,16 +155,11 @@ class ManageNotificationsController extends Controller } } } elseif ($request->user_type == 2) { - // user_type 2 unsubscribed users - $allPrincipalIds = IamPrincipal::where('principal_type_xid', 3) - ->pluck('id'); - + $allPrincipalIds = IamPrincipal::where('principal_type_xid', 3)->pluck('id'); $subscribedIds = Subscriptions::select('iam_principal_xid') ->where('next_payment_date', '>=', $formattedDateTime) ->pluck('iam_principal_xid'); - $unsubscribedIds = $allPrincipalIds->diff($subscribedIds); - $unsubscribedPrincipals = IamPrincipal::whereIn('id', $unsubscribedIds) ->where('is_active', 1) ->where('notification_status', 1) @@ -179,22 +169,20 @@ class ManageNotificationsController extends Controller $allRestaurantOneSignalIds = $unsubscribedPrincipals->pluck('id'); $restaurantData = IamPrincipal::whereIn('id', $allRestaurantOneSignalIds)->get(); - foreach ($restaurantData as $restaurantsData) { - if ($request->schedule_radio1 == 1 && $request->schedule_date) { - // Scheduled Notification - NotificationDetails::create([ - 'principal_xid' => $restaurantsData->id, - 'description' => $request->description, - 'type' => $request->title, - 'image' => $imagePath, - 'date_added' => $request->schedule_date, - 'is_schedule' => 1, - 'delivery_schedule' => $request->schedule_date, - 'is_active' => 0, - ]); - $scheduled = true; - } else { - // Immediate Notification + if ($request->schedule_radio1 == 1 && $request->schedule_date) { + NotificationDetails::create([ + 'user_type' => $request->user_type, + 'description' => $request->description, + 'type' => $request->title, + 'image' => $imagePath, + 'date_added' => $request->schedule_date, + 'is_schedule' => 1, + 'delivery_schedule' => $request->schedule_date, + 'state_ids' => json_encode($states), + ]); + $scheduled = true; + } else { + foreach ($restaurantData as $restaurantsData) { if ($restaurantsData->one_signal_player_id) { onesignalhelper::sendNotificationApi( $restaurantsData->one_signal_player_id, @@ -209,7 +197,6 @@ class ManageNotificationsController extends Controller } } } elseif ($request->user_type == 3) { - // user_type 3 = subscribed and unsubscribed users $userQuery = IamPrincipal::where('is_active', 1) ->where('notification_status', 1) ->where('principal_type_xid', 3) @@ -218,22 +205,20 @@ class ManageNotificationsController extends Controller $allUserOneSignalIds = $userQuery->pluck('id'); $UserData = IamPrincipal::whereIn('id', $allUserOneSignalIds)->get(); - foreach ($UserData as $CustomerData) { - if ($request->schedule_radio1 == 1 && $request->schedule_date) { - // Scheduled Notification - NotificationDetails::create([ - 'principal_xid' => $CustomerData->id, - 'description' => $request->description, - 'type' => $request->title, - 'image' => $imagePath, - 'date_added' => $request->schedule_date, - 'is_schedule' => 1, - 'delivery_schedule' => $request->schedule_date, - 'is_active' => 0, - ]); - $scheduled = true; - } else { - // Immediate Notification + if ($request->schedule_radio1 == 1 && $request->schedule_date) { + NotificationDetails::create([ + 'user_type' => $request->user_type, + 'description' => $request->description, + 'type' => $request->title, + 'image' => $imagePath, + 'date_added' => $request->schedule_date, + 'is_schedule' => 1, + 'delivery_schedule' => $request->schedule_date, + 'state_ids' => json_encode($states), + ]); + $scheduled = true; + } else { + foreach ($UserData as $CustomerData) { if ($CustomerData->one_signal_player_id) { onesignalhelper::sendNotificationApi( $CustomerData->one_signal_player_id, @@ -268,6 +253,7 @@ class ManageNotificationsController extends Controller + /** * Created By : Sayli Raut * Created at : 10 June 2024 diff --git a/app/Models/NotificationDetails.php b/app/Models/NotificationDetails.php index 708389b..646076c 100644 --- a/app/Models/NotificationDetails.php +++ b/app/Models/NotificationDetails.php @@ -11,7 +11,7 @@ class NotificationDetails extends Model protected $table = "notification_details"; protected $fillable = [ 'id', - 'principal_xid', 'type', 'description', 'image', 'date_added','is_schedule','delivery_schedule','is_active' + 'principal_xid', 'type', 'description', 'image', 'date_added','is_schedule','delivery_schedule','is_active', 'user_type', 'state_ids' ]; protected $guarded = []; @@ -20,4 +20,9 @@ class NotificationDetails extends Model { return $this->belongsTo(IamPrincipal::class, 'principal_xid', 'id'); } + + protected $casts = [ + 'state_ids' => 'array', + ]; + } diff --git a/database/migrations/2024_05_31_095128_create_notification_details.php b/database/migrations/2024_05_31_095128_create_notification_details.php index 54468aa..b51c990 100644 --- a/database/migrations/2024_05_31_095128_create_notification_details.php +++ b/database/migrations/2024_05_31_095128_create_notification_details.php @@ -13,13 +13,15 @@ return new class extends Migration { Schema::create('notification_details', function (Blueprint $table) { $table->id(); - $table->unsignedBigInteger('principal_xid'); + $table->unsignedBigInteger('principal_xid')->nullable(); $table->foreign('principal_xid')->references('id')->on('iam_principal')->onDelete('cascade'); $table->string('type'); $table->longText('description'); $table->string('image')->nullable(); - $table->boolean('is_schedule')->comment('1=specific time, 0=immediate'); - $table->dateTime('delivery_schedule'); + $table->string('user_type')->nullable(); + $table->boolean('is_schedule')->default(0)->comment('1=specific time, 0=immediate'); + $table->dateTime('delivery_schedule')->nullable(); + $table->json('state_ids')->nullable(); $table->timestamp('date_added')->useCurrent(); $table->boolean('is_active')->default(1)->comment('1=Active, 0=Expired'); $table->integer('created_by')->nullable(); diff --git a/routes/web.php b/routes/web.php index 794a67b..98cc222 100644 --- a/routes/web.php +++ b/routes/web.php @@ -189,7 +189,7 @@ Route::group(['middleware' => ['checkStatus']], function () { //*******************************************************manage notification******************************************************** Route::get('/manage-notification', [ManageNotificationsController::class, 'index'])->name('manage.notification'); Route::get('/manage_add_notifications', [ManageNotificationsController::class, 'add'])->name('manage_add_notifications'); - Route::post('/insert_notification', [ManageNotificationsController::class, 'store_notificaton_data']); + Route::post('/insert_notification', [ManageNotificationsController::class, 'store_notification_data']); Route::get('/manage_view_notifications/{id}', [ManageNotificationsController::class, 'view']);