исправлены баги в модуле уведомлений

This commit is contained in:
Thekindbull 2025-05-27 08:53:58 +08:00
parent 57b1c744b9
commit 93a09f63ad
7 changed files with 169 additions and 34 deletions

View File

@ -7,62 +7,67 @@
use App\Models\Deal\Deal; use App\Models\Deal\Deal;
use App\Models\Deal\DealStatus; use App\Models\Deal\DealStatus;
use App\Notifications\UniqueContact;
use App\Notifications\NotUniqueContact;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class ClientsApiController extends Controller class ClientsApiController extends Controller
{ {
public const ACTION_CONFIRM = 'confirm'; public const ACTION_CONFIRM = 'confirm';
public const ACTION_UPDATE_CONTRACT = 'contract'; public const ACTION_UPDATE_CONTRACT = 'contract';
public function index(Request $request) public function index(Request $request)
{ {
if ($deal = Deal::where('confirm_token', $request->hash)->first()) if ($deal = Deal::where('confirm_token', $request->hash)->first())
{ {
switch ( $request->action ) switch ( $request->action )
{ {
case $this::ACTION_CONFIRM: case $this::ACTION_CONFIRM:
$this->confirm($deal, $request); $this->confirm($deal, $request);
break; break;
case $this::ACTION_UPDATE_CONTRACT: case $this::ACTION_UPDATE_CONTRACT:
$this->updateContract($deal, $request); $this->updateContract($deal, $request);
break; break;
}
return true;
} }
return false; return true;
} }
return false;
}
public function confirm(Deal $deal, Request $request) public function confirm(Deal $deal, Request $request)
{ {
$agent = $deal->agent;
if ((bool) $request->is_unique) if ((bool) $request->is_unique)
{ {
$deal->status = DealStatus::UNIQUE; $deal->status = DealStatus::UNIQUE;
} $agent->user->notify(new UniqueContact($deal));
}
else else
{ {
$deal->status = DealStatus::NOT_UNIQUE; $deal->status = DealStatus::NOT_UNIQUE;
} $agent->user->notify(new NotUniqueContact($deal));
}
Log::build([ Log::build([
'driver' => 'single', 'driver' => 'single',
'path' => storage_path('logs/bitrix.log'), 'path' => storage_path('logs/bitrix.log'),
])->error( ])->error(
json_encode( json_encode(
[ [
'is_unique' => $request->is_unique, 'is_unique' => $request->is_unique,
'deal' => $deal->id, 'deal' => $deal->id,
'status' => $deal->status 'status' => $deal->status
] ]
) )
); );
$deal->save(); $deal->save();
return $deal->id; return $deal->id;
} }
public function updateContract(Deal $deal, Request $request) public function updateContract(Deal $deal, Request $request)
{ {
$contract = new ContractApiController; $contract = new ContractApiController;
$contract($deal, $request); $contract($deal, $request);
}
} }
}

View File

@ -9,22 +9,25 @@
use App\Models\Deal\Contract; use App\Models\Deal\Contract;
use App\Models\Deal\ContractStatus; use App\Models\Deal\ContractStatus;
use App\Models\Agent\Agent; use App\Models\Agent\Agent;
use App\Notifications\ContractUpdated;
class ContractApiController class ContractApiController
{ {
public function __invoke(Deal $deal, Request $request) public function __invoke(Deal $deal, Request $request)
{ {
$status = $status =
Contract::updateOrCreate( Contract::updateOrCreate(
['deal_id' => $deal->id], ['deal_id' => $deal->id],
[ [
'status' => constant(ContractStatus::class . "::" . $request->status), 'status' => constant(ContractStatus::class . "::" . $request->status),
'comment' => $request->comment, 'comment' => $request->comment,
'price' => $request->price, 'price' => $request->price,
'reward' => $request->reward, 'reward' => $request->reward,
'square' => $request->square, 'square' => $request->square,
'floor' => $request->floor 'floor' => $request->floor
] ]
); );
$agent = $deal->agent;
$agent->user->notify(new ContractUpdated($deal->contract));
return true; return true;
}
} }
}

View File

@ -6,12 +6,14 @@
use App\Models\Deal\Deal; use App\Models\Deal\Deal;
use App\Notifications\UniqueContact; use App\Notifications\UniqueContact;
use App\Notifications\NotUniqueContact;
use App\Notifications\ContractUpdated;
class NotificationProbeController extends Controller class NotificationProbeController extends Controller
{ {
public function index() public function index()
{ {
auth()->user()->notify(new UniqueContact(Deal::find(4))); auth()->user()->notify(new ContractUpdated(Deal::find(4)->contract));
echo auth()->user()->unreadNotifications->count() . '<br>'; echo auth()->user()->unreadNotifications->count() . '<br>';
die(auth()->user()->notifications); die(auth()->user()->notifications);
} }

View File

@ -3,7 +3,7 @@
<div class="d-flex justify-content-center align-items-center"> <div class="d-flex justify-content-center align-items-center">
<div class="d-grid gap-1 p-3"> <div class="d-grid gap-1 p-3">
<i class="bi bi-inbox display-5 text-center"></i> <i class="bi bi-inbox display-5 text-center"></i>
<span class="fs-6 fw-semibold">{{ __('notification.has no notifications') }}</span> <span class="fs-6 fw-semibold">{{ __('notifications.has no notifications') }}</span>
</div> </div>
</div> </div>
@else @else

View File

@ -0,0 +1,62 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Models\Deal\Contract;
use App\Models\Deal\DealStatus;
class ContractUpdated extends Notification
{
use Queueable;
private $contract;
/**
* Create a new notification instance.
*/
public function __construct(Contract $contract)
{
/*if ($deal->status != DealStatus::NOT_UNIQUE)
{
throw new \Exception('Notification sending: deal is unique, but request as unique');
}*/
$this->contract = $contract;
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['database'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'contract' => $this->contract->id,
'text' => __('notifications.' . get_class($this), ['contact' => $this->contract->deal->user->name])
];
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Models\Deal\Deal;
use App\Models\Deal\DealStatus;
class NotUniqueContact extends Notification
{
use Queueable;
private $deal;
/**
* Create a new notification instance.
*/
public function __construct(Deal $deal)
{
if ($deal->status != DealStatus::NOT_UNIQUE)
{
//throw new \Exception('Notification sending: deal is unique, but request as unique');
}
$this->deal = $deal;
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['database'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'deal' => $this->deal->id,
'text' => __('notifications.' . get_class($this), ['contact' => $this->deal->user->name])
];
}
}

View File

@ -12,7 +12,8 @@
| has failed, such as for an invalid token or invalid new password. | has failed, such as for an invalid token or invalid new password.
| |
*/ */
'has no notifications' => 'Список уведомлений пуст',
'App\Notifications\UniqueContact' => 'Контакт :contact был проверен, уникальность подтверждена', 'App\Notifications\UniqueContact' => 'Контакт :contact был проверен, уникальность подтверждена',
'App\Notifications\NotUniqueContact' => 'Контакт :contact был проверен и является не уникальным',
'App\Notifications\ContractUpdated' => 'Информация о договоре для контакта ":contact" обновлен',
]; ];