initial commit
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\Booking;
|
||||
use App\Models\DailyEstablishmentStat;
|
||||
use App\Models\Machine;
|
||||
use App\Models\PricingRule;
|
||||
use App\Models\Promotion;
|
||||
use App\Models\Supervisor;
|
||||
use App\Models\Wash;
|
||||
use Closure;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class EnsureSupervisorScope
|
||||
{
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$supervisor = $this->resolveSupervisor($request);
|
||||
|
||||
if ($supervisor === null) {
|
||||
return response()->json([
|
||||
'message' => 'Superviseur non authentifié.',
|
||||
], 401);
|
||||
}
|
||||
|
||||
if (! $supervisor->is_active) {
|
||||
return response()->json([
|
||||
'message' => 'Compte superviseur désactivé.',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$request->attributes->set('supervisor', $supervisor);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
public static function resolveSupervisor(Request $request): ?Supervisor
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
return $user instanceof Supervisor ? $user : null;
|
||||
}
|
||||
|
||||
public static function machinesQuery(Supervisor $supervisor): Builder
|
||||
{
|
||||
$query = Machine::query()->with(['establishment', 'machineIntegration']);
|
||||
|
||||
return self::applyEstablishmentScope($query, $supervisor, 'establishment_id');
|
||||
}
|
||||
|
||||
public static function bookingsQuery(Supervisor $supervisor): Builder
|
||||
{
|
||||
$query = Booking::query()
|
||||
->with(['user', 'machine.establishment'])
|
||||
->whereHas('machine.establishment', function (Builder $query) use ($supervisor) {
|
||||
self::applyOrganizationFilter($query, $supervisor);
|
||||
});
|
||||
|
||||
if ($supervisor->establishment_id !== null) {
|
||||
$query->whereHas('machine', fn (Builder $q) => $q->where('establishment_id', $supervisor->establishment_id));
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public static function washesQuery(Supervisor $supervisor): Builder
|
||||
{
|
||||
$query = Wash::query()
|
||||
->with(['user', 'machine.establishment', 'booking'])
|
||||
->whereHas('machine.establishment', function (Builder $query) use ($supervisor) {
|
||||
self::applyOrganizationFilter($query, $supervisor);
|
||||
});
|
||||
|
||||
if ($supervisor->establishment_id !== null) {
|
||||
$query->whereHas('machine', fn (Builder $q) => $q->where('establishment_id', $supervisor->establishment_id));
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public static function pricingRulesQuery(Supervisor $supervisor): Builder
|
||||
{
|
||||
$query = PricingRule::query()->with(['establishment', 'machine']);
|
||||
|
||||
return self::applyEstablishmentScope($query, $supervisor, 'establishment_id');
|
||||
}
|
||||
|
||||
public static function promotionsQuery(Supervisor $supervisor): Builder
|
||||
{
|
||||
$query = Promotion::query()->with('establishment');
|
||||
|
||||
return self::applyEstablishmentScope($query, $supervisor, 'establishment_id');
|
||||
}
|
||||
|
||||
public static function dailyStatsQuery(Supervisor $supervisor): Builder
|
||||
{
|
||||
$query = DailyEstablishmentStat::query()->with('establishment');
|
||||
|
||||
return self::applyEstablishmentScope($query, $supervisor, 'establishment_id');
|
||||
}
|
||||
|
||||
private static function applyEstablishmentScope(Builder $query, Supervisor $supervisor, string $column): Builder
|
||||
{
|
||||
if ($supervisor->establishment_id !== null) {
|
||||
return $query->where($column, $supervisor->establishment_id);
|
||||
}
|
||||
|
||||
return $query->whereHas('establishment', function (Builder $query) use ($supervisor) {
|
||||
self::applyOrganizationFilter($query, $supervisor);
|
||||
});
|
||||
}
|
||||
|
||||
private static function applyOrganizationFilter(Builder $query, Supervisor $supervisor): void
|
||||
{
|
||||
$query->where('organization_id', $supervisor->organization_id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user