273 lines
7.9 KiB
TypeScript
273 lines
7.9 KiB
TypeScript
import { Router } from 'express';
|
|
import { z } from 'zod';
|
|
import type { AppDeps } from '../deps.js';
|
|
import { authenticate } from '../middleware/auth.js';
|
|
import { validate } from '../middleware/validate.js';
|
|
import {
|
|
abandonSession,
|
|
addExercise,
|
|
addSet,
|
|
completeSession,
|
|
reopenSession,
|
|
deleteSet,
|
|
getDay,
|
|
getSession,
|
|
startSession,
|
|
updateExercise,
|
|
updateSet,
|
|
} from '../services/sessions.js';
|
|
import { logActivity } from '../services/audit.js';
|
|
import { asyncHandler } from '../lib/asyncHandler.js';
|
|
|
|
const addExerciseSchema = z.object({ exerciseId: z.string().min(1) });
|
|
|
|
const updateExerciseSchema = z.object({
|
|
hidden: z.boolean().optional(),
|
|
removed: z.boolean().optional(),
|
|
});
|
|
|
|
const updateSetSchema = z.object({
|
|
reps: z.number().int().positive().max(1000).nullable().optional(),
|
|
rir: z.number().int().min(0).max(10).nullable().optional(),
|
|
difficulty: z.number().int().min(1).max(5).nullable().optional(),
|
|
note: z.string().max(2000).nullable().optional(),
|
|
});
|
|
|
|
export function createSessionRoutes({ prisma }: AppDeps) {
|
|
const router = Router();
|
|
router.use(authenticate(prisma));
|
|
|
|
/**
|
|
* @openapi
|
|
* /days/{dayId}/sessions:
|
|
* post:
|
|
* summary: Start session for a planned day (initializes session exercises + sets from plan).
|
|
* Only one active session per user (across all days).
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 201: { description: Created }
|
|
* 409: { description: User already has an active session }
|
|
*/
|
|
router.post(
|
|
'/days/:dayId/sessions',
|
|
asyncHandler(async (req, res) => {
|
|
const result = await startSession(prisma, req.params.dayId, req.user!.id);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'session.started',
|
|
entityType: 'session',
|
|
entityId: result.sessionId,
|
|
});
|
|
res.status(201).json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /days/{dayId}:
|
|
* get:
|
|
* summary: Training day details (status, date, sessions)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
* 404: { description: Not found }
|
|
*/
|
|
router.get(
|
|
'/days/:dayId',
|
|
asyncHandler(async (req, res) => {
|
|
const result = await getDay(prisma, req.params.dayId, req.user!.id);
|
|
res.json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/{id}:
|
|
* get:
|
|
* summary: Session details (exercises -> sets, planned parameters as parent)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
*/
|
|
router.get(
|
|
'/sessions/:id',
|
|
asyncHandler(async (req, res) => {
|
|
const result = await getSession(prisma, req.params.id, req.user!.id);
|
|
res.json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/{id}/exercises:
|
|
* post:
|
|
* summary: Add exercise to session (SessionSet without PlanSet — data kept for restore)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 201: { description: Created }
|
|
*/
|
|
router.post(
|
|
'/sessions/:id/exercises',
|
|
validate(addExerciseSchema),
|
|
asyncHandler(async (req, res) => {
|
|
const { exerciseId } = req.body as z.infer<typeof addExerciseSchema>;
|
|
const result = await addExercise(prisma, req.params.id, req.user!.id, exerciseId);
|
|
res.status(201).json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/exercises/{id}:
|
|
* patch:
|
|
* summary: Hide or remove exercise from session (data kept for restore)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
*/
|
|
router.patch(
|
|
'/sessions/exercises/:id',
|
|
validate(updateExerciseSchema),
|
|
asyncHandler(async (req, res) => {
|
|
const data = req.body as z.infer<typeof updateExerciseSchema>;
|
|
const result = await updateExercise(prisma, req.params.id, req.user!.id, data);
|
|
res.json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/exercises/{id}/sets:
|
|
* post:
|
|
* summary: Add an extra (bonus) set. Only when all planned sets of this exercise are
|
|
* recorded (added exercises have no norm) and this exercise has no extra set
|
|
* still pending (at most one pending extra per exercise).
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 201: { description: Created }
|
|
* 400: { description: Norm of this exercise not done or an extra set is already in progress }
|
|
*/
|
|
router.post(
|
|
'/sessions/exercises/:id/sets',
|
|
asyncHandler(async (req, res) => {
|
|
const result = await addSet(prisma, req.params.id, req.user!.id);
|
|
res.status(201).json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/sets/{id}:
|
|
* patch:
|
|
* summary: Record actual set results (reps, RIR, difficulty, note)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
*/
|
|
router.patch(
|
|
'/sessions/sets/:id',
|
|
validate(updateSetSchema),
|
|
asyncHandler(async (req, res) => {
|
|
const data = req.body as z.infer<typeof updateSetSchema>;
|
|
const result = await updateSet(prisma, req.params.id, req.user!.id, data);
|
|
res.json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/sets/{id}:
|
|
* delete:
|
|
* summary: Remove an extra (bonus) set (only sets without a planned counterpart)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
* 400: { description: Planned set or session not in progress }
|
|
* 404: { description: Not found }
|
|
*/
|
|
router.delete(
|
|
'/sessions/sets/:id',
|
|
asyncHandler(async (req, res) => {
|
|
const result = await deleteSet(prisma, req.params.id, req.user!.id);
|
|
res.json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/{id}/complete:
|
|
* post:
|
|
* summary: Complete session (requires all planned/norm sets recorded and no extra set
|
|
* still in progress — recorded bonus sets don't block; day -> completed)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
* 400: { description: Session not in progress, planned sets still pending, or an extra set in progress }
|
|
*/
|
|
router.post(
|
|
'/sessions/:id/complete',
|
|
asyncHandler(async (req, res) => {
|
|
const result = await completeSession(prisma, req.params.id, req.user!.id);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'session.completed',
|
|
entityType: 'session',
|
|
entityId: req.params.id,
|
|
});
|
|
res.json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/{id}/abandon:
|
|
* post:
|
|
* summary: Abandon session (data kept, day -> skipped, the training cannot be executed again)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
* 400: { description: Session not in progress }
|
|
*/
|
|
router.post(
|
|
'/sessions/:id/abandon',
|
|
asyncHandler(async (req, res) => {
|
|
const result = await abandonSession(prisma, req.params.id, req.user!.id);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'session.abandoned',
|
|
entityType: 'session',
|
|
entityId: req.params.id,
|
|
});
|
|
res.json(result);
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/{id}/reopen:
|
|
* post:
|
|
* summary: Reopen completed session (naprawa pomyłki; session -> in progress,
|
|
* day -> planned; requires no other active session)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK — session is in progress again }
|
|
* 400: { description: Session is not completed }
|
|
* 409: { description: Another session is active or the day is no longer completed }
|
|
*/
|
|
router.post(
|
|
'/sessions/:id/reopen',
|
|
asyncHandler(async (req, res) => {
|
|
const result = await reopenSession(prisma, req.params.id, req.user!.id);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'session.reopened',
|
|
entityType: 'session',
|
|
entityId: req.params.id,
|
|
});
|
|
res.json(result);
|
|
}),
|
|
);
|
|
|
|
return router;
|
|
}
|