Enhance chat functionality: add support for vendor-specific chat channels, implement methods for managing conversations with admin and store owners, and improve conversation payload structure. Update middleware and policies for better access control, and refine chat UI to indicate view-only status for store owner replies.
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Support;
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\ChatUserResource;
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\MessageResource;
|
||||
use App\Models\Chat\Conversation;
|
||||
use App\Models\Ecommerce\Channel\Channel;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -46,11 +47,81 @@ class ChatSupport
|
||||
return false;
|
||||
}
|
||||
|
||||
return true; // TODO: Remove this
|
||||
|
||||
return $user->isMe() || $user->isAdmin() || $user->isDirektor() || $user->isManager();
|
||||
}
|
||||
|
||||
public static function canAccessPanelChat(?User $user): bool
|
||||
{
|
||||
if ($user === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static::isOperator($user)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $user->isVendor() && $user->channels()->exists();
|
||||
}
|
||||
|
||||
public static function ownsChannel(?User $user, ?Channel $channel): bool
|
||||
{
|
||||
if ($user === null || $channel === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $channel->channelables_type === User::class
|
||||
&& (int) $channel->channelables_id === (int) $user->id;
|
||||
}
|
||||
|
||||
public static function canReply(?User $user, Conversation $conversation): bool
|
||||
{
|
||||
if ($user === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static::isOperator($user)) {
|
||||
return $conversation->isSupportThread();
|
||||
}
|
||||
|
||||
if (! $user->isVendor() || ! $conversation->channel_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$conversation->loadMissing('channel');
|
||||
|
||||
return static::ownsChannel($user, $conversation->channel)
|
||||
&& $conversation->hasParticipant($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{available: bool, name: string|null}
|
||||
*/
|
||||
public static function adminTarget(?User $viewer): array
|
||||
{
|
||||
$id = static::userId();
|
||||
|
||||
if ($id === null || ($viewer && (int) $viewer->id === $id)) {
|
||||
return [
|
||||
'available' => false,
|
||||
'name' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$user = User::query()->find($id);
|
||||
|
||||
if (! $user) {
|
||||
return [
|
||||
'available' => false,
|
||||
'name' => null,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'available' => true,
|
||||
'name' => $user->fullname ?: 'Admin',
|
||||
];
|
||||
}
|
||||
|
||||
public static function conversationIncludesSupport(int $conversationId): bool
|
||||
{
|
||||
$id = static::userId();
|
||||
@@ -68,11 +139,12 @@ class ChatSupport
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function conversationPayload(Conversation $conversation, User $viewer, bool $includePhone = false): array
|
||||
public static function conversationPayload(Conversation $conversation, User $actor, bool $includePhone = false): array
|
||||
{
|
||||
$conversation->loadMissing(['users', 'latestMessage.user']);
|
||||
$conversation->loadMissing(['users', 'latestMessage.user', 'channel', 'customer']);
|
||||
|
||||
$otherUser = $conversation->users->firstWhere('id', '!=', $viewer->id);
|
||||
$otherUser = $conversation->customer
|
||||
?? $conversation->users->firstWhere('id', '!=', $actor->id);
|
||||
|
||||
$user = $otherUser ? (new ChatUserResource($otherUser))->resolve() : null;
|
||||
|
||||
@@ -80,9 +152,18 @@ class ChatSupport
|
||||
$user['phone_number'] = $otherUser->phone_number;
|
||||
}
|
||||
|
||||
$channel = $conversation->channel;
|
||||
|
||||
return [
|
||||
'id' => $conversation->id,
|
||||
'thread_key' => $conversation->thread_key,
|
||||
'user' => $user,
|
||||
'channel' => $channel ? [
|
||||
'id' => $channel->id,
|
||||
'name' => $channel->name,
|
||||
'slug' => $channel->slug,
|
||||
] : null,
|
||||
'can_reply' => static::canReply($actor, $conversation),
|
||||
'last_message' => $conversation->latestMessage
|
||||
? (new MessageResource($conversation->latestMessage))->resolve()
|
||||
: null,
|
||||
|
||||
Reference in New Issue
Block a user