From 0fe259a86615f2e32a8f45d00202129ab84820c0 Mon Sep 17 00:00:00 2001 From: Tilumca Date: Thu, 15 Oct 2026 02:12:22 +0200 Subject: [PATCH] =?UTF-8?q?Feed:=20aktualnosci=20=E2=80=94=20powiadomienia?= =?UTF-8?q?=20+=20ogloszenia=20silni=20i=20platformy=20(karta=20na=20dashb?= =?UTF-8?q?oardzie,=20strona=20/announcements,=20badge,=20CRUD=20ownera=20?= =?UTF-8?q?i=20admina)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/src/App.tsx | 2 + admin/src/api/types.ts | 14 + admin/src/components/Layout.tsx | 2 + .../src/pages/announcements/Announcements.tsx | 262 +++++++++++ admin/src/pages/gyms/GymDetail.tsx | 151 +++++- admin/test/GymDetail.test.tsx | 10 +- admin/test/e2e/announcements.spec.ts | 92 ++++ admin/test/e2e/mock-api.ts | 81 +++- .../migration.sql | 71 +++ api/prisma/schema.prisma | 109 ++++- api/src/app.ts | 3 + api/src/routes/admin.ts | 93 ++++ api/src/routes/feed.ts | 111 +++++ api/src/routes/gyms/management.ts | 124 ++++- api/src/routes/gyms/schemas.ts | 17 +- api/src/services/admin.ts | 35 +- api/src/services/announcements.ts | 198 ++++++++ api/src/services/feed.ts | 222 +++++++++ api/src/services/gyms/core.ts | 51 ++- api/src/services/gyms/exceptions.ts | 20 +- api/src/services/plans.ts | 16 + api/src/swagger.ts | 169 +++++++ api/test/integration/feed.test.ts | 432 ++++++++++++++++++ api/test/unit/availabilityChanged.test.ts | 38 ++ api/test/unit/services-plans.test.ts | 11 + app/src/App.tsx | 2 + app/src/api/queries.ts | 62 ++- app/src/api/types.ts | 40 ++ app/src/components/FeedItemRow.tsx | 103 +++++ app/src/components/Layout.tsx | 12 + app/src/pages/Dashboard.tsx | 39 +- app/src/pages/announcements/Announcements.tsx | 72 +++ app/src/pages/gyms/GymDetail.tsx | 138 ++++++ app/test/e2e/feed.spec.ts | 89 ++++ app/test/e2e/mock-api.ts | 36 ++ 35 files changed, 2830 insertions(+), 97 deletions(-) create mode 100644 admin/src/pages/announcements/Announcements.tsx create mode 100644 admin/test/e2e/announcements.spec.ts create mode 100644 api/prisma/migrations/20261014234912_feed_notifications_announcements/migration.sql create mode 100644 api/src/routes/feed.ts create mode 100644 api/src/services/announcements.ts create mode 100644 api/src/services/feed.ts create mode 100644 api/test/integration/feed.test.ts create mode 100644 api/test/unit/availabilityChanged.test.ts create mode 100644 app/src/components/FeedItemRow.tsx create mode 100644 app/src/pages/announcements/Announcements.tsx create mode 100644 app/test/e2e/feed.spec.ts diff --git a/admin/src/App.tsx b/admin/src/App.tsx index c4d5865..7b144c5 100644 --- a/admin/src/App.tsx +++ b/admin/src/App.tsx @@ -18,6 +18,7 @@ import UserClaims from './pages/users/UserClaims'; import FocusAreas from './pages/catalog/FocusAreas'; import Exercises from './pages/catalog/Exercises'; import EquipmentCatalog from './pages/catalog/Equipment'; +import Announcements from './pages/announcements/Announcements'; import Logs from './pages/Logs'; import VerificationsList, { VerificationDetail } from './pages/Verifications'; @@ -54,6 +55,7 @@ export default function App() { } /> } /> } /> + } /> } /> } /> diff --git a/admin/src/api/types.ts b/admin/src/api/types.ts index 87da3ff..8318541 100644 --- a/admin/src/api/types.ts +++ b/admin/src/api/types.ts @@ -316,3 +316,17 @@ export interface ExerciseProgress { muscle: string; points: ExerciseProgressPoint[]; } + +export type AnnouncementKind = 'MANUAL' | 'HOURS_CHANGE' | 'EXCEPTION'; + +/** Ogłoszenie w widoku panelu admina (globalne lub per-siłownia). */ +export interface AdminAnnouncement { + id: string; + gymId: string | null; + authorId: string | null; + kind: AnnouncementKind; + title: string; + body: string; + createdAt: string; + gymName: string | null; +} diff --git a/admin/src/components/Layout.tsx b/admin/src/components/Layout.tsx index 863a692..7ba1c8e 100644 --- a/admin/src/components/Layout.tsx +++ b/admin/src/components/Layout.tsx @@ -4,6 +4,7 @@ import { Building2, Dumbbell, LogOut, + Megaphone, Menu, ScrollText, ShieldCheck, @@ -39,6 +40,7 @@ const SECTIONS: NavSection[] = [ { to: '/gyms', label: 'Siłownie', icon: Building2, end: false }, { to: '/verifications', label: 'Weryfikacje', icon: ShieldCheck, end: false }, { to: '/users', label: 'Użytkownicy', icon: Users, end: false }, + { to: '/announcements', label: 'Ogłoszenia', icon: Megaphone, end: false }, ], }, { diff --git a/admin/src/pages/announcements/Announcements.tsx b/admin/src/pages/announcements/Announcements.tsx new file mode 100644 index 0000000..bd97c22 --- /dev/null +++ b/admin/src/pages/announcements/Announcements.tsx @@ -0,0 +1,262 @@ +import { useEffect, useState } from 'react'; +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; +import { Megaphone, Pencil, Plus, Trash2 } from 'lucide-react'; +import { api } from '../../api/client'; +import type { AdminAnnouncement, Gym } from '../../api/types'; +import { useTopbar } from '../../topbar'; +import { useListSearch } from '../../store/list-search'; +import { fmtDateTime } from '../../lib/format'; +import { + Badge, + Button, + Card, + ConfirmDialog, + EmptyState, + ErrorText, + Field, + Input, + Loading, + Modal, + Muted, + Select, + Textarea, +} from '../../components/ui'; + +const KIND_LABELS: Record< + AdminAnnouncement['kind'], + { label: string; color: 'blue' | 'yellow' | 'gray' } +> = { + MANUAL: { label: 'Ręczne', color: 'blue' }, + HOURS_CHANGE: { label: 'Zmiana godzin', color: 'yellow' }, + EXCEPTION: { label: 'Wyjątek', color: 'yellow' }, +}; + +interface FormState { + title: string; + body: string; + /** '' = globalne (wszyscy userzy); inaczej id siłowni. */ + gymId: string; +} + +const EMPTY: FormState = { title: '', body: '', gymId: '' }; + +export default function Announcements() { + const qc = useQueryClient(); + const { data, isLoading, error } = useQuery({ + queryKey: ['admin-announcements'], + queryFn: () => api<{ announcements: AdminAnnouncement[] }>('/admin/announcements'), + }); + const announcements = data?.announcements ?? []; + const { data: gymsData } = useQuery({ + queryKey: ['gyms'], + queryFn: () => api<{ gyms: Gym[] }>('/admin/gyms'), + }); + const gyms = gymsData?.gyms ?? []; + const query = useListSearch((s) => s.queries['announcements']); + const setQuery = useListSearch((s) => s.setQuery); + + const filtered = query + ? announcements.filter((a) => + [a.title, a.body, a.gymName ?? 'Platforma'].some((s) => + s.toLowerCase().includes(query.toLowerCase()), + ), + ) + : announcements; + + useEffect(() => () => setQuery('announcements', ''), [setQuery]); + + const [formOpen, setFormOpen] = useState(false); + const [editing, setEditing] = useState(null); + const [form, setForm] = useState(EMPTY); + const [formError, setFormError] = useState(null); + const [deleting, setDeleting] = useState(null); + + const invalidate = () => qc.invalidateQueries({ queryKey: ['admin-announcements'] }); + + const save = useMutation({ + mutationFn: async () => { + const body = { + title: form.title.trim(), + body: form.body.trim(), + ...(editing ? {} : { gymId: form.gymId || null }), + }; + if (editing) { + await api(`/admin/announcements/${editing.id}`, { method: 'PATCH', body }); + } else { + await api('/admin/announcements', { method: 'POST', body }); + } + }, + onSuccess: () => { + invalidate(); + setFormOpen(false); + setEditing(null); + setForm(EMPTY); + setFormError(null); + }, + onError: (e) => setFormError((e as Error).message), + }); + + const remove = useMutation({ + mutationFn: (id: string) => api(`/admin/announcements/${id}`, { method: 'DELETE' }), + onSuccess: () => { + invalidate(); + setDeleting(null); + }, + }); + + function openCreate() { + setEditing(null); + setForm(EMPTY); + setFormError(null); + setFormOpen(true); + } + + function openEdit(a: AdminAnnouncement) { + setEditing(a); + setForm({ title: a.title, body: a.body, gymId: a.gymId ?? '' }); + setFormError(null); + setFormOpen(true); + } + + useTopbar({ + title: 'Ogłoszenia', + search: 'announcements', + actions: [{ label: 'Nowe ogłoszenie', onClick: openCreate, icon: Plus }], + }); + + return ( + <> + + Ogłoszenia platformy (trafiają do wszystkich użytkowników) oraz awaryjne ogłoszenia + publikowane w imieniu siłowni — gdy właściciel nie może tego zrobić sam. + + + {isLoading && } + {error && Błąd: {(error as Error).message}} + +
+ {filtered.map((a) => ( + +
+
+
{a.title}
+
+ {a.gymName ?? 'Platforma'} + {KIND_LABELS[a.kind].label} + + {fmtDateTime(a.createdAt)} + +
+

{a.body}

+
+ {a.kind === 'MANUAL' && ( +
+ + +
+ )} +
+
+ ))} +
+ + {!isLoading && !error && filtered.length === 0 && ( + + + + )} + + setFormOpen(false)} + title={editing ? 'Edytuj ogłoszenie' : 'Nowe ogłoszenie'} + > +
+ {!editing && ( + + + + )} + {editing && ( + + Adresaci: {editing.gymName ?? 'wszyscy użytkownicy (platforma)'} + + )} + + setForm((f) => ({ ...f, title: e.target.value }))} + maxLength={120} + placeholder="np. Przerwa techniczna platformy" + /> + + +