schedule notification

This commit is contained in:
sayliraut
2024-07-09 15:54:23 +05:30
parent f1e148a335
commit f50ea7e39e
2 changed files with 176 additions and 95 deletions

View File

@@ -0,0 +1,76 @@
<?php
namespace App\Console\Commands;
use App\Models\IamPrincipal;
use App\Models\NotificationDetails;
use Illuminate\Console\Command;
use Carbon\Carbon;
use Exception;
use DateTime;
use App\Helpers\onesignalhelper;
use Illuminate\Support\Facades\Log;
class SentScheduleNotification extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:sent-schedule-notification';
/**
* The console command description.
*
* @var string
*/
protected $description = 'It will send Recommendation to users Based on stored Data';
/**
* Execute the console command.
*/
public function handle()
{
$getAllPushNotifications = NotificationDetails::where('is_schedule', 1)
->where('is_active', 0)
->orderByDesc('id')
->get();
$currentDateTime = new DateTime();
foreach ($getAllPushNotifications as $inAppNotificationItem) {
$storedDateTime = new DateTime($inAppNotificationItem->delivery_schedule);
$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;
$principalData = IamPrincipal::find($inAppNotificationItem->principal_xid);
if ($principalData && $principalData->one_signal_player_id) {
OneSignalHelper::sendNotificationApi(
$principalData->one_signal_player_id,
$title,
$description,
'Dashboard Notification',
$imagePath,
$id = null
);
Log::info("INAPP scheduled notification sent successfully");
$inAppNotificationItem->is_active = 1;
$inAppNotificationItem->save();
}
}
}
}
}

View File

