send notification
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Events\EventType;
|
use App\Events\EventType;
|
||||||
|
use App\Models\System\Roles\Permission;
|
||||||
use App\Models\System\Verification;
|
use App\Models\System\Verification;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Psr7\Request as GuzzleRequest;
|
use GuzzleHttp\Psr7\Request as GuzzleRequest;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Stevebauman\Location\Facades\Location;
|
use Stevebauman\Location\Facades\Location;
|
||||||
use Symfony\Component\HttpFoundation\IpUtils;
|
use Symfony\Component\HttpFoundation\IpUtils;
|
||||||
@@ -286,3 +289,10 @@ function cached(string $name, mixed $value, int $seconds = 60): mixed
|
|||||||
callback: fn () => is_callable($value) ? call_user_func($value) : $value
|
callback: fn () => is_callable($value) ? call_user_func($value) : $value
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function view_loan_order_permission_id(): int
|
||||||
|
{
|
||||||
|
return Cache::rememberForever('view_loan_order_permission_id', function () {
|
||||||
|
return Permission::query()->where('name', 'ViewLoanOrders')->first(['id', 'name'])->id;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ use App\Rules\DowranAgaAllowed;
|
|||||||
use App\Rules\OnlyLetters;
|
use App\Rules\OnlyLetters;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Laravel\Nova\Fields\Date;
|
use Laravel\Nova\Fields\Date;
|
||||||
use Laravel\Nova\Fields\Email;
|
use Laravel\Nova\Fields\Email;
|
||||||
@@ -151,9 +150,14 @@ class LoanOrder extends Resource
|
|||||||
*/
|
*/
|
||||||
public static function afterUpdate(NovaRequest $request, Model $model): void
|
public static function afterUpdate(NovaRequest $request, Model $model): void
|
||||||
{
|
{
|
||||||
|
/** @var LoanOrderModel */
|
||||||
|
$loanOrder = $model;
|
||||||
|
|
||||||
if ($request->user()->doesntHaveRoles()) {
|
if ($request->user()->doesntHaveRoles()) {
|
||||||
LoanOrderRepo::notifyUser($request, $model, __('Loan order updated'), 'info', 'pencil-alt');
|
LoanOrderRepo::notifyUser($request, $loanOrder, __('Loan order updated'), 'info', 'pencil-alt');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LoanOrderRepo::notifyOperators($loanOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ class LoanOrderPolicy
|
|||||||
*/
|
*/
|
||||||
public function viewAny(User $user): bool
|
public function viewAny(User $user): bool
|
||||||
{
|
{
|
||||||
if ($user->isOperator() && $user->cannot('viewLoanOrders')) {
|
if ($user->isOperator()) {
|
||||||
return false;
|
return $user->getPermissionNames()->contains('ViewLoanOrders');
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -29,8 +29,11 @@ class LoanOrderPolicy
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->isOperator() && $user->can('viewLoanOrders')) {
|
if ($user->isOperator() && $user->getPermissionNames()->contains('ViewLoanOrders')) {
|
||||||
return $user->branches()->where('branches.id', $loanOrder->branch_id)->exists();
|
return in_array(
|
||||||
|
$loanOrder->branch_id,
|
||||||
|
$user->branches->pluck('id')->toArray()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->ownsLoanOrder($loanOrder)) {
|
if ($user->ownsLoanOrder($loanOrder)) {
|
||||||
@@ -45,8 +48,8 @@ class LoanOrderPolicy
|
|||||||
*/
|
*/
|
||||||
public function create(User $user): bool
|
public function create(User $user): bool
|
||||||
{
|
{
|
||||||
if ($user->isOperator() && $user->cannot('viewLoanOrders')) {
|
if ($user->isOperator()) {
|
||||||
return false;
|
return $user->getPermissionNames()->contains('ViewLoanOrders');
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -61,8 +64,11 @@ class LoanOrderPolicy
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->isOperator() && $user->can('viewLoanOrders')) {
|
if ($user->isOperator() && $user->getPermissionNames()->contains('ViewLoanOrders')) {
|
||||||
return $user->branches()->where('branches.id', $loanOrder->branch_id)->exists();
|
return in_array(
|
||||||
|
$loanOrder->branch_id,
|
||||||
|
$user->branches->pluck('id')->toArray()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->ownsLoanOrder($loanOrder) && in_array($loanOrder->status, [
|
if ($user->ownsLoanOrder($loanOrder) && in_array($loanOrder->status, [
|
||||||
@@ -83,8 +89,11 @@ class LoanOrderPolicy
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->isOperator() && $user->can('viewLoanOrders')) {
|
if ($user->isOperator() && $user->getPermissionNames()->contains('ViewLoanOrders')) {
|
||||||
return $user->branches()->where('branches.id', $loanOrder->branch_id)->exists();
|
return in_array(
|
||||||
|
$loanOrder->branch_id,
|
||||||
|
$user->branches->pluck('id')->toArray()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->ownsLoanOrder($loanOrder)) {
|
if ($user->ownsLoanOrder($loanOrder)) {
|
||||||
|
|||||||
@@ -3,10 +3,13 @@
|
|||||||
namespace App\Repos\Order\Loan;
|
namespace App\Repos\Order\Loan;
|
||||||
|
|
||||||
use App\Models\Branch\Branch;
|
use App\Models\Branch\Branch;
|
||||||
|
use App\Models\Order\Loan\LoanOrder;
|
||||||
use App\Nova\Resources\Order\Loan\LoanOrder as NovaLoanOrder;
|
use App\Nova\Resources\Order\Loan\LoanOrder as NovaLoanOrder;
|
||||||
use App\Repos\Order\OrderRepo;
|
use App\Repos\Order\OrderRepo;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||||
use Laravel\Nova\Notifications\NovaNotification;
|
use Laravel\Nova\Notifications\NovaNotification;
|
||||||
use Laravel\Nova\URL;
|
use Laravel\Nova\URL;
|
||||||
@@ -62,14 +65,11 @@ class LoanOrderRepo
|
|||||||
|
|
||||||
public static function notifyUser(
|
public static function notifyUser(
|
||||||
NovaRequest $request,
|
NovaRequest $request,
|
||||||
Model $model,
|
LoanOrder $loanOrder,
|
||||||
string $message,
|
string $message,
|
||||||
string $type = 'info',
|
string $type = 'info',
|
||||||
string $icon = 'eye',
|
string $icon = 'eye',
|
||||||
): void {
|
): void {
|
||||||
/** @var \App\Models\Order\Loan\LoanOrder */
|
|
||||||
$loanOrder = $model;
|
|
||||||
|
|
||||||
$id = $loanOrder->id;
|
$id = $loanOrder->id;
|
||||||
$nova_path = config('nova.path');
|
$nova_path = config('nova.path');
|
||||||
$loan_order_path = NovaLoanOrder::uriKey();
|
$loan_order_path = NovaLoanOrder::uriKey();
|
||||||
@@ -83,4 +83,49 @@ class LoanOrderRepo
|
|||||||
->type($type)
|
->type($type)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function notifyOperators(
|
||||||
|
LoanOrder $loanOrder,
|
||||||
|
string $message,
|
||||||
|
string $type = 'info',
|
||||||
|
string $icon = 'eye',
|
||||||
|
): void {
|
||||||
|
$id = $loanOrder->id;
|
||||||
|
$nova_path = config('nova.path');
|
||||||
|
$loan_order_path = NovaLoanOrder::uriKey();
|
||||||
|
$url = url($nova_path.'/resources/'.$loan_order_path.'/'.$id);
|
||||||
|
|
||||||
|
// Get users related with branch...
|
||||||
|
$branch_users = DB::table('branch_user')->where('branch_id', $loanOrder->branch_id)->pluck('user_id');
|
||||||
|
|
||||||
|
// Check if they have permission for viewing loan order
|
||||||
|
$loanOrderOperators = DB::table('model_has_permissions')
|
||||||
|
->where('permission_id', view_loan_order_permission_id())
|
||||||
|
->whereIntegerInRaw('model_id', $branch_users)
|
||||||
|
->pluck('model_id');
|
||||||
|
|
||||||
|
$loanOrderOperators->each(function ($branch_user) use ($icon, $message, $url, $type) {
|
||||||
|
DB::table('nova_notifications')->insert([
|
||||||
|
'id' => Str::uuid(),
|
||||||
|
'type' => 'Laravel\Nova\Notifications\NovaNotification',
|
||||||
|
'notifiable_type' => 'App\Models\User',
|
||||||
|
'notifiable_id' => $branch_user,
|
||||||
|
'data' => json_encode([
|
||||||
|
'component' => 'message-notification',
|
||||||
|
'icon' => $icon,
|
||||||
|
'message' => $message,
|
||||||
|
'actionText' => 'Görmek',
|
||||||
|
'actionUrl' => [
|
||||||
|
'url' => URL::remote($url),
|
||||||
|
'remote' => true,
|
||||||
|
],
|
||||||
|
'openInNewTab' => false,
|
||||||
|
'type' => $type,
|
||||||
|
'iconClass' => 'text-sky-500',
|
||||||
|
]),
|
||||||
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user