1.1 User can request for message from explore page 1.2User cannot message broker if not following 1.3 prepare action which handles creating inbox between user and broker if not exists
33 lines
743 B
PHP
33 lines
743 B
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Models\Inbox;
|
|
use App\Models\User;
|
|
use DB;
|
|
|
|
final readonly class CreateOrGetInboxAction
|
|
{
|
|
/**
|
|
*
|
|
* @throws \Throwable
|
|
*/
|
|
public function execute(User $recipient, User $sender): Inbox
|
|
{
|
|
$existingInbox = Inbox::whereHas('users', fn($q) => $q->where('id', $sender->id))
|
|
->whereHas('users', fn($q) => $q->where('id', $recipient->id))
|
|
->first();
|
|
|
|
if ($existingInbox) {
|
|
return $existingInbox;
|
|
}
|
|
|
|
return DB::transaction(function () use ($sender, $recipient) {
|
|
$inbox = Inbox::create();
|
|
$inbox->users()->attach([$sender->id, $recipient->id]);
|
|
|
|
return $inbox;
|
|
}, 2);
|
|
}
|
|
}
|