diff --git a/admin/src/api/types.ts b/admin/src/api/types.ts index 8318541..57568c2 100644 --- a/admin/src/api/types.ts +++ b/admin/src/api/types.ts @@ -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 }[]; } diff --git a/admin/src/pages/users/UserDetail.tsx b/admin/src/pages/users/UserDetail.tsx index 080b961..d4c3995 100644 --- a/admin/src/pages/users/UserDetail.tsx +++ b/admin/src/pages/users/UserDetail.tsx @@ -216,6 +216,15 @@ export default function UserDetail() { + {user.status === 'SUSPENDED' && ( + + Zawieszone + {user.suspensionUntil + ? ` do ${new Intl.DateTimeFormat('pl-PL', { dateStyle: 'medium', timeStyle: 'short' }).format(new Date(user.suspensionUntil))}` + : ''} + + )} + {user.status === 'BANNED' && Zablokowane} {user.nickname && @{user.nickname}} {user.isAdmin && Admin} {memberships.length > 0 && {memberships.length} sił.} diff --git a/admin/src/pages/users/UserForm.tsx b/admin/src/pages/users/UserForm.tsx index 7516d0f..8959bd5 100644 --- a/admin/src/pages/users/UserForm.tsx +++ b/admin/src/pages/users/UserForm.tsx @@ -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() { + {/* Krok Dostęp — kara na koncie (tylko edycja: konto musi najpierw istnieć) */} + {isEdit && ( + +
+ + 2 + +

Dostęp

+
+ + + + {form.status === 'SUSPENDED' && ( + + setForm({ ...form, suspensionUntil: e.target.value })} + /> + + )} + {form.status !== 'ACTIVE' && ( + + setForm({ ...form, restrictionReason: e.target.value })} + placeholder={form.status === 'BANNED' ? 'np. udostępnianie konta' : 'opcjonalnie'} + /> + + )} +
+ )} + {/* Krok 2 — role w panelu */}
- 2 + {isEdit ? 3 : 2}

Role

@@ -375,7 +449,7 @@ export default function UserForm() {
- 3 + {isEdit ? 4 : 3}

Członkostwa w siłowniach

diff --git a/admin/src/pages/users/UsersList.tsx b/admin/src/pages/users/UsersList.tsx index f2f4917..0343ce4 100644 --- a/admin/src/pages/users/UsersList.tsx +++ b/admin/src/pages/users/UsersList.tsx @@ -63,6 +63,8 @@ export default function UsersList() { } actions={ <> + {u.status === 'SUSPENDED' && Zawieszone} + {u.status === 'BANNED' && Zablokowane} {u.isAdmin && Admin} {u.memberships?.length ?? 0} sił. diff --git a/admin/test/Login.test.tsx b/admin/test/Login.test.tsx index 3df87ce..d246487 100644 --- a/admin/test/Login.test.tsx +++ b/admin/test/Login.test.tsx @@ -23,6 +23,9 @@ const ADMIN: User = { canDoMuscles: [], difficultMuscles: [], avoidMuscles: [], + status: 'ACTIVE', + suspensionUntil: null, + restrictionReason: null, createdAt: '2026-08-22T10:00:00.000Z', }; diff --git a/admin/test/UserDetail.test.tsx b/admin/test/UserDetail.test.tsx index 54b98b0..a72d3d4 100644 --- a/admin/test/UserDetail.test.tsx +++ b/admin/test/UserDetail.test.tsx @@ -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] })], diff --git a/admin/test/UserForm.test.tsx b/admin/test/UserForm.test.tsx index e7a41c7..d63d47b 100644 --- a/admin/test/UserForm.test.tsx +++ b/admin/test/UserForm.test.tsx @@ -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 { 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()); + }); }); diff --git a/admin/test/UserInfo.test.tsx b/admin/test/UserInfo.test.tsx index ff3b96b..5dfb832 100644 --- a/admin/test/UserInfo.test.tsx +++ b/admin/test/UserInfo.test.tsx @@ -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: [], }; diff --git a/admin/test/UserMemberships.test.tsx b/admin/test/UserMemberships.test.tsx index 5a50a74..94a722e 100644 --- a/admin/test/UserMemberships.test.tsx +++ b/admin/test/UserMemberships.test.tsx @@ -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 }], }; diff --git a/admin/test/UsersList.test.tsx b/admin/test/UsersList.test.tsx index a1e1565..ddec7c0 100644 --- a/admin/test/UsersList.test.tsx +++ b/admin/test/UsersList.test.tsx @@ -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: [], }; diff --git a/admin/test/e2e/admin.spec.ts b/admin/test/e2e/admin.spec.ts index 5617e70..145e38c 100644 --- a/admin/test/e2e/admin.spec.ts +++ b/admin/test/e2e/admin.spec.ts @@ -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, }) => { diff --git a/admin/test/e2e/mock-api.ts b/admin/test/e2e/mock-api.ts index 4c83d43..6776936 100644 --- a/admin/test/e2e/mock-api.ts +++ b/admin/test/e2e/mock-api.ts @@ -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 }], };