Skip to content
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
36 changes: 36 additions & 0 deletions backend/app/Http/Actions/Admin/Events/GetUpcomingEventsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace HiEvents\Http\Actions\Admin\Events;

use HiEvents\DomainObjects\Enums\Role;
use HiEvents\Http\Actions\BaseAction;
use HiEvents\Resources\Event\EventResource;
use HiEvents\Services\Application\Handlers\Admin\DTO\GetUpcomingEventsDTO;
use HiEvents\Services\Application\Handlers\Admin\GetUpcomingEventsHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class GetUpcomingEventsAction extends BaseAction
{
public function __construct(
private readonly GetUpcomingEventsHandler $handler,
)
{
}

public function __invoke(Request $request): JsonResponse
{
$this->minimumAllowedRole(Role::SUPERADMIN);

$events = $this->handler->handle(new GetUpcomingEventsDTO(
perPage: min((int)$request->query('per_page', 20), 100),
));

return $this->resourceResponse(
resource: EventResource::class,
data: $events
);
}
}
13 changes: 10 additions & 3 deletions backend/app/Repository/Eloquent/AccountRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ public function getAllAccountsWithCounts(?string $search, int $perPage): LengthA
{
$query = $this->model
->select('accounts.*')
->withCount(['events', 'users']);
->withCount(['events', 'users'])
->with(['users' => function ($query) {
$query->select('users.id', 'users.first_name', 'users.last_name', 'users.email')
->withPivot('role');
}]);

if ($search) {
$query->where(function ($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%");
$q->where('accounts.name', 'like', "{$search}%")
->orWhere('accounts.email', 'like', "{$search}%")
->orWhereHas('users', function ($userQuery) use ($search) {
$userQuery->where('users.email', 'like', "{$search}%");
});
});
}

Expand Down
18 changes: 18 additions & 0 deletions backend/app/Repository/Eloquent/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,22 @@ public function findEvents(array $where, QueryParamsDTO $params): LengthAwarePag
page: $params->page,
);
}

public function getUpcomingEventsForAdmin(int $perPage): LengthAwarePaginator
{
$now = now();
$next24Hours = now()->addDay();

return $this->handleResults($this->model
->select('events.*')
->with(['account', 'organizer'])
->where(EventDomainObjectAbstract::START_DATE, '>=', $now)
->where(EventDomainObjectAbstract::START_DATE, '<=', $next24Hours)
->whereIn(EventDomainObjectAbstract::STATUS, [
EventStatus::LIVE->name,
EventStatus::DRAFT->name,
])
->orderBy(EventDomainObjectAbstract::START_DATE, 'asc')
->paginate($perPage));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ interface EventRepositoryInterface extends RepositoryInterface
public function findEventsForOrganizer(int $organizerId, int $accountId, QueryParamsDTO $params): LengthAwarePaginator;

public function findEvents(array $where, QueryParamsDTO $params): LengthAwarePaginator;

public function getUpcomingEventsForAdmin(int $perPage): LengthAwarePaginator;
}
9 changes: 9 additions & 0 deletions backend/app/Resources/Account/AdminAccountResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public function toArray(Request $request): array
'created_at' => $this->resource->created_at,
'events_count' => $this->resource->events_count ?? 0,
'users_count' => $this->resource->users_count ?? 0,
'users' => $this->resource->users->map(function ($user) {
return [
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'role' => $user->pivot->role,
];
}),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace HiEvents\Services\Application\Handlers\Admin\DTO;

use HiEvents\DataTransferObjects\BaseDataObject;

class GetUpcomingEventsDTO extends BaseDataObject
{
public function __construct(
public readonly int $perPage = 20,
)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace HiEvents\Services\Application\Handlers\Admin;

use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Services\Application\Handlers\Admin\DTO\GetUpcomingEventsDTO;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

class GetUpcomingEventsHandler
{
public function __construct(
private readonly EventRepositoryInterface $eventRepository,
)
{
}

public function handle(GetUpcomingEventsDTO $dto): LengthAwarePaginator
{
return $this->eventRepository->getUpcomingEventsForAdmin($dto->perPage);
}
}
2 changes: 2 additions & 0 deletions backend/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
use HiEvents\Http\Actions\Users\UpdateMeAction;
use HiEvents\Http\Actions\Users\UpdateUserAction;
use HiEvents\Http\Actions\Admin\Accounts\GetAllAccountsAction;
use HiEvents\Http\Actions\Admin\Events\GetUpcomingEventsAction;
use HiEvents\Http\Actions\Admin\Stats\GetAdminStatsAction;
use HiEvents\Http\Actions\Admin\Users\GetAllUsersAction;
use HiEvents\Http\Actions\Admin\Users\StartImpersonationAction;
Expand Down Expand Up @@ -373,6 +374,7 @@ function (Router $router): void {
$router->get('/stats', GetAdminStatsAction::class);
$router->get('/accounts', GetAllAccountsAction::class);
$router->get('/users', GetAllUsersAction::class);
$router->get('/events/upcoming', GetUpcomingEventsAction::class);
$router->post('/impersonate/{user_id}', StartImpersonationAction::class);
$router->post('/stop-impersonation', StopImpersonationAction::class);
}
Expand Down
20 changes: 19 additions & 1 deletion frontend/src/api/admin.client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {api} from "./client";
import {GenericDataResponse, GenericPaginatedResponse, IdParam, User} from "../types";
import {GenericPaginatedResponse, IdParam, User} from "../types";

export interface AdminUser extends User {
accounts?: AccountWithRole[];
Expand All @@ -12,6 +12,14 @@ export interface AccountWithRole {
role: string;
}

export interface AdminAccountUser {
id: IdParam;
first_name: string;
last_name: string;
email: string;
role: string;
}

export interface AdminAccount {
id: IdParam;
name: string;
Expand All @@ -21,6 +29,7 @@ export interface AdminAccount {
created_at: string;
events_count: number;
users_count: number;
users: AdminAccountUser[];
}

export interface AdminStats {
Expand Down Expand Up @@ -86,6 +95,15 @@ export const adminClient = {
return response.data;
},

getUpcomingEvents: async (perPage: number = 10) => {
const response = await api.get<GenericPaginatedResponse<any>>('admin/events/upcoming', {
params: {
per_page: perPage,
}
});
return response.data;
},

startImpersonation: async (userId: IdParam, accountId: IdParam) => {
const response = await api.post<StartImpersonationResponse>(
`admin/impersonate/${userId}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,35 @@
text-align: center;
padding: 3rem 1rem;
}

.usersSection {
padding-top: 1rem;
border-top: 1px solid var(--mantine-color-default-border);
}

.usersList {
display: flex;
flex-direction: column;
gap: 0.75rem;
}

.userItem {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.75rem;
background: var(--mantine-color-gray-0);
border-radius: var(--mantine-radius-sm);
transition: background-color 0.2s;

&:hover {
background: var(--mantine-color-gray-1);
}
}

.userDetails {
display: flex;
flex-direction: column;
gap: 0.25rem;
flex: 1;
}
60 changes: 58 additions & 2 deletions frontend/src/components/common/AdminAccountsTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {Badge, Stack, Text} from "@mantine/core";
import {Badge, Button, Stack, Text} from "@mantine/core";
import {t} from "@lingui/macro";
import {AdminAccount} from "../../../api/admin.client";
import {IconCalendar, IconWorld, IconBuildingBank, IconUsers} from "@tabler/icons-react";
import classes from "./AdminAccountsTable.module.scss";
import {IdParam} from "../../../types";

interface AdminAccountsTableProps {
accounts: AdminAccount[];
onImpersonate: (userId: IdParam, accountId: IdParam) => void;
isLoading?: boolean;
}

const AdminAccountsTable = ({accounts}: AdminAccountsTableProps) => {
const AdminAccountsTable = ({accounts, onImpersonate, isLoading}: AdminAccountsTableProps) => {
if (!accounts || accounts.length === 0) {
return (
<div className={classes.emptyState}>
Expand All @@ -23,6 +26,24 @@ const AdminAccountsTable = ({accounts}: AdminAccountsTableProps) => {
return date.toLocaleDateString();
};

const getRoleBadgeColor = (role: string) => {
switch (role) {
case 'ADMIN':
return 'blue';
case 'ORGANIZER':
return 'green';
case 'SUPERADMIN':
return 'red';
default:
return 'gray';
}
};

const canImpersonate = (role: string) => {
return role !== 'SUPERADMIN';
};


return (
<div className={classes.cardsContainer}>
{accounts.map((account) => (
Expand Down Expand Up @@ -52,6 +73,41 @@ const AdminAccountsTable = ({accounts}: AdminAccountsTableProps) => {
</div>
</div>

{account.users && account.users.length > 0 && (
<div className={classes.usersSection}>
<Text size="sm" fw={600} mb="xs">{t`Users`}</Text>
<div className={classes.usersList}>
{account.users.map((user) => (
<div key={user.id} className={classes.userItem}>
<div className={classes.userDetails}>
<Text size="sm" fw={500}>
{user.first_name} {user.last_name}
</Text>
<Text size="xs" c="dimmed">{user.email}</Text>
<Badge
size="xs"
color={getRoleBadgeColor(user.role)}
mt={4}
>
{user.role}
</Badge>
</div>
{canImpersonate(user.role) && (
<Button
size="xs"
variant="light"
onClick={() => onImpersonate(user.id, account.id)}
disabled={isLoading}
>
{t`Impersonate`}
</Button>
)}
</div>
))}
</div>
</div>
)}

<div className={classes.cardFooter}>
<div className={classes.footerInfo}>
<div className={classes.footerItem}>
Expand Down
32 changes: 31 additions & 1 deletion frontend/src/components/routes/admin/Accounts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import {t} from "@lingui/macro";
import {IconSearch} from "@tabler/icons-react";
import {useState, useEffect} from "react";
import {useGetAllAccounts} from "../../../../queries/useGetAllAccounts";
import {useStartImpersonation} from "../../../../mutations/useStartImpersonation";
import AdminAccountsTable from "../../../common/AdminAccountsTable";
import {showError, showSuccess} from "../../../../utilites/notifications";
import {IdParam} from "../../../../types";
import {useNavigate} from "react-router";

const Accounts = () => {
const navigate = useNavigate();
const [page, setPage] = useState(1);
const [search, setSearch] = useState("");
const [debouncedSearch, setDebouncedSearch] = useState("");
Expand All @@ -16,6 +21,8 @@ const Accounts = () => {
search: debouncedSearch
});

const startImpersonationMutation = useStartImpersonation();

useEffect(() => {
const timer = setTimeout(() => {
setDebouncedSearch(search);
Expand All @@ -25,6 +32,25 @@ const Accounts = () => {
return () => clearTimeout(timer);
}, [search]);

const handleImpersonate = (userId: IdParam, accountId: IdParam) => {
startImpersonationMutation.mutate({userId, accountId}, {
onSuccess: (response) => {
showSuccess(response.message || t`Impersonation started`);
if (response.redirect_url) {
window.location.href = response.redirect_url;
} else {
navigate('/manage/events');
}
},
onError: (error: any) => {
showError(
error?.response?.data?.message ||
t`Failed to start impersonation. Please try again.`
);
}
});
};

return (
<Container size="xl" p="xl">
<Stack gap="lg">
Expand All @@ -44,7 +70,11 @@ const Accounts = () => {
<Skeleton height={180} radius="md" />
</Stack>
) : (
<AdminAccountsTable accounts={accountsData?.data || []} />
<AdminAccountsTable
accounts={accountsData?.data || []}
onImpersonate={handleImpersonate}
isLoading={startImpersonationMutation.isPending}
/>
)}

{accountsData?.meta && accountsData.meta.last_page > 1 && (
Expand Down
Loading
Loading