Baseline: stan przed refaktoryzacja struktury
This commit is contained in:
83
admin/src/api/client.ts
Normal file
83
admin/src/api/client.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { API_BASE } from '../config';
|
||||
import type { ApiErrorBody } from './types';
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
public status: number,
|
||||
public body: ApiErrorBody | null,
|
||||
) {
|
||||
super(body?.error?.message || `HTTP ${status}`);
|
||||
}
|
||||
}
|
||||
|
||||
let accessToken: string | null = localStorage.getItem('fitos_admin_access');
|
||||
let refreshing: Promise<boolean> | null = null;
|
||||
|
||||
export function setAccessToken(token: string | null) {
|
||||
accessToken = token;
|
||||
if (token) localStorage.setItem('fitos_admin_access', token);
|
||||
else localStorage.removeItem('fitos_admin_access');
|
||||
}
|
||||
|
||||
export function getAccessToken() {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
async function tryRefresh(): Promise<boolean> {
|
||||
if (!refreshing) {
|
||||
refreshing = fetch(`${API_BASE}/auth/refresh`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
})
|
||||
.then(async (res) => {
|
||||
if (!res.ok) return false;
|
||||
const data = await res.json();
|
||||
setAccessToken(data.accessToken);
|
||||
return true;
|
||||
})
|
||||
.catch(() => false)
|
||||
.finally(() => {
|
||||
setTimeout(() => (refreshing = null), 0);
|
||||
});
|
||||
}
|
||||
return refreshing;
|
||||
}
|
||||
|
||||
export async function api<T>(path: string, opts: { method?: string; body?: unknown } = {}): Promise<T> {
|
||||
const doFetch = () =>
|
||||
fetch(`${API_BASE}${path}`, {
|
||||
method: opts.method || 'GET',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
|
||||
},
|
||||
body: opts.body !== undefined ? JSON.stringify(opts.body) : undefined,
|
||||
});
|
||||
|
||||
let res = await doFetch();
|
||||
|
||||
if (res.status === 401 && path !== '/auth/refresh' && path !== '/auth/login' && path !== '/auth/register') {
|
||||
const ok = await tryRefresh();
|
||||
if (ok) res = await doFetch();
|
||||
else {
|
||||
setAccessToken(null);
|
||||
window.dispatchEvent(new Event('fitos:logout'));
|
||||
}
|
||||
}
|
||||
|
||||
const text = await res.text();
|
||||
let json: unknown = null;
|
||||
if (text) {
|
||||
try {
|
||||
json = JSON.parse(text);
|
||||
} catch {
|
||||
// ciało nie-JSON (np. rate limiter, proxy) — traktujemy jako nieprzezroczysty błąd
|
||||
}
|
||||
}
|
||||
if (!res.ok) {
|
||||
const body = (json ?? (text ? { error: { code: `HTTP_${res.status}`, message: text } } : null)) as ApiErrorBody | null;
|
||||
throw new ApiError(res.status, body);
|
||||
}
|
||||
return json as T;
|
||||
}
|
||||
216
admin/src/api/types.ts
Normal file
216
admin/src/api/types.ts
Normal file
@@ -0,0 +1,216 @@
|
||||
export type GymRole = 'OWNER' | 'TRAINER' | 'MEMBER';
|
||||
|
||||
export interface DayAvailability {
|
||||
start: string;
|
||||
end: string;
|
||||
}
|
||||
|
||||
export type Availability = Array<DayAvailability | null>;
|
||||
|
||||
export type SocialMediaPlatform = 'INSTAGRAM' | 'TIKTOK' | 'YOUTUBE' | 'FACEBOOK' | 'X' | 'OTHER';
|
||||
|
||||
export interface SocialMediaEntry {
|
||||
platform: SocialMediaPlatform;
|
||||
url: string;
|
||||
label?: string;
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
firstName: string | null;
|
||||
lastName: string | null;
|
||||
nickname: string | null;
|
||||
socialMedia: SocialMediaEntry[] | null;
|
||||
age: number | null;
|
||||
heightCm: number | null;
|
||||
phone: string | null;
|
||||
isAdmin: boolean;
|
||||
availability: Availability | null;
|
||||
priorities: string | null;
|
||||
exclusions: string | null;
|
||||
developMuscles: string[];
|
||||
canDoMuscles: string[];
|
||||
difficultMuscles: string[];
|
||||
avoidMuscles: string[];
|
||||
createdAt: string;
|
||||
memberships?: { gymId: string; roles: GymRole[]; gym: Gym }[];
|
||||
}
|
||||
|
||||
export interface FocusArea {
|
||||
id: string;
|
||||
name: string;
|
||||
muscle: string;
|
||||
description: string | null;
|
||||
setsMin: number | null;
|
||||
setsMax: number | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface FocusChainEntry {
|
||||
muscle: string;
|
||||
name: string;
|
||||
exercises: { id: string; name: string }[];
|
||||
}
|
||||
|
||||
export interface FocusChain {
|
||||
develop: FocusChainEntry[];
|
||||
canDo: FocusChainEntry[];
|
||||
difficult: FocusChainEntry[];
|
||||
avoid: FocusChainEntry[];
|
||||
}
|
||||
|
||||
export type EquipmentKind =
|
||||
| 'DUMBBELL'
|
||||
| 'KETTLEBELL'
|
||||
| 'BARBELL'
|
||||
| 'CABLE'
|
||||
| 'MACHINE'
|
||||
| 'BENCH'
|
||||
| 'CARDIO'
|
||||
| 'BODYWEIGHT'
|
||||
| 'OTHER';
|
||||
|
||||
/** Type-specific spec payload: DUMBBELL/KETTLEBELL/CABLE/MACHINE → { minKg, maxKg }, BARBELL/BENCH → { maxLoadKg }, CARDIO → { maxSpeedKmh?, maxInclinePct? }. */
|
||||
export interface EquipmentSpec {
|
||||
minKg?: number;
|
||||
maxKg?: number;
|
||||
maxLoadKg?: number;
|
||||
maxSpeedKmh?: number;
|
||||
maxInclinePct?: number;
|
||||
}
|
||||
|
||||
export interface Equipment {
|
||||
id: string;
|
||||
name: string;
|
||||
kind: EquipmentKind;
|
||||
spec: EquipmentSpec | null;
|
||||
description: string | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
/** A gym's equipment instance: how many units of a catalog item the gym has. */
|
||||
export interface GymEquipment {
|
||||
id: string;
|
||||
gymId: string;
|
||||
equipmentId: string;
|
||||
quantity: number;
|
||||
equipment: Equipment;
|
||||
}
|
||||
|
||||
export interface Exercise {
|
||||
id: string;
|
||||
slug: string;
|
||||
name: string;
|
||||
primaryMuscle: string;
|
||||
secondaryMuscles: string[];
|
||||
equipment: Equipment[];
|
||||
hasReps: boolean;
|
||||
hasLoad: boolean;
|
||||
hasRest: boolean;
|
||||
hasSets: boolean;
|
||||
hasRir: boolean;
|
||||
hasDifficulty: boolean;
|
||||
hasCardio: boolean;
|
||||
hasTime: boolean;
|
||||
hasSpeed: boolean;
|
||||
description: string | null;
|
||||
howTo: string | null;
|
||||
commonMistakes: string | null;
|
||||
difficulty: number;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export type GymKind = 'VIRTUAL' | 'REAL';
|
||||
|
||||
export interface GymPhoto {
|
||||
id: string;
|
||||
url: string;
|
||||
order: number;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface Gym {
|
||||
id: string;
|
||||
name: string;
|
||||
location: string | null;
|
||||
availability: Availability | null;
|
||||
capacity: number | null;
|
||||
kind: GymKind;
|
||||
isPrivate: boolean;
|
||||
equipment: GymEquipment[];
|
||||
coverPhoto: string | null;
|
||||
photos?: GymPhoto[];
|
||||
_count?: { memberships: number };
|
||||
}
|
||||
|
||||
export type ClaimStatus = 'PENDING' | 'APPROVED' | 'REJECTED';
|
||||
|
||||
export interface GymClaimDocument {
|
||||
id: string;
|
||||
originalName: string;
|
||||
mimeType: string;
|
||||
sizeBytes: number;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
/** Wniosek o siłownię (admin): `gymId` null = wniosek o NOWĄ realną (dane w `gymName`/`gymLocation`). */
|
||||
export interface GymClaim {
|
||||
id: string;
|
||||
gymId: string | null;
|
||||
type: 'verification' | 'new';
|
||||
gym: { id: string; name: string; kind: GymKind; isPrivate: boolean } | null;
|
||||
gymName: string | null;
|
||||
gymLocation: string | null;
|
||||
gymAvailability: Availability | null;
|
||||
gymCapacity: number | null;
|
||||
description: string;
|
||||
companyName: string | null;
|
||||
nip: string | null;
|
||||
phone: string | null;
|
||||
email: string | null;
|
||||
address: string | null;
|
||||
status: ClaimStatus;
|
||||
rejectReason: string | null;
|
||||
decidedAt: string | null;
|
||||
createdAt: string;
|
||||
user: { id: string; firstName: string | null; lastName: string | null; email: string; phone?: string | null };
|
||||
documentCount?: number;
|
||||
documents: GymClaimDocument[];
|
||||
}
|
||||
|
||||
export interface GymException {
|
||||
id: string;
|
||||
gymId: string;
|
||||
date: string;
|
||||
availability: DayAvailability | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface ActivityLogActor {
|
||||
id: string;
|
||||
email: string;
|
||||
firstName: string | null;
|
||||
lastName: string | null;
|
||||
}
|
||||
|
||||
export interface ActivityLog {
|
||||
id: string;
|
||||
action: string;
|
||||
entityType: string;
|
||||
entityId: string | null;
|
||||
details: Record<string, unknown> | null;
|
||||
createdAt: string;
|
||||
actor: ActivityLogActor;
|
||||
}
|
||||
|
||||
export interface ActivityLogPage {
|
||||
logs: ActivityLog[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
export interface ApiErrorBody {
|
||||
error: { code: string; message: string; details?: unknown };
|
||||
}
|
||||
Reference in New Issue
Block a user