Plany: PATCH z startDate (aktywacja przy zapisie) i udostepnianie planow trener-podopieczny (PlanShare, sekcja Udostepnione mi, widok read-only)
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
updateProfile,
|
||||
} from '../services/me.js';
|
||||
import { getFocusChain } from '../services/focusChain.js';
|
||||
import { getMyTrainers } from '../services/plans.js';
|
||||
import { getActiveSession } from '../services/sessions.js';
|
||||
import { addMyComment, deleteMyComment } from '../services/comments.js';
|
||||
import { getExerciseProgress } from '../services/exerciseProgress.js';
|
||||
@@ -107,6 +108,23 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /me/trainers:
|
||||
* get:
|
||||
* summary: My trainers (ACTIVE coaching relations where I am the trainee)
|
||||
* tags: [Profile]
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get(
|
||||
'/trainers',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getMyTrainers(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /me/focus-chain:
|
||||
|
||||
@@ -50,10 +50,23 @@ export const createPlanSchema = planBaseSchema
|
||||
message: 'weeks must have exactly weeksCount entries',
|
||||
});
|
||||
|
||||
export const updatePlanSchema = planBaseSchema.refine((d) => d.weeks.length === d.weeksCount, {
|
||||
message: 'weeks must have exactly weeksCount entries',
|
||||
});
|
||||
export const updatePlanSchema = planBaseSchema
|
||||
.extend({
|
||||
// Opcjonalna data startu (poniedziałek) — zapis = aktualizacja struktury + aktywacja
|
||||
// planu w jednym kroku (plan musi być DRAFT).
|
||||
startDate: z
|
||||
.string()
|
||||
.refine((v) => !Number.isNaN(new Date(v).getTime()), 'Invalid date')
|
||||
.optional(),
|
||||
})
|
||||
.refine((d) => d.weeks.length === d.weeksCount, {
|
||||
message: 'weeks must have exactly weeksCount entries',
|
||||
});
|
||||
|
||||
export const activateSchema = z.object({
|
||||
startDate: z.string().refine((v) => !Number.isNaN(new Date(v).getTime()), 'Invalid date'),
|
||||
});
|
||||
|
||||
export const sharePlanSchema = z.object({
|
||||
userId: z.string().min(1),
|
||||
});
|
||||
|
||||
@@ -6,16 +6,25 @@ import { validate } from '../middleware/validate.js';
|
||||
import {
|
||||
abandonPlan,
|
||||
activatePlan,
|
||||
assertPlanOwner,
|
||||
completePlan,
|
||||
createPlan,
|
||||
deletePlan,
|
||||
getPlan,
|
||||
listPlans,
|
||||
listSharedWithMe,
|
||||
sharePlan,
|
||||
unsharePlan,
|
||||
updatePlan,
|
||||
} from '../services/plans.js';
|
||||
import { logActivity } from '../services/audit.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
import { activateSchema, createPlanSchema, updatePlanSchema } from './plans-schemas.js';
|
||||
import {
|
||||
activateSchema,
|
||||
createPlanSchema,
|
||||
sharePlanSchema,
|
||||
updatePlanSchema,
|
||||
} from './plans-schemas.js';
|
||||
|
||||
export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
const router = Router();
|
||||
@@ -67,11 +76,29 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /plans/shared-with-me:
|
||||
* get:
|
||||
* summary: Plans explicitly shared with the user (read-only)
|
||||
* tags: [Plans]
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get(
|
||||
'/shared-with-me',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listSharedWithMe(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /plans/{id}:
|
||||
* get:
|
||||
* summary: Plan details (weeks -> days -> exercises -> sets)
|
||||
* summary: Plan details (weeks -> days -> exercises -> sets). Access: owner,
|
||||
* trainer with an ACTIVE coaching relation, or a share recipient (read-only).
|
||||
* tags: [Plans]
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
@@ -89,15 +116,21 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* @openapi
|
||||
* /plans/{id}:
|
||||
* patch:
|
||||
* summary: Replace plan structure (draft only)
|
||||
* summary: Replace plan structure (draft only). Optional startDate (a Monday,
|
||||
* current or future week) updates and activates the plan in one step.
|
||||
* tags: [Plans]
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
* 400: { description: Not a draft / start date not a Monday / start week in the past }
|
||||
* 409: { description: User already has an active plan }
|
||||
*/
|
||||
router.patch(
|
||||
'/:id',
|
||||
validate(updatePlanSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
// mutacje trasy właściciela = tylko właściciel (trener ma trasy /trainer/...,
|
||||
// odbiorca share'a jest read-only)
|
||||
await assertPlanOwner(prisma, req.params.id, req.user!.id);
|
||||
const data = req.body as z.infer<typeof updatePlanSchema>;
|
||||
const result = await updatePlan(prisma, req.params.id, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
@@ -123,6 +156,7 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
router.delete(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertPlanOwner(prisma, req.params.id, req.user!.id);
|
||||
const result = await deletePlan(prisma, req.params.id, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -150,6 +184,7 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
'/:id/activate',
|
||||
validate(activateSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertPlanOwner(prisma, req.params.id, req.user!.id);
|
||||
const { startDate } = req.body as z.infer<typeof activateSchema>;
|
||||
const result = await activatePlan(prisma, req.params.id, req.user!.id, startDate);
|
||||
await logActivity(prisma, {
|
||||
@@ -175,6 +210,7 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/:id/abandon',
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertPlanOwner(prisma, req.params.id, req.user!.id);
|
||||
const result = await abandonPlan(prisma, req.params.id, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -199,6 +235,7 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/:id/complete',
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertPlanOwner(prisma, req.params.id, req.user!.id);
|
||||
const result = await completePlan(prisma, req.params.id, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -211,5 +248,62 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /plans/{id}/share:
|
||||
* post:
|
||||
* summary: Share the plan with a user in an ACTIVE coaching relation
|
||||
* (trainer <-> trainee). Owner only; recipients get read-only access.
|
||||
* tags: [Plans]
|
||||
* responses:
|
||||
* 201: { description: Shared }
|
||||
* 400: { description: Self-share / no active coaching relation }
|
||||
* 403: { description: Not the plan owner }
|
||||
* 404: { description: Plan not found }
|
||||
* 409: { description: Already shared with this user }
|
||||
*/
|
||||
router.post(
|
||||
'/:id/share',
|
||||
validate(sharePlanSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const { userId } = req.body as z.infer<typeof sharePlanSchema>;
|
||||
const result = await sharePlan(prisma, req.params.id, req.user!.id, userId);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'plan.shared',
|
||||
entityType: 'plan',
|
||||
entityId: req.params.id,
|
||||
details: { userId },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /plans/{id}/share/{userId}:
|
||||
* delete:
|
||||
* summary: Revoke a plan share. Owner only.
|
||||
* tags: [Plans]
|
||||
* responses:
|
||||
* 204: { description: Unshared }
|
||||
* 403: { description: Not the plan owner }
|
||||
* 404: { description: Plan not found / not shared with this user }
|
||||
*/
|
||||
router.delete(
|
||||
'/:id/share/:userId',
|
||||
asyncHandler(async (req, res) => {
|
||||
await unsharePlan(prisma, req.params.id, req.user!.id, req.params.userId);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'plan.unshared',
|
||||
entityType: 'plan',
|
||||
entityId: req.params.id,
|
||||
details: { userId: req.params.userId },
|
||||
});
|
||||
res.status(204).end();
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -488,13 +488,15 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* @openapi
|
||||
* /trainer/trainees/{userId}/plans/{planId}:
|
||||
* patch:
|
||||
* summary: Replace plan structure (draft only). Same body as POST (gymId, weeks[].customName)
|
||||
* summary: Replace plan structure (draft only). Same body as POST (gymId, weeks[].customName).
|
||||
* Optional startDate (a Monday, current or future week) updates and activates the plan in one step.
|
||||
* tags: [Trainer]
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
* 400: { description: Not a draft / gym not a trainee membership }
|
||||
* 400: { description: Not a draft / gym not a trainee membership / start date not a Monday / start week in the past }
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee or plan not found }
|
||||
* 409: { description: Trainee already has an active plan }
|
||||
*/
|
||||
router.patch(
|
||||
'/trainees/:userId/plans/:planId',
|
||||
|
||||
Reference in New Issue
Block a user