send notification

This commit is contained in:
sayliraut
2024-06-11 16:03:19 +05:30
parent 811dcfbb1a
commit 78444f3b16
2 changed files with 73 additions and 80 deletions

View File

@@ -3,8 +3,13 @@
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\IamPrincipal;
use App\Models\NotificationDetails;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Support\Facades\Log;
use App\Helpers\onesignalhelper;
use Illuminate\Support\Facades\DB;
class ManageNotificationsController extends Controller
{
@@ -68,7 +73,7 @@ class ManageNotificationsController extends Controller
return view('Admin.pages.manage_notification.manage_notifications_add');
}
/**
/**
* Created By : Sayli Raut
* Created at : 10 June 2024
* Use : To add notification .
@@ -91,37 +96,30 @@ class ManageNotificationsController extends Controller
}
$tnormalImage = saveSingleImageWithoutCrop($image, 'notification_images', $image_db);
$imagePath = ListingImageUrl('notification_images', $tnormalImage);
$allOrders = Order::pluck('iam_principal_xid');
$passportId = $request->passport;
$passportUserIds = OrderedPassport::where('passport_xid', $passportId)
->whereHas('iamPrincipal', function ($query) {
$query->where('notification_status', 1)
->where('principal_type_xid', 3);
})
->pluck('iam_principal_xid');
$passportUserIdsToArray = $passportUserIds->toArray();
$allCustomerOneSignalIds = IamPrincipal::where('is_active', 1)
->where('notification_status', 1)
->where('principal_type_xid', 3)
->pluck('id');
$allRestaurantOneSignalIds = IamPrincipal::where('is_active', 1)
->where('notification_status', 1)
->where('principal_type_xid', 4)
->pluck('id');
// Find the remaining user IDs
$remainingUserIds = $allCustomerOneSignalIds->diff($allOrders);
$remainingUserIdsArray = $remainingUserIds->toArray();
// Find the remaining user data
$remainingUserData = IamPrincipal::whereIn('id', $remainingUserIdsArray)->get();
$parchasePassportUserData = IamPrincipal::whereIn('id', $passportUserIdsToArray)->get();
$UserData = IamPrincipal::whereIn('id', $allCustomerOneSignalIds)->get();
$title = $request->title;
$message = $request->description;
$content_type = 'Notification';
$imageUrl = $imagePath;
// FOR all User
if ($request->user_type == 1) {
foreach ($remainingUserData as $customerIdItem) {
// FOR all customer
if ($request->recipients == 'customer') {
foreach ($UserData as $customerIdItem) {
if ($customerIdItem->one_signal_player_id) {
onesignalhelper::sendNotificationApi(
$customerIdItem->one_signal_player_id,
@@ -134,11 +132,14 @@ class ManageNotificationsController extends Controller
}
onesignalhelper::StoreNotificationDetails($customerIdItem->id, $content_type, $title, $imagePath);
}
} elseif ($request->user_type == 2) {
foreach ($parchasePassportUserData as $customerIdItem) {
if ($customerIdItem->one_signal_player_id) {
} elseif ($request->recipients == 'restaurant') {
$restaurantData = IamPrincipal::whereIn('id', $allRestaurantOneSignalIds)->get();
foreach ($restaurantData as $restIdItem) {
if ($restIdItem->one_signal_player_id) {
onesignalhelper::sendNotificationApi(
$customerIdItem->one_signal_player_id,
$restIdItem->one_signal_player_id,
$title,
$message,
$content_type,
@@ -146,21 +147,15 @@ class ManageNotificationsController extends Controller
$id = null
);
}
onesignalhelper::StoreNotificationDetails($customerIdItem->id, $content_type, $title, $imagePath);
onesignalhelper::StoreNotificationDetails($restIdItem->id, $content_type, $title, $imagePath);
}
}
// FOR All passport user
DB::commit();
return jsonResponseWithSuccessMessage(__('success.save_data'));
// return $voucher_data;
} catch (Exception $e) {
DB::rollBack();
Log::error("Coupon Store Page Load Failed " . $e->getMessage());
Log::error("Notification send Failed " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}

View File

@@ -43,13 +43,13 @@
@csrf
<div class="row">
<div class="col-md-6">
<div class="form-group ">
<div class="form-group">
<label for="company-name" class="label">Notification Content</label>
<input type="text" class="form-control" name="title">
</div>
</div>
<div class="col-md-6">
<div class="form-group ">
<div class="form-group">
<label for="company-name" class="label">Description</label>
<textarea type="text" class="form-control" name="description"></textarea>
</div>
@@ -65,22 +65,20 @@
<label for="company-name" class="label"> Recipients </label>
<select id="recipients" name="recipients" class="form-control">
<option value="">Select Category</option>
<option value="customer">
Customer
</option>
<option value="customer">
Restaurant
</option>
<option value="customer">Customer</option>
<option value="restaurant">Restaurant</option>
</select>
</div>
</div>
<div class="col-md-12">
<button class="download-btn-custom mt-3 custom-width-10" type="submit"
id="store_notification_btn" class="w-100"><span>Submit</span></button>
id="store_notification_btn">
<span>Submit</span>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
@@ -89,44 +87,43 @@
</div>
</div>
@endsection
<script>
$(document).on("click", "#store_notification_btn", function(e) {
$.validator.addMethod("atLeastOneChecked", function(value, element) {
return $('input[type=radio]:checked', element.form).length > 0;
}, "Please select at least one option");
@section('section_script')
$('#send_notification_form').validate({
ignore: [],
debug: false,
rules: {
title: {
required: true
},
description: {
required: true
},
image: {
required: true,
},
user_type: {
atLeastOneChecked: true
}
},
messages: {
title: {
required: 'Please enter this field',
},
description: {
required: 'Please enter this field',
},
image: {
required: 'Please upload an image file',
},
user_type: {
atLeastOneChecked: 'Please select at least one option'
}
},
errorClass: 'error-message',
<script>
$(document).ready(function() {
// Validate the form
$('#send_notification_form').validate({
ignore: [],
debug: false,
rules: {
title: {
required: true
},
description: {
required: true
},
image: {
required: true
},
recipients: {
required: true
}
},
messages: {
title: {
required: 'Please enter this field'
},
description: {
required: 'Please enter this field'
},
image: {
required: 'Please upload an image file'
},
recipients: {
required: 'Please select at least one category'
}
},
errorClass: 'error-message',
errorPlacement: function(error, element) {
if (element.attr("name") == "user_type") {
error.insertAfter("#select-popular-ids").addClass('error-message');
@@ -157,7 +154,7 @@ $(document).on("click", "#store_notification_btn", function(e) {
toastr.success('Data Added Successfully');
setTimeout(function() {
window.location.href = base_url +
"/manage_notifications";
"/manage-notification";
}, 2000);
} else if (result.status_code == 422) {
// Display validation errors using toastr
@@ -165,14 +162,14 @@ $(document).on("click", "#store_notification_btn", function(e) {
toastr.error(value);
setTimeout(function() {
window.location.href = base_url +
"/manage_notifications";
"/manage-notification";
}, 2000);
});
} else {
toastr.error('Something Went Wrong');
setTimeout(function() {
window.location.href = base_url +
"/manage_notifications";
"/manage-notification";
}, 2000);
}
$('#store_notification_btn').attr('disabled', false);
@@ -183,3 +180,4 @@ $(document).on("click", "#store_notification_btn", function(e) {
});
});
</script>
@endsection