@@ -82,107 +82,110 @@ class ManageNotificationsController extends Controller
* Use : To add notification .
*/
public function store_notificaton_data(Request $request)
{
try {
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
public function store_notificaton_data(Request $request)
{
try {
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
DB::beginTransaction();
DB::beginTransaction();
if (isset($request->image)) {
$image = $request->image;
$image_db = null;
} else {
$image = null;
$image_db = $request->image;
}
if (isset($request->image)) {
$image = $request->image;
$image_db = null;
} else {
$image = null;
$image_db = $request->image;
}
$tnormalImage = saveSingleImageWithoutCrop($image, 'notification_images', $image_db);
$imagePath = ListingImageUrl('notification_images', $tnormalImage);
$tnormalImage = saveSingleImageWithoutCrop($image, 'notification_images', $image_db);
$imagePath = ListingImageUrl('notification_images', $tnormalImage);
$states = $request->states;
$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();
$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');
$iamPrincipalIds = $iamPrincipals->pluck('iam_principal_xid');
$subscribe = IamPrincipal::whereIn('id', $iamPrincipalIds)
->where('is_active', 1)
->where('notification_status', 1)
->where('principal_type_xid', 3)
->whereIn('state_xid', $states)
->get();
$subscribe = IamPrincipal::whereIn('id', $iamPrincipalIds)
->where('is_active', 1)
->where('notification_status', 1)
->where('principal_type_xid', 3)
->whereIn('state_xid', $states)
->get();
if ($request->user_type == 1) {
$allCustomerOneSignalIds = $subscribe->pluck('id');
$UserData = IamPrincipal::whereIn('id', $allCustomerOneSignalIds)->get();
if ($request->user_type == 1) {
$allCustomerOneSignalIds = $subscribe->pluck('id');
$UserData = IamPrincipal::whereIn('id', $allCustomerOneSignalIds)->get();
foreach ($UserData as $customerIdItem) {
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' => 'Dashboard Notification',
'image' => $imagePath,
'date_added' => $request->schedule_date,
'is_schedule' => 1,
'delivery_schedule' => $request->schedule_date,
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,
]);
} else {
// Immediate Notification
if ($customerIdItem->one_signal_player_id) {
onesignalhelper::sendNotificationApi(
$customerIdItem->one_signal_player_id,
$request->title,
$request->description,
'Dashboard Notification',
$imagePath,
$id = null
);
}
onesignalhelper::StoreNotificationDetails($customerIdItem->id, 'Notification', $request->title, $imagePath);
}
}
} elseif ($request->user_type == 2) {
//user_type 2 unsubscribed users
$allPrincipalIds = IamPrincipal::where('principal_type_xid', 3)
->pluck('id');
]);
} else {
// Immediate Notification
if ($customerIdItem->one_signal_player_id) {
onesignalhelper::sendNotificationApi(
$customerIdItem->one_signal_player_id,
$request->title,
$request->description,
'Dashboard Notification',
$imagePath,
$id = null
);
}
onesignalhelper::StoreNotificationDetails($customerIdItem->id, 'Notification', $request->title, $imagePath);
}
}
} elseif ($request->user_type == 2) {
//user_type 2 unsubscribed users
$allPrincipalIds = IamPrincipal::where('principal_type_xid', 3)
->pluck('id');
$subscribedIds = Subscriptions::select('iam_principal_xid')
->where('next_payment_date', '>=', $formattedDateTime)
->pluck('iam_principal_xid');
$subscribedIds = Subscriptions::select('iam_principal_xid')
->where('next_payment_date', '>=', $formattedDateTime)
->pluck('iam_principal_xid');
$unsubscribedIds = $allPrincipalIds->diff($subscribedIds);
$unsubscribedIds = $allPrincipalIds->diff($subscribedIds);
$unsubscribedPrincipals = IamPrincipal::whereIn('id', $unsubscribedIds)
->where('is_active', 1)
->where('notification_status', 1)
->whereIn('state_xid', $states)
->get();
$unsubscribedPrincipals = IamPrincipal::whereIn('id', $unsubscribedIds)
->where('is_active', 1)
->where('notification_status', 1)
->whereIn('state_xid', $states)
->get();
$allRestaurantOneSignalIds = $unsubscribedPrincipals->pluck('id');
$restaurantData = IamPrincipal::whereIn('id', $allRestaurantOneSignalIds)->get();
$allRestaurantOneSignalIds = $unsubscribedPrincipals->pluck('id');
$restaurantData = IamPrincipal::whereIn('id', $allRestaurantOneSignalIds)->get();
foreach ($restaurantData as $restaurantsData) {
foreach ($restaurantData as $restaurantsData) {
if ($request->schedule_radio1 == 1 && $request->schedule_date) {
// Scheduled Notification
NotificationDetails::create([
'principal_xid' => $restaurantsData->id,
'description' => $request->description,
'type' => 'Dashboard Notification',
'type' => $request->title,
'image' => $imagePath,
'date_added' => $request->schedule_date,
'is_schedule' => 1,
'delivery_schedule' => $request->schedule_date,
'is_active' => 0,
]);
} else {
@@ -192,7 +195,7 @@ class ManageNotificationsController extends Controller
$restaurantsData->one_signal_player_id,
$request->title,
$request->description,
'Dashboard Notification',
$request->title,
$imagePath,
$id = null
);
@@ -200,27 +203,29 @@ class ManageNotificationsController extends Controller
onesignalhelper::StoreNotificationDetails($restaurantsData->id, 'Notification', $request->title, $imagePath);
}
}
} 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)
->whereIn('state_xid', $states);
} 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)
->whereIn('state_xid', $states);
$allUserOneSignalIds = $userQuery->pluck('id');
$UserData = IamPrincipal::whereIn('id', $allUserOneSignalIds)->get();
$allUserOneSignalIds = $userQuery->pluck('id');
$UserData = IamPrincipal::whereIn('id', $allUserOneSignalIds)->get();
foreach ($UserData as $CustomerData) {
foreach ($UserData as $CustomerData) {
if ($request->schedule_radio1 == 1 && $request->schedule_date) {
// Scheduled Notification
NotificationDetails::create([
'principal_xid' => $CustomerData->id,
'description' => $request->description,
'type' => 'Dashboard Notification',
'type' => $request->title,
'image' => $imagePath,
'date_added' => $request->schedule_date,
'is_schedule' => 1,
'delivery_schedule' => $request->schedule_date,
'is_active' => 0,
]);
} else {
@@ -240,14 +245,14 @@ class ManageNotificationsController extends Controller
}
}
DB::commit();
return jsonResponseWithSuccessMessage(__('success.save_data'));
} catch (Exception $e) {
DB::rollBack();
Log::error("Notification send Failed " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}
DB::commit();
return jsonResponseWithSuccessMessage(__('success.save_data'));
} catch (Exception $e) {
DB::rollBack();
Log::error("Notification send Failed " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}