180 lines
4.8 KiB
PHP
180 lines
4.8 KiB
PHP
<?php
|
|
|
|
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;
|
|
|
|
class ChatSupport
|
|
{
|
|
public static function userId(): ?int
|
|
{
|
|
$id = (int) config('chat.support_user_id');
|
|
|
|
return $id > 0 ? $id : null;
|
|
}
|
|
|
|
public static function user(): User
|
|
{
|
|
$id = static::userId();
|
|
|
|
abort_unless($id, 422, 'Chat support user is not configured.');
|
|
|
|
$user = User::query()->find($id);
|
|
|
|
abort_unless($user, 422, 'Chat support user was not found.');
|
|
|
|
return $user;
|
|
}
|
|
|
|
public static function isSupportUser(?User $user): bool
|
|
{
|
|
$id = static::userId();
|
|
|
|
return $user !== null && $id !== null && (int) $user->id === $id;
|
|
}
|
|
|
|
/**
|
|
* Nova staff who may operate the support inbox (vendors excluded).
|
|
*/
|
|
public static function isOperator(?User $user): bool
|
|
{
|
|
if ($user === null) {
|
|
return false;
|
|
}
|
|
|
|
if (static::isSupportUser($user)) {
|
|
return true;
|
|
}
|
|
|
|
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();
|
|
|
|
if ($id === null) {
|
|
return false;
|
|
}
|
|
|
|
return DB::table('conversation_user')
|
|
->where('conversation_id', $conversationId)
|
|
->where('user_id', $id)
|
|
->exists();
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public static function conversationPayload(Conversation $conversation, User $actor, bool $includePhone = false): array
|
|
{
|
|
$conversation->loadMissing(['users', 'latestMessage.user', 'channel', 'customer']);
|
|
|
|
$otherUser = $conversation->customer
|
|
?? $conversation->users->firstWhere('id', '!=', $actor->id);
|
|
|
|
$user = $otherUser ? (new ChatUserResource($otherUser))->resolve() : null;
|
|
|
|
if ($includePhone && $otherUser && is_array($user)) {
|
|
$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,
|
|
'unread_count' => (int) ($conversation->unread_count ?? 0),
|
|
'created_at' => $conversation->created_at,
|
|
'updated_at' => $conversation->updated_at,
|
|
];
|
|
}
|
|
}
|