Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

1.2 KB
Raw Blame
<?php

declare(strict_types=1);

namespace HiEvents\Http\Actions\Admin\Users;

use HiEvents\Http\Actions\Auth\BaseAuthAction;
use HiEvents\Services\Application\Handlers\Admin\DTO\StartImpersonationDTO;
use HiEvents\Services\Application\Handlers\Admin\StartImpersonationHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class StartImpersonationAction extends BaseAuthAction
{
    public function __construct(
        private readonly StartImpersonationHandler $handler,
    ) {}

    public function __invoke(Request $request, int $userId): JsonResponse
    {
        $this->requireSuperAdmin();

        $this->validate($request, [
            'account_id' => 'required|exists:accounts,id',
        ]);

        $token = $this->handler->handle(new StartImpersonationDTO(
            userId: $userId,
            accountId: $request->input('account_id'),
            impersonatorId: $this->getAuthenticatedUser()->getId(),
        ));

        $response = $this->jsonResponse([
            'message' => __('Impersonation started'),
            'redirect_url' => '/manage/events',
            'token' => $token,
        ]);

        return $this->addTokenToResponse($response, $token);
    }
}