Admin: nadawanie zawieszenia/bana w wizardzie + badge statusu na liscie i w profilu
This commit is contained in:
@@ -33,6 +33,9 @@ export interface User {
|
||||
canDoMuscles: string[];
|
||||
difficultMuscles: string[];
|
||||
avoidMuscles: string[];
|
||||
status: 'ACTIVE' | 'SUSPENDED' | 'BANNED';
|
||||
suspensionUntil: string | null;
|
||||
restrictionReason: string | null;
|
||||
createdAt: string;
|
||||
memberships?: { gymId: string; roles: GymRole[]; gym: Gym }[];
|
||||
}
|
||||
|
||||
@@ -216,6 +216,15 @@ export default function UserDetail() {
|
||||
</div>
|
||||
</div>
|
||||
<span className="flex flex-wrap items-center gap-1.5">
|
||||
{user.status === 'SUSPENDED' && (
|
||||
<Badge color="yellow">
|
||||
Zawieszone
|
||||
{user.suspensionUntil
|
||||
? ` do ${new Intl.DateTimeFormat('pl-PL', { dateStyle: 'medium', timeStyle: 'short' }).format(new Date(user.suspensionUntil))}`
|
||||
: ''}
|
||||
</Badge>
|
||||
)}
|
||||
{user.status === 'BANNED' && <Badge color="red">Zablokowane</Badge>}
|
||||
{user.nickname && <Badge color="blue">@{user.nickname}</Badge>}
|
||||
{user.isAdmin && <Badge color="blue">Admin</Badge>}
|
||||
{memberships.length > 0 && <Badge color="gray">{memberships.length} sił.</Badge>}
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
Label,
|
||||
Loading,
|
||||
Modal,
|
||||
Select,
|
||||
} from '../../components/ui';
|
||||
import { useTopbar } from '../../topbar';
|
||||
import { useAdminUser, useUserGyms } from './user-queries';
|
||||
@@ -31,6 +32,15 @@ interface MembershipRow {
|
||||
existing: boolean;
|
||||
}
|
||||
|
||||
/** Wartość pola datetime-local (czas lokalny) na ISO UTC do API. */
|
||||
const toIsoUtc = (localValue: string) => new Date(localValue).toISOString();
|
||||
|
||||
/** ISO UTC na wartość input[type=datetime-local] (czas lokalny). */
|
||||
const toDatetimeLocal = (iso: string) => {
|
||||
const d = new Date(iso);
|
||||
return new Date(d.getTime() - d.getTimezoneOffset() * 60000).toISOString().slice(0, 16);
|
||||
};
|
||||
|
||||
let rowSeq = 0;
|
||||
const nextKey = () => ++rowSeq;
|
||||
|
||||
@@ -50,9 +60,10 @@ function AdminExplain({ open }: { open: boolean }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Kreator tworzenia/edycji użytkownika — jedyne miejsce edycji (także członkostw).
|
||||
* Kreator tworzenia/edycji użytkownika — jedyne miejsce edycji (także członkostw i kar).
|
||||
* Tworzenie: dane + role + członkostwa (POST-y po utworzeniu konta).
|
||||
* Edycja: PATCH konta + porządkowanie członkostw (POST/PATCH/DELETE wg zmian w kroku 3);
|
||||
* Edycja: PATCH konta (w tym status: zawieszenie/ban w kroku „Dostęp")
|
||||
* + porządkowanie członkostw (POST/PATCH/DELETE wg zmian);
|
||||
* strona „Członkostwa w siłowniach" jest tylko do odczytu.
|
||||
*/
|
||||
export default function UserForm() {
|
||||
@@ -95,6 +106,9 @@ export default function UserForm() {
|
||||
email: '',
|
||||
age: '',
|
||||
isAdmin: false,
|
||||
status: 'ACTIVE' as User['status'],
|
||||
suspensionUntil: '',
|
||||
restrictionReason: '',
|
||||
});
|
||||
const [adminExplain, setAdminExplain] = useState(false);
|
||||
// domyślnie zero wierszy — pierwszą siłownię dodaje admin (mile widziana w stanie pustym)
|
||||
@@ -120,6 +134,9 @@ export default function UserForm() {
|
||||
email: user.email,
|
||||
age: user.age?.toString() ?? '',
|
||||
isAdmin: user.isAdmin,
|
||||
status: user.status,
|
||||
suspensionUntil: user.suspensionUntil ? toDatetimeLocal(user.suspensionUntil) : '',
|
||||
restrictionReason: user.restrictionReason ?? '',
|
||||
});
|
||||
setRows(
|
||||
(user.memberships ?? []).map((m) => ({
|
||||
@@ -157,6 +174,14 @@ export default function UserForm() {
|
||||
return 'Nowe hasło musi mieć co najmniej 8 znaków.';
|
||||
// w edycji oba pola mogą zostać puste (bez zmiany hasła); w tworzeniu muszą się zgadzać
|
||||
if (form.password !== form.password2) return 'Podane hasła muszą być takie same.';
|
||||
if (isEdit && form.status === 'SUSPENDED') {
|
||||
if (!form.suspensionUntil) return 'Podaj datę, do kiedy konto ma być zawieszone.';
|
||||
if (new Date(form.suspensionUntil) <= new Date())
|
||||
return 'Data zawieszenia musi być w przyszłości.';
|
||||
}
|
||||
if (isEdit && form.status === 'BANNED' && !form.restrictionReason.trim()) {
|
||||
return 'Podaj powód blokady konta.';
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
@@ -226,6 +251,12 @@ export default function UserForm() {
|
||||
lastName: form.lastName || null,
|
||||
age: form.age ? parseInt(form.age) : null,
|
||||
isAdmin: form.isAdmin,
|
||||
status: form.status,
|
||||
suspensionUntil:
|
||||
form.status === 'SUSPENDED' && form.suspensionUntil
|
||||
? toIsoUtc(form.suspensionUntil)
|
||||
: null,
|
||||
restrictionReason: form.status === 'ACTIVE' ? null : form.restrictionReason.trim() || null,
|
||||
};
|
||||
if (form.password) patchBody.password = form.password;
|
||||
await api(`/admin/users/${id}`, { method: 'PATCH', body: patchBody });
|
||||
@@ -329,11 +360,54 @@ export default function UserForm() {
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* Krok Dostęp — kara na koncie (tylko edycja: konto musi najpierw istnieć) */}
|
||||
{isEdit && (
|
||||
<Card>
|
||||
<div className="mb-3 flex items-center gap-2.5">
|
||||
<span className="grid h-7 w-7 place-items-center rounded-full bg-accent/10 text-[0.8rem] font-bold text-accent-text">
|
||||
2
|
||||
</span>
|
||||
<H3 className="mb-0">Dostęp</H3>
|
||||
</div>
|
||||
<Field label="Status konta">
|
||||
<Select
|
||||
aria-label="Status konta"
|
||||
value={form.status}
|
||||
onChange={(e) => setForm({ ...form, status: e.target.value as User['status'] })}
|
||||
>
|
||||
<option value="ACTIVE">Aktywne</option>
|
||||
<option value="SUSPENDED">Zawieszone</option>
|
||||
<option value="BANNED">Zablokowane</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{form.status === 'SUSPENDED' && (
|
||||
<Field label="Zawieszone do">
|
||||
<Input
|
||||
type="datetime-local"
|
||||
aria-label="Zawieszone do"
|
||||
value={form.suspensionUntil}
|
||||
onChange={(e) => setForm({ ...form, suspensionUntil: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
{form.status !== 'ACTIVE' && (
|
||||
<Field label="Powód">
|
||||
<Input
|
||||
aria-label="Powód kary"
|
||||
value={form.restrictionReason}
|
||||
onChange={(e) => setForm({ ...form, restrictionReason: e.target.value })}
|
||||
placeholder={form.status === 'BANNED' ? 'np. udostępnianie konta' : 'opcjonalnie'}
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Krok 2 — role w panelu */}
|
||||
<Card>
|
||||
<div className="mb-3 flex items-center gap-2.5">
|
||||
<span className="grid h-7 w-7 place-items-center rounded-full bg-accent/10 text-[0.8rem] font-bold text-accent-text">
|
||||
2
|
||||
{isEdit ? 3 : 2}
|
||||
</span>
|
||||
<H3 className="mb-0">Role</H3>
|
||||
</div>
|
||||
@@ -375,7 +449,7 @@ export default function UserForm() {
|
||||
<Card>
|
||||
<div className="mb-3 flex items-center gap-2.5">
|
||||
<span className="grid h-7 w-7 place-items-center rounded-full bg-accent/10 text-[0.8rem] font-bold text-accent-text">
|
||||
3
|
||||
{isEdit ? 4 : 3}
|
||||
</span>
|
||||
<div className="flex min-w-0 flex-1 items-center justify-between gap-3">
|
||||
<H3 className="mb-0">Członkostwa w siłowniach</H3>
|
||||
|
||||
@@ -63,6 +63,8 @@ export default function UsersList() {
|
||||
}
|
||||
actions={
|
||||
<>
|
||||
{u.status === 'SUSPENDED' && <Badge color="yellow">Zawieszone</Badge>}
|
||||
{u.status === 'BANNED' && <Badge color="red">Zablokowane</Badge>}
|
||||
{u.isAdmin && <Badge color="blue">Admin</Badge>}
|
||||
<Badge color="gray">{u.memberships?.length ?? 0} sił.</Badge>
|
||||
</>
|
||||
|
||||
@@ -23,6 +23,9 @@ const ADMIN: User = {
|
||||
canDoMuscles: [],
|
||||
difficultMuscles: [],
|
||||
avoidMuscles: [],
|
||||
status: 'ACTIVE',
|
||||
suspensionUntil: null,
|
||||
restrictionReason: null,
|
||||
createdAt: '2026-08-22T10:00:00.000Z',
|
||||
};
|
||||
|
||||
|
||||
@@ -34,6 +34,9 @@ const U1: User = {
|
||||
canDoMuscles: [],
|
||||
difficultMuscles: [],
|
||||
avoidMuscles: [],
|
||||
status: 'ACTIVE',
|
||||
suspensionUntil: null,
|
||||
restrictionReason: null,
|
||||
createdAt: '2026-08-01T10:00:00.000Z',
|
||||
memberships: [{ gymId: 'g1', roles: ['OWNER', 'TRAINER'], gym: G1 }],
|
||||
};
|
||||
@@ -84,6 +87,42 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe('admin UserDetail (przegląd profilu)', () => {
|
||||
it('pokazuje badge bana na koncie zablokowanym', async () => {
|
||||
mockFetchRoutes([
|
||||
['/admin/users', () => ({ users: [{ ...U1, status: 'BANNED', restrictionReason: 'Powód' }] })],
|
||||
['/killstreak', () => ({ killstreak: 0 })],
|
||||
['/body-parameters', () => ({ weight: [], circumferences: [] })],
|
||||
['/history', () => ({ sessions: [] })],
|
||||
['/claims', () => ({ claims: [] })],
|
||||
]);
|
||||
renderUserDetail();
|
||||
expect(await screen.findByText('Zablokowane')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('pokazuje badge zawieszenia z terminem', async () => {
|
||||
mockFetchRoutes([
|
||||
[
|
||||
'/admin/users',
|
||||
() => ({
|
||||
users: [
|
||||
{
|
||||
...U1,
|
||||
status: 'SUSPENDED',
|
||||
suspensionUntil: '2026-11-01T12:00:00.000Z',
|
||||
restrictionReason: null,
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
['/killstreak', () => ({ killstreak: 0 })],
|
||||
['/body-parameters', () => ({ weight: [], circumferences: [] })],
|
||||
['/history', () => ({ sessions: [] })],
|
||||
['/claims', () => ({ claims: [] })],
|
||||
]);
|
||||
renderUserDetail();
|
||||
expect(await screen.findByText(/Zawieszone do/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renderuje hero, mini-statystyki i wiersze sekcji', async () => {
|
||||
mockFetchRoutes([
|
||||
['/admin/users', () => ({ users: [U1] })],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { act, render, screen, within } from '@testing-library/react';
|
||||
import { act, fireEvent, render, screen, within } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { MemoryRouter, Route, Routes, useParams } from 'react-router-dom';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
@@ -51,6 +51,9 @@ function emptyUser(over: Partial<User>): User {
|
||||
canDoMuscles: [],
|
||||
difficultMuscles: [],
|
||||
avoidMuscles: [],
|
||||
status: 'ACTIVE',
|
||||
suspensionUntil: null,
|
||||
restrictionReason: null,
|
||||
createdAt: '2026-08-01T10:00:00.000Z',
|
||||
memberships: [],
|
||||
...over,
|
||||
@@ -357,4 +360,72 @@ describe('admin UserForm — kreator edycji', () => {
|
||||
);
|
||||
expect(del).toBeDefined();
|
||||
});
|
||||
|
||||
it('blokuje nadanie bany bez powodu — zero żądań PATCH', async () => {
|
||||
const calls = mockRoutes([
|
||||
{ match: '/admin/gyms', reply: () => ({ gyms: [GYM_A, GYM_B] }) },
|
||||
{ method: 'PATCH', match: '/admin/users/u1', reply: () => ({ user: EDITED }) },
|
||||
{ match: '/admin/users', reply: () => ({ users: [EDITED] }) },
|
||||
]);
|
||||
const user = userEvent.setup();
|
||||
renderForm('/users/u1/edit');
|
||||
await screen.findByDisplayValue('Fit Factory');
|
||||
|
||||
await user.selectOptions(screen.getByRole('combobox', { name: 'Status konta' }), 'BANNED');
|
||||
await submitForm();
|
||||
|
||||
expect(await screen.findByText('Podaj powód blokady konta.')).toBeInTheDocument();
|
||||
expect(
|
||||
calls.some((c) => c.init?.method === 'PATCH' && c.url.endsWith('/admin/users/u1')),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('nadanie bany PATCHuje status z powodem', async () => {
|
||||
const calls = mockRoutes([
|
||||
{ match: '/admin/gyms', reply: () => ({ gyms: [GYM_A, GYM_B] }) },
|
||||
{ method: 'PATCH', match: '/admin/users/u1', reply: () => ({ user: EDITED }) },
|
||||
{ match: '/admin/users', reply: () => ({ users: [EDITED] }) },
|
||||
]);
|
||||
const user = userEvent.setup();
|
||||
renderForm('/users/u1/edit');
|
||||
await screen.findByDisplayValue('Fit Factory');
|
||||
|
||||
await user.selectOptions(screen.getByRole('combobox', { name: 'Status konta' }), 'BANNED');
|
||||
await user.type(screen.getByRole('textbox', { name: 'Powód kary' }), 'Sprzedaż dostępu');
|
||||
await submitForm();
|
||||
|
||||
expect(await screen.findByText('DETAIL-u1')).toBeInTheDocument();
|
||||
const patch = calls.find(
|
||||
(c) => c.init?.method === 'PATCH' && c.url.endsWith('/admin/users/u1'),
|
||||
);
|
||||
expect(JSON.parse(patch?.init?.body as string)).toMatchObject({
|
||||
status: 'BANNED',
|
||||
restrictionReason: 'Sprzedaż dostępu',
|
||||
});
|
||||
});
|
||||
|
||||
it('zawieszenie PATCHuje status z terminem (ISO)', async () => {
|
||||
const calls = mockRoutes([
|
||||
{ match: '/admin/gyms', reply: () => ({ gyms: [GYM_A, GYM_B] }) },
|
||||
{ method: 'PATCH', match: '/admin/users/u1', reply: () => ({ user: EDITED }) },
|
||||
{ match: '/admin/users', reply: () => ({ users: [EDITED] }) },
|
||||
]);
|
||||
const user = userEvent.setup();
|
||||
renderForm('/users/u1/edit');
|
||||
await screen.findByDisplayValue('Fit Factory');
|
||||
|
||||
await user.selectOptions(screen.getByRole('combobox', { name: 'Status konta' }), 'SUSPENDED');
|
||||
// input[type=datetime-local] nie ma roli dostępnej w jsdom — ustawiamy wartość przez change
|
||||
const until = document.querySelector('input[type="datetime-local"]') as HTMLInputElement;
|
||||
fireEvent.change(until, { target: { value: '2099-01-15T18:00' } });
|
||||
await submitForm();
|
||||
|
||||
expect(await screen.findByText('DETAIL-u1')).toBeInTheDocument();
|
||||
const patch = calls.find(
|
||||
(c) => c.init?.method === 'PATCH' && c.url.endsWith('/admin/users/u1'),
|
||||
);
|
||||
const body = JSON.parse(patch?.init?.body as string);
|
||||
expect(body.status).toBe('SUSPENDED');
|
||||
expect(new Date(body.suspensionUntil).getTime()).toBe(new Date('2099-01-15T18:00').getTime());
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,6 +54,9 @@ const U1: User = {
|
||||
canDoMuscles: [],
|
||||
difficultMuscles: [],
|
||||
avoidMuscles: ['legs'],
|
||||
status: 'ACTIVE',
|
||||
suspensionUntil: null,
|
||||
restrictionReason: null,
|
||||
createdAt: '2026-08-01T10:00:00.000Z',
|
||||
memberships: [],
|
||||
};
|
||||
|
||||
@@ -35,6 +35,9 @@ const U1: User = {
|
||||
canDoMuscles: [],
|
||||
difficultMuscles: [],
|
||||
avoidMuscles: [],
|
||||
status: 'ACTIVE',
|
||||
suspensionUntil: null,
|
||||
restrictionReason: null,
|
||||
createdAt: '2026-08-01T10:00:00.000Z',
|
||||
memberships: [{ gymId: 'g1', roles: ['OWNER', 'TRAINER'], gym: G1 }],
|
||||
};
|
||||
|
||||
@@ -23,6 +23,9 @@ const U1: User = {
|
||||
canDoMuscles: [],
|
||||
difficultMuscles: [],
|
||||
avoidMuscles: [],
|
||||
status: 'ACTIVE',
|
||||
suspensionUntil: null,
|
||||
restrictionReason: null,
|
||||
createdAt: '2026-08-01T10:00:00.000Z',
|
||||
memberships: [],
|
||||
};
|
||||
@@ -43,6 +46,9 @@ const U2: User = {
|
||||
canDoMuscles: [],
|
||||
difficultMuscles: [],
|
||||
avoidMuscles: [],
|
||||
status: 'ACTIVE',
|
||||
suspensionUntil: null,
|
||||
restrictionReason: null,
|
||||
createdAt: '2026-08-02T10:00:00.000Z',
|
||||
memberships: [],
|
||||
};
|
||||
|
||||
@@ -174,6 +174,61 @@ test('usunięcie członkostwa w kreatorze edycji — zapis wysyła DELETE', asyn
|
||||
await expect(page).toHaveURL(/\/users\/user-1$/);
|
||||
});
|
||||
|
||||
test('nadanie bany w kreatorze — zapis PATCHuje status i powód', async ({ page }) => {
|
||||
mockApi(page, { gyms: { gyms: [TEST_GYM] }, users: { users: [TEST_USER] } });
|
||||
await loginAsAdmin(page);
|
||||
await page.getByRole('link', { name: 'Użytkownicy' }).click();
|
||||
await page.getByRole('link', { name: 'Jan Kowalski' }).click();
|
||||
await page.getByRole('button', { name: 'Edytuj' }).click();
|
||||
|
||||
await page.getByRole('combobox', { name: 'Status konta' }).selectOption('BANNED');
|
||||
// ban bez powodu — zapis wzbroniony (walidacja reaktywna, zero żądań)
|
||||
await page.getByRole('button', { name: 'Zapisz' }).click();
|
||||
await expect(page.getByText('Podaj powód blokady konta.')).toBeVisible();
|
||||
|
||||
await page.getByRole('textbox', { name: 'Powód kary' }).fill('Sprzedaż dostępu');
|
||||
const patchPromise = page.waitForRequest(
|
||||
(req) => req.method() === 'PATCH' && req.url().endsWith('/admin/users/user-1'),
|
||||
);
|
||||
await page.getByRole('button', { name: 'Zapisz' }).click();
|
||||
const req = await patchPromise;
|
||||
expect(req.postDataJSON()).toMatchObject({ status: 'BANNED', restrictionReason: 'Sprzedaż dostępu' });
|
||||
});
|
||||
|
||||
test('zawieszenie w kreatorze — zapis PATCHuje status z przyszłym terminem', async ({ page }) => {
|
||||
mockApi(page, { gyms: { gyms: [TEST_GYM] }, users: { users: [TEST_USER] } });
|
||||
await loginAsAdmin(page);
|
||||
await page.getByRole('link', { name: 'Użytkownicy' }).click();
|
||||
await page.getByRole('link', { name: 'Jan Kowalski' }).click();
|
||||
await page.getByRole('button', { name: 'Edytuj' }).click();
|
||||
|
||||
await page.getByRole('combobox', { name: 'Status konta' }).selectOption('SUSPENDED');
|
||||
await page.getByRole('textbox', { name: 'Zawieszone do' }).fill('2099-03-01T12:00');
|
||||
const patchPromise = page.waitForRequest(
|
||||
(req) => req.method() === 'PATCH' && req.url().endsWith('/admin/users/user-1'),
|
||||
);
|
||||
await page.getByRole('button', { name: 'Zapisz' }).click();
|
||||
const req = await patchPromise;
|
||||
const body = req.postDataJSON() as { status: string; suspensionUntil: string };
|
||||
expect(body.status).toBe('SUSPENDED');
|
||||
expect(new Date(body.suspensionUntil).getTime()).toBe(new Date('2099-03-01T12:00').getTime());
|
||||
});
|
||||
|
||||
test('konto zbanowane — badge widoczny na liście i w szczegółach', async ({ page }) => {
|
||||
mockApi(page, {
|
||||
gyms: { gyms: [TEST_GYM] },
|
||||
users: {
|
||||
users: [{ ...TEST_USER, status: 'BANNED', restrictionReason: 'Udostępnianie konta' }],
|
||||
},
|
||||
});
|
||||
await loginAsAdmin(page);
|
||||
await page.getByRole('link', { name: 'Użytkownicy' }).click();
|
||||
await expect(page.getByText('Zablokowane')).toBeVisible();
|
||||
|
||||
await page.getByRole('link', { name: 'Jan Kowalski' }).click();
|
||||
await expect(page.getByText('Zablokowane')).toBeVisible();
|
||||
});
|
||||
|
||||
test('zakładka danych osobowych jest tylko do odczytu (brak kontrolek edycji)', async ({
|
||||
page,
|
||||
}) => {
|
||||
|
||||
@@ -165,6 +165,9 @@ export const TEST_USER: User = {
|
||||
canDoMuscles: [],
|
||||
difficultMuscles: [],
|
||||
avoidMuscles: ['legs'],
|
||||
status: 'ACTIVE',
|
||||
suspensionUntil: null,
|
||||
restrictionReason: null,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
memberships: [{ gymId: 'gym-1', roles: ['MEMBER'], gym: TEST_GYM }],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user