220 lines
5.8 KiB
TypeScript
220 lines
5.8 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 {
|
|
abandonPlan,
|
|
activatePlan,
|
|
completePlan,
|
|
createPlan,
|
|
deletePlan,
|
|
getPlan,
|
|
listPlans,
|
|
updatePlan,
|
|
} from '../services/plans.js';
|
|
import { logActivity } from '../services/audit.js';
|
|
import { activateSchema, createPlanSchema, updatePlanSchema } from './plans-schemas.js';
|
|
|
|
export function createPlanRoutes({ prisma }: AppDeps) {
|
|
const router = Router();
|
|
router.use(authenticate(prisma));
|
|
|
|
/**
|
|
* @openapi
|
|
* /plans:
|
|
* get:
|
|
* summary: User's plans
|
|
* tags: [Plans]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
*/
|
|
router.get('/', async (req, res, next) => {
|
|
try {
|
|
const result = await listPlans(prisma, req.user!.id);
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /plans:
|
|
* post:
|
|
* summary: Create plan. Structure: weeks -> days (dayOfWeek) -> exercises -> sets.
|
|
* Optional startDate (a Monday) creates and activates the plan in one step.
|
|
* tags: [Plans]
|
|
* responses:
|
|
* 201: { description: Created }
|
|
* 400: { description: Validation error / start date not a Monday / start week in the past }
|
|
* 409: { description: User already has an active plan }
|
|
*/
|
|
router.post('/', validate(createPlanSchema), async (req, res, next) => {
|
|
try {
|
|
const data = req.body as z.infer<typeof createPlanSchema>;
|
|
const result = await createPlan(prisma, req.user!.id, data);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'plan.created',
|
|
entityType: 'plan',
|
|
entityId: result.plan.id,
|
|
details: { title: result.plan.title },
|
|
});
|
|
res.status(201).json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /plans/{id}:
|
|
* get:
|
|
* summary: Plan details (weeks -> days -> exercises -> sets)
|
|
* tags: [Plans]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
* 404: { description: Not found }
|
|
*/
|
|
router.get('/:id', async (req, res, next) => {
|
|
try {
|
|
const result = await getPlan(prisma, req.params.id, req.user!.id);
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /plans/{id}:
|
|
* patch:
|
|
* summary: Replace plan structure (draft only)
|
|
* tags: [Plans]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
*/
|
|
router.patch('/:id', validate(updatePlanSchema), async (req, res, next) => {
|
|
try {
|
|
const data = req.body as z.infer<typeof updatePlanSchema>;
|
|
const result = await updatePlan(prisma, req.params.id, req.user!.id, data);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'plan.updated',
|
|
entityType: 'plan',
|
|
entityId: req.params.id,
|
|
details: { title: data.title },
|
|
});
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /plans/{id}:
|
|
* delete:
|
|
* summary: Delete plan (draft only)
|
|
* tags: [Plans]
|
|
* responses:
|
|
* 204: { description: Deleted }
|
|
*/
|
|
router.delete('/:id', async (req, res, next) => {
|
|
try {
|
|
const result = await deletePlan(prisma, req.params.id, req.user!.id);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'plan.deleted',
|
|
entityType: 'plan',
|
|
entityId: req.params.id,
|
|
details: { title: result.title },
|
|
});
|
|
res.status(204).end();
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /plans/{id}/activate:
|
|
* post:
|
|
* summary: Activate plan. startDate must be a Monday (first day of week)
|
|
* of the current or a future week. Training day dates are computed automatically.
|
|
* tags: [Plans]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
* 409: { description: User already has an active plan }
|
|
*/
|
|
router.post('/:id/activate', validate(activateSchema), async (req, res, next) => {
|
|
try {
|
|
const { startDate } = req.body as z.infer<typeof activateSchema>;
|
|
const result = await activatePlan(prisma, req.params.id, req.user!.id, startDate);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'plan.activated',
|
|
entityType: 'plan',
|
|
entityId: req.params.id,
|
|
details: { title: result.title, startDate },
|
|
});
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /plans/{id}/abandon:
|
|
* post:
|
|
* summary: Abandon plan (draft/active)
|
|
* tags: [Plans]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
*/
|
|
router.post('/:id/abandon', async (req, res, next) => {
|
|
try {
|
|
const result = await abandonPlan(prisma, req.params.id, req.user!.id);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'plan.abandoned',
|
|
entityType: 'plan',
|
|
entityId: req.params.id,
|
|
details: { title: result.title },
|
|
});
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /plans/{id}/complete:
|
|
* post:
|
|
* summary: Manually archive plan (mark as completed)
|
|
* tags: [Plans]
|
|
* responses:
|
|
* 200: { description: OK }
|
|
*/
|
|
router.post('/:id/complete', async (req, res, next) => {
|
|
try {
|
|
const result = await completePlan(prisma, req.params.id, req.user!.id);
|
|
await logActivity(prisma, {
|
|
actorId: req.user!.id,
|
|
action: 'plan.completed',
|
|
entityType: 'plan',
|
|
entityId: req.params.id,
|
|
details: { title: result.title },
|
|
});
|
|
res.json(result);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
return router;
|
|
}
|