Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 25 additions & 29 deletions app/Helpers/SupportRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Support\Category\SupportCategory;
use App\Models\Support\SupportTicket;
use App\Models\Support\SupportTicketMessage;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
Expand All @@ -32,10 +33,10 @@ public static function nextTicket(?int $category_id): ?SupportTicket
if (! empty($category_id)) {
if (
$category_id > 0 &&
! empty($category = SupportCategory::byId($category_id)) &&
! empty($category = SupportCategory::where('id', '=', $category_id)->first()) &&
$category->assignments->where('user_id', '=', Auth::id())
) {
return SupportTicket::where('category_id', '=', $category_id)
$tickets = SupportTicket::where('category_id', '=', $category->id)
->where('status', '=', 'open')
->whereDoesntHave('history', function (Builder $builder) {
return $builder->where('user_id', '=', Auth::id())
Expand All @@ -46,19 +47,15 @@ public static function nextTicket(?int $category_id): ?SupportTicket
->whereDoesntHave('run', function (Builder $builder) {
return $builder->whereNull('ended_at');
})
->whereHas('messages', function (Builder $builder) {
return $builder
->latest()
->whereHas('user', function (Builder $builder) {
return $builder->where('role', '=', 'customer');
});
})
->orderByDesc('escalated')
->orderBy('hold')
->orderBy('created_at')
->first();
->get();
} elseif ($category_id === 0) {
return SupportTicket::where('category_id', '=', 0)
$tickets = SupportTicket::where(function (Builder $builder) {
return $builder->where('category_id', '=', 0)
->orWhereNull('category_id');
})
->where('status', '=', 'open')
->whereDoesntHave('history', function (Builder $builder) {
return $builder->where('user_id', '=', Auth::id())
Expand All @@ -69,20 +66,13 @@ public static function nextTicket(?int $category_id): ?SupportTicket
->whereDoesntHave('run', function (Builder $builder) {
return $builder->whereNull('ended_at');
})
->whereHas('messages', function (Builder $builder) {
return $builder
->latest()
->whereHas('user', function (Builder $builder) {
return $builder->where('role', '=', 'customer');
});
})
->orderByDesc('escalated')
->orderBy('hold')
->orderBy('created_at')
->first();
->get();
}
} else {
return SupportTicket::where(function (Builder $builder) {
$tickets = SupportTicket::where(function (Builder $builder) {
return $builder->where('category_id', '=', 0)
->orWhereNull('category_id')
->orWhereHas('category', function (Builder $builder) {
Expand All @@ -101,19 +91,25 @@ public static function nextTicket(?int $category_id): ?SupportTicket
->whereDoesntHave('run', function (Builder $builder) {
return $builder->whereNull('ended_at');
})
->whereHas('messages', function (Builder $builder) {
return $builder
->latest()
->whereHas('user', function (Builder $builder) {
return $builder->where('role', '=', 'customer');
});
})
->orderByDesc('escalated')
->orderBy('hold')
->orderBy('created_at')
->first();
->get();
}

return null;
if (empty($tickets)) {
return null;
}

return $tickets->filter(function (SupportTicket $ticket) {
$lastMessage = $ticket->messages->filter(function (SupportTicketMessage $message) {
return !$message->note;
})->last();

return !in_array($lastMessage->user?->role, [
'admin',
'employee',
]);
})->first();
}
}
12 changes: 9 additions & 3 deletions app/Http/Controllers/AdminSupportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ public function support_index_list(Request $request): JsonResponse
})
->orWhere('category_id', '=', 0)
->orWhereNull('category_id');
})->where('category_id', '=', $request->category);
});

if ($request->category) {
$query = $query->where('category_id', '=', $request->category);
} else {
$query = $query->whereNull('category_id');
}

switch ($request->type) {
case 'open':
Expand Down Expand Up @@ -678,7 +684,7 @@ public function support_move(Request $request): RedirectResponse
{
Validator::make($request->toArray(), [
'ticket_id' => ['required', 'integer'],
'category_id' => ['required', 'integer'],
'category_id' => ['nullable', 'integer'],
])->validate();

if (
Expand Down Expand Up @@ -891,7 +897,7 @@ public function support_run_start(Request $request): RedirectResponse
->exists()
) {
/* @var SupportTicket|null $ticket */
if (! empty($ticket = SupportRunHelper::nextTicket($request->category ?? null))) {
if (! empty($ticket = SupportRunHelper::nextTicket($request->category ? (int) $request->category : null))) {
/* @var SupportRun|null $run */
$run = SupportRun::create([
'category_id' => $request->category ?? null,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/CustomerSupportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function support_details(Request $request)
public function support_create(Request $request): RedirectResponse
{
Validator::make($request->toArray(), [
'category_id' => ['required', 'integer'],
'category_id' => ['nullable', 'integer'],
'subject' => ['required', 'string'],
'priority' => ['required', 'string'],
'message' => ['required', 'string'],
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use App\Http\Middleware\Authenticate;
use App\Http\Middleware\EncryptCookies;
use App\Http\Middleware\IdentifyAdminProductLists;
use App\Http\Middleware\IdentifyAdminStatus;
use App\Http\Middleware\IdentifyCustomerProductLists;
use App\Http\Middleware\IdentifyCustomerStatus;
use App\Http\Middleware\IdentifyTenant;
use App\Http\Middleware\InjectNavigateables;
use App\Http\Middleware\InjectShopCategoryOrProduct;
Expand Down Expand Up @@ -112,5 +114,7 @@ class Kernel extends HttpKernel
'shop.categoryOrProduct' => InjectShopCategoryOrProduct::class,
'products.admin' => IdentifyAdminProductLists::class,
'products.customer' => IdentifyCustomerProductLists::class,
'status.admin' => IdentifyAdminStatus::class,
'status.customer' => IdentifyCustomerStatus::class,
];
}
2 changes: 1 addition & 1 deletion app/Http/Middleware/IdentifyAdminProductLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle(Request $request, Closure $next)
{
$results = [];

$products = Products::list()->reject(function ($handler) {
Products::list()->reject(function ($handler) {
return !$handler->ui()->admin;
})->each(function ($handler) use (&$results) {
$results[] = (object) [
Expand Down
70 changes: 70 additions & 0 deletions app/Http/Middleware/IdentifyAdminStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Models\Shop\OrderQueue\ShopOrderQueue;
use App\Models\Support\SupportTicket;
use App\Models\Support\SupportTicketMessage;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
* Class IdentifyAdminStatus.
*
* This class is the middleware for identifying the admin status.
*
* @author Marcel Menk <marcel.menk@ipvx.io>
*/
class IdentifyAdminStatus
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$request->attributes->add([
'badges' => (object) [
'orders' => ShopOrderQueue::where('invalid', '=', false)
->where('approved', '=', true)
->where('disapproved', '=', false)
->where('setup', '=', false)
->where('deleted', '=', false)
->where('fails', '<', 3)
->count(),
'tickets' => SupportTicket::where('status', '=', 'open')
->where(function (Builder $builder) {
return $builder->where('category_id', '=', 0)
->orWhereNull('category_id')
->orWhereHas('category', function (Builder $builder) {
return $builder->whereHas('assignments', function (Builder $builder) {
return $builder->where('user_id', '=', Auth::id());
});
});
})
->get()
->filter(function (SupportTicket $ticket) {
$lastMessage = $ticket->messages->filter(function (SupportTicketMessage $message) {
return !$message->note;
})->last();

return !in_array($lastMessage->user?->role, [
'admin',
'employee',
]);
})
->count(),
],
]);

return $next($request);
}
}
2 changes: 1 addition & 1 deletion app/Http/Middleware/IdentifyCustomerProductLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle(Request $request, Closure $next)
{
$results = [];

$products = Products::list()->reject(function ($handler) {
Products::list()->reject(function ($handler) {
return !$handler->ui()->customer;
})->each(function ($handler) use (&$results) {
$results[] = (object) [
Expand Down
70 changes: 70 additions & 0 deletions app/Http/Middleware/IdentifyCustomerStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Models\Accounting\Invoice\Invoice;
use App\Models\Support\SupportTicket;
use App\Models\Support\SupportTicketMessage;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
* Class IdentifyCustomerStatus.
*
* This class is the middleware for identifying the customer status.
*
* @author Marcel Menk <marcel.menk@ipvx.io>
*/
class IdentifyCustomerStatus
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$invoices = Invoice::where('user_id', '=', Auth::id())
->where('status', '=', 'unpaid')
->whereDoesntHave('type', function (Builder $builder) {
return $builder->where('type', '=', 'prepaid');
})
->get();

$request->attributes->add([
'badges' => (object) [
'invoices' => (object) [
'total' => (clone $invoices)->count(),
'overdue' => (clone $invoices)->filter(function (Invoice $invoice) {
return $invoice->overdue;
})->count(),
],
'tickets' => SupportTicket::whereHas('assignments', function (Builder $builder) {
return $builder->where('user_id', '=', Auth::id());
})
->where('status', '=', 'open')
->get()
->filter(function (SupportTicket $ticket) {
$lastMessage = $ticket->messages->filter(function (SupportTicketMessage $message) {
return !$message->note;
})->last();

return in_array($lastMessage->user?->role, [
'admin',
'employee',
]);
})
->count(),
],
]);

return $next($request);
}
}
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading