Plany: PATCH z startDate (aktywacja przy zapisie) i udostepnianie planow trener-podopieczny (PlanShare, sekcja Udostepnione mi, widok read-only)
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
import { describe, it, expect, beforeAll } from 'vitest';
|
||||
import { prisma } from '../../src/lib/prisma.js';
|
||||
import { buildApp, createUser, createExercise, loginViaApi, rand, registerViaApi, seedPlan, PASSWORD } from '../helpers.js';
|
||||
import {
|
||||
buildApp,
|
||||
createUser,
|
||||
createUserWithToken,
|
||||
createExercise,
|
||||
loginViaApi,
|
||||
rand,
|
||||
registerViaApi,
|
||||
seedPlan,
|
||||
PASSWORD,
|
||||
} from '../helpers.js';
|
||||
|
||||
// Daty dynamiczne: tydzień startu musi być bieżący lub przyszły (nie miniony).
|
||||
function mondayIso(offsetWeeks: number): string {
|
||||
@@ -629,3 +639,260 @@ describe('plans: trainer-created plan (creator + owner access)', () => {
|
||||
expect(res.body.error.message).toBe('Plan owner is not a member of the gym');
|
||||
});
|
||||
});
|
||||
|
||||
describe('plans: sharing (PlanShare)', () => {
|
||||
let app: ReturnType<typeof buildApp>;
|
||||
let trainer: { user: { id: string }; token: string };
|
||||
let trainee: { user: { id: string }; token: string };
|
||||
let stranger: { user: { id: string }; token: string };
|
||||
let exerciseId: string;
|
||||
let planId: string;
|
||||
|
||||
const body = (title: string) => ({
|
||||
title,
|
||||
weeksCount: 1,
|
||||
weeks: [
|
||||
{
|
||||
index: 0,
|
||||
days: [
|
||||
{ dayOfWeek: 1, exercises: [{ exerciseId, order: 0, sets: [{ index: 0, reps: 10 }] }] },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
beforeAll(async () => {
|
||||
app = buildApp();
|
||||
trainer = await createUserWithToken({ firstName: 'ShareTrainer' });
|
||||
trainee = await createUserWithToken({ firstName: 'ShareTrainee' });
|
||||
stranger = await createUserWithToken({ firstName: 'ShareStranger' });
|
||||
// ACTIVE coaching: trainer -> trainee
|
||||
await prisma.coaching.create({
|
||||
data: {
|
||||
trainerId: trainer.user.id,
|
||||
userId: trainee.user.id,
|
||||
invitedBy: trainer.user.id,
|
||||
status: 'ACTIVE',
|
||||
expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
|
||||
},
|
||||
});
|
||||
const ex = await createExercise();
|
||||
exerciseId = ex.id;
|
||||
// plan należy do podopiecznego (właściciel = podopieczny)
|
||||
const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${trainee.token}`).send(body('Plan do udostępnienia'));
|
||||
expect(res.status).toBe(201);
|
||||
planId = res.body.plan.id;
|
||||
});
|
||||
|
||||
it('non-owner (trainer) cannot share the plan (403)', async () => {
|
||||
const res = await app
|
||||
.post(`/api/v1/plans/${planId}/share`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`)
|
||||
.send({ userId: trainee.user.id });
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body.error.message).toBe('Only the plan owner can share the plan');
|
||||
});
|
||||
|
||||
it('owner cannot share the plan with themselves (400)', async () => {
|
||||
const res = await app
|
||||
.post(`/api/v1/plans/${planId}/share`)
|
||||
.set('Authorization', `Bearer ${trainee.token}`)
|
||||
.send({ userId: trainee.user.id });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('Cannot share a plan with yourself');
|
||||
});
|
||||
|
||||
it('owner cannot share with a user without an active coaching relation (400)', async () => {
|
||||
const res = await app
|
||||
.post(`/api/v1/plans/${planId}/share`)
|
||||
.set('Authorization', `Bearer ${trainee.token}`)
|
||||
.send({ userId: stranger.user.id });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('No active coaching relation with this user');
|
||||
});
|
||||
|
||||
it('owner shares the plan with the trainer (201)', async () => {
|
||||
const res = await app
|
||||
.post(`/api/v1/plans/${planId}/share`)
|
||||
.set('Authorization', `Bearer ${trainee.token}`)
|
||||
.send({ userId: trainer.user.id });
|
||||
expect(res.status).toBe(201);
|
||||
expect(res.body.share).toMatchObject({ id: expect.any(String) });
|
||||
});
|
||||
|
||||
it('duplicate share is rejected with 409', async () => {
|
||||
const res = await app
|
||||
.post(`/api/v1/plans/${planId}/share`)
|
||||
.set('Authorization', `Bearer ${trainee.token}`)
|
||||
.send({ userId: trainer.user.id });
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error.message).toBe('Plan is already shared with this user');
|
||||
});
|
||||
|
||||
it('the owner sees the recipients in the plan detail', async () => {
|
||||
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${trainee.token}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.plan.shares).toEqual([
|
||||
{ userId: trainer.user.id, user: { id: trainer.user.id, firstName: 'ShareTrainer', lastName: null } },
|
||||
]);
|
||||
expect(res.body.plan.canManage).toBe(true);
|
||||
});
|
||||
|
||||
it('the recipient sees the plan in /plans/shared-with-me', async () => {
|
||||
const res = await app.get('/api/v1/plans/shared-with-me').set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(res.status).toBe(200);
|
||||
const found = res.body.plans.find((p: { id: string }) => p.id === planId);
|
||||
expect(found).toBeTruthy();
|
||||
expect(found.sharedBy).toMatchObject({ id: trainee.user.id, firstName: 'ShareTrainee' });
|
||||
expect(found.progress).toEqual({ completed: 0, total: 1, percent: 0 });
|
||||
});
|
||||
|
||||
it('the trainee sees the trainer in /me/trainers', async () => {
|
||||
const res = await app.get('/api/v1/me/trainers').set('Authorization', `Bearer ${trainee.token}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.trainers).toEqual([
|
||||
{ id: trainer.user.id, firstName: 'ShareTrainer', lastName: null, nickname: null },
|
||||
]);
|
||||
});
|
||||
|
||||
it('the trainer (ACTIVE coaching) still manages the plan via the owner route? NO — owner route is owner-only (404)', async () => {
|
||||
const res = await app
|
||||
.patch(`/api/v1/plans/${planId}`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`)
|
||||
.send(body('Trener edytuje'));
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
|
||||
it('the share survives the coaching ending: recipient keeps read-only access', async () => {
|
||||
await prisma.coaching.updateMany({
|
||||
where: { trainerId: trainer.user.id, userId: trainee.user.id },
|
||||
data: { status: 'ARCHIVED' },
|
||||
});
|
||||
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.plan.canManage).toBe(false);
|
||||
expect(res.body.plan.shares).toBeUndefined();
|
||||
});
|
||||
|
||||
it('a stranger gets 404 for the shared plan', async () => {
|
||||
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${stranger.token}`);
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
|
||||
it('the owner revokes the share (204) and the recipient loses access', async () => {
|
||||
const res = await app
|
||||
.delete(`/api/v1/plans/${planId}/share/${trainer.user.id}`)
|
||||
.set('Authorization', `Bearer ${trainee.token}`);
|
||||
expect(res.status).toBe(204);
|
||||
|
||||
const detail = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(detail.status).toBe(404);
|
||||
|
||||
const list = await app.get('/api/v1/plans/shared-with-me').set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(list.body.plans.find((p: { id: string }) => p.id === planId)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('revoking a missing share is 404', async () => {
|
||||
const res = await app
|
||||
.delete(`/api/v1/plans/${planId}/share/${trainer.user.id}`)
|
||||
.set('Authorization', `Bearer ${trainee.token}`);
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('plans: PATCH with startDate (update + activate)', () => {
|
||||
let app: ReturnType<typeof buildApp>;
|
||||
let token: string;
|
||||
let userId: string;
|
||||
let exerciseId: string;
|
||||
let planId: string;
|
||||
|
||||
const body = (title: string, weeksCount = 1) => ({
|
||||
title,
|
||||
weeksCount,
|
||||
weeks: [
|
||||
{
|
||||
index: 0,
|
||||
days: [
|
||||
{ dayOfWeek: 1, exercises: [{ exerciseId, order: 0, sets: [{ index: 0, reps: 10 }] }] },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
beforeAll(async () => {
|
||||
app = buildApp();
|
||||
const reg = await createUserWithToken();
|
||||
token = reg.token;
|
||||
userId = reg.user.id;
|
||||
const ex = await createExercise();
|
||||
exerciseId = ex.id;
|
||||
const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body('Draft do aktywacji'));
|
||||
expect(res.status).toBe(201);
|
||||
planId = res.body.plan.id;
|
||||
});
|
||||
|
||||
it('PATCH without startDate keeps the plan DRAFT', async () => {
|
||||
const res = await app
|
||||
.patch(`/api/v1/plans/${planId}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send(body('Draft v2'));
|
||||
expect(res.status).toBe(200);
|
||||
const detail = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${token}`);
|
||||
expect(detail.body.plan.status).toBe('DRAFT');
|
||||
expect(detail.body.plan.startDate).toBeNull();
|
||||
});
|
||||
|
||||
it('PATCH with a non-Monday startDate is rejected with 400', async () => {
|
||||
const [y, m, d] = mondayIso(1).split('-').map(Number);
|
||||
const tuesday = new Date(y, m - 1, d + 1);
|
||||
const tuesdayIso = `${tuesday.getFullYear()}-${String(tuesday.getMonth() + 1).padStart(2, '0')}-${String(tuesday.getDate()).padStart(2, '0')}`;
|
||||
const res = await app
|
||||
.patch(`/api/v1/plans/${planId}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ ...body('Wtorek'), startDate: tuesdayIso });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('Start date must be a Monday');
|
||||
});
|
||||
|
||||
it('PATCH with a past-week startDate is rejected with 400', async () => {
|
||||
const res = await app
|
||||
.patch(`/api/v1/plans/${planId}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ ...body('Miniony'), startDate: mondayIso(-1) });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('Start date cannot be in the past');
|
||||
});
|
||||
|
||||
it('PATCH with startDate updates the structure and activates the plan', async () => {
|
||||
const res = await app
|
||||
.patch(`/api/v1/plans/${planId}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ ...body('Aktywowany'), startDate: mondayIso(1) });
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const detail = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${token}`);
|
||||
expect(detail.body.plan.status).toBe('ACTIVE');
|
||||
expect(detail.body.plan.title).toBe('Aktywowany');
|
||||
expect(detail.body.plan.startDate).not.toBeNull();
|
||||
const localDate = (iso: string) => {
|
||||
const d = new Date(iso);
|
||||
return [d.getFullYear(), d.getMonth() + 1, d.getDate()];
|
||||
};
|
||||
const [sy, sm, sd] = mondayIso(1).split('-').map(Number);
|
||||
const day1 = detail.body.plan.weeks.find((w: { index: number }) => w.index === 0).days.find(
|
||||
(d: { dayOfWeek: number }) => d.dayOfWeek === 1,
|
||||
);
|
||||
expect(localDate(day1.date)).toEqual([sy, sm, sd]);
|
||||
});
|
||||
|
||||
it('PATCH with startDate while another plan is active is rejected with 409', async () => {
|
||||
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body('Konflikt'));
|
||||
const res = await app
|
||||
.patch(`/api/v1/plans/${created.body.plan.id}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ ...body('Konflikt'), startDate: mondayIso(2) });
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error.message).toBe('User already has an active plan');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user