API+App+Admin: telemetria bytnosci w ogolnym widoku sirowni - GET /gyms/:id/presence-stats (heatmapa dzien×godzina, sredni pobyt, wizyty/dzien, szczyt, teraz w srodku, top urzadzenia; stale 30 dni), wspoldzielony komponent PresenceHeatmap, telemetria wyciagnieta z podstrony statystyk; pasek godzin w forma zegarowa z minutami, usuniete karta Dane i filtr okresu; swagger + testy integracyjne, unit i e2e

This commit is contained in:
tisachi
2026-10-27 05:32:55 +01:00
parent 104748b3e8
commit 1629068bf0
30 changed files with 965 additions and 507 deletions

View File

@@ -16,7 +16,12 @@ import {
} from '../services/gyms.js';
import { logActivity } from '../services/audit.js';
import { asyncHandler } from '../lib/asyncHandler.js';
import { checkIn, getEquipmentUsage, getGymStatsOverview } from '../services/presence.js';
import {
checkIn,
getEquipmentUsage,
getGymPresenceTelemetry,
getGymStatsOverview,
} from '../services/presence.js';
import { rangeQuerySchema, resolveRange } from './presence.js';
import { checkInSchema, createGymSchema, rateGymSchema, updateGymSchema } from './gyms/schemas.js';
import { createGymClaimRoutes } from './gyms/claims.js';
@@ -170,6 +175,33 @@ export function createGymRoutes(deps: AppDeps) {
}),
);
/**
* @openapi
* /gyms/{gymId}/presence-stats:
* get:
* summary: Presence telemetry of a gym (general view) — day×hour heatmap, avg stay, visits/day, peak, live, top equipment (default range: last 30 days). REAL visible to all logged-in users, VIRTUAL to managers (empty — presence is REAL only)
* tags: [Gyms]
* parameters:
* - { in: query, name: from, schema: { type: string, format: date-time } }
* - { in: query, name: to, schema: { type: string, format: date-time } }
* responses:
* 200: { description: "OK, returns { gym, stats: { visits, avgStayMinutes, heatmap, topGyms, peak, topEquipment, live, visitsPerDay } }" }
* 403: { description: VIRTUAL gym and the caller is not a manager }
* 404: { description: Gym not found }
*/
router.get(
'/:gymId/presence-stats',
validate(rangeQuerySchema, 'query'),
asyncHandler(async (req, res) => {
const query = req.query as z.infer<typeof rangeQuerySchema>;
const result = await getGymPresenceTelemetry(prisma, req.params.gymId, resolveRange(query), {
userId: req.user!.id,
isAdmin: req.user!.isAdmin,
});
res.json(result);
}),
);
/**
* @openapi
* /gyms/{gymId}/stats/overview:

View File

@@ -439,6 +439,30 @@ export async function getGymStats(
};
}
/**
* Telemetria bytności dla widoku ogólnego siłowni (aplikacja): dostęp jak
* mapa urządzeń — REAL: każdy zalogowany; VIRTUAL: tylko menedżer (OWNER) lub
* admin (puste statystyki — bytność istnieje tylko dla REAL). 404 gdy brak siłowni.
*/
export async function getGymPresenceTelemetry(
prisma: PrismaClient,
gymId: string,
range: Required<RangeQuery>,
caller: { userId: string; isAdmin: boolean },
) {
const gym = await prisma.gym.findUnique({ where: { id: gymId }, select: { kind: true } });
if (!gym) throw notFound('Gym not found');
if (gym.kind !== 'REAL' && !caller.isAdmin) {
const membership = await prisma.gymMembership.findUnique({
where: { userId_gymId: { userId: caller.userId, gymId } },
select: { roles: true },
});
if (!membership?.roles.includes('OWNER'))
throw forbidden('You do not have access to this gym');
}
return getGymStats(prisma, gymId, range);
}
/**
* Przegląd statystyk właściciela siłowni (tylko REAL): telemetria bytności
* (bez top urządzeń — jest na podstronie urządzeń) + członkowie i napływ +

View File

@@ -588,6 +588,27 @@ export const openapi: Record<string, unknown> = {
},
},
},
'/gyms/{gymId}/presence-stats': {
get: {
tags: ['Gyms'],
summary:
'Presence telemetry of a gym (general view): day×hour heatmap, avg stay, visits/day, peak, live, top equipment. REAL visible to all logged-in users; VIRTUAL to managers only (empty — presence is REAL only). Default range: last 30 days',
security: [{ bearerAuth: [] }],
parameters: [
{ name: 'gymId', in: 'path', required: true, schema: { type: 'string' } },
{ name: 'from', in: 'query', schema: { type: 'string', format: 'date-time' } },
{ name: 'to', in: 'query', schema: { type: 'string', format: 'date-time' } },
],
responses: {
200: {
description:
'OK, returns { gym, stats: { visits, avgStayMinutes, heatmap: [{ day, hour, count }], topGyms, peak, topEquipment, live, visitsPerDay } }',
},
403: { description: 'VIRTUAL gym and the caller is not a manager' },
404: { description: 'Gym not found' },
},
},
},
'/gyms/{gymId}/stats/overview': {
get: {
tags: ['Gyms'],