263 lines
7.2 KiB
TypeScript
263 lines
7.2 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,
|
|
deleteSet,
|
|
getDay,
|
|
getSession,
|
|
startSession,
|
|
updateExercise,
|
|
updateSet,
|
|
} from '../services/sessions.js';
|
|
import { logActivity } from '../services/audit.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', async (req, res, next) => {
|
|
try {
|
|
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);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @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', async (req, res, next) => {
|
|
try {
|
|
const result = await getDay(prisma, req.params.dayId, req.user!.id);
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/{id}:
|
|
* get:
|
|
* summary: Session details (exercises -> sets, planned parameters as parent)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
*/
|
|
router.get('/sessions/:id', async (req, res, next) => {
|
|
try {
|
|
const result = await getSession(prisma, req.params.id, req.user!.id);
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @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),
|
|
async (req, res, next) => {
|
|
try {
|
|
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);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @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),
|
|
async (req, res, next) => {
|
|
try {
|
|
const data = req.body as z.infer<typeof updateExerciseSchema>;
|
|
const result = await updateExercise(prisma, req.params.id, req.user!.id, data);
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/exercises/{id}/sets:
|
|
* post:
|
|
* summary: Add an extra (bonus) set. Only when all planned sets of the session are
|
|
* recorded and no extra set is still pending (at most one pending extra per session).
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 201: { description: Created }
|
|
* 400: { description: Norm not done or an extra set is already in progress }
|
|
*/
|
|
router.post('/sessions/exercises/:id/sets', async (req, res, next) => {
|
|
try {
|
|
const result = await addSet(prisma, req.params.id, req.user!.id);
|
|
res.status(201).json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @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),
|
|
async (req, res, next) => {
|
|
try {
|
|
const data = req.body as z.infer<typeof updateSetSchema>;
|
|
const result = await updateSet(prisma, req.params.id, req.user!.id, data);
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @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', async (req, res, next) => {
|
|
try {
|
|
const result = await deleteSet(prisma, req.params.id, req.user!.id);
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /sessions/{id}/complete:
|
|
* post:
|
|
* summary: Complete session (requires all planned/norm sets recorded; extra sets are
|
|
* a bonus and don't block; day -> completed)
|
|
* tags: [Sessions]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
* 400: { description: Session not in progress or planned sets still pending }
|
|
*/
|
|
router.post('/sessions/:id/complete', async (req, res, next) => {
|
|
try {
|
|
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);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @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', async (req, res, next) => {
|
|
try {
|
|
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);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
return router;
|
|
}
|