Plany i sesje: porzucenie/konczenie tylko z ACTIVE, przyszle dni bez skip, usuwanie tylko cwiczen dodanych do sesji
This commit is contained in:
@@ -337,16 +337,47 @@ describe('plans routes', () => {
|
||||
.post(`/api/v1/plans/${planId}/abandon`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(again.status).toBe(400);
|
||||
expect(again.body.error.message).toBe('Plan is already closed');
|
||||
expect(again.body.error.message).toBe('Only an active plan can be abandoned');
|
||||
});
|
||||
|
||||
it('completes a draft plan and refuses to close it twice', async () => {
|
||||
it('rejects abandoning a draft plan with 400', async () => {
|
||||
const app = buildApp();
|
||||
const created = await app
|
||||
.post('/api/v1/plans')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send(planBody('Draft abandon'));
|
||||
const res = await app
|
||||
.post(`/api/v1/plans/${created.body.plan.id}/abandon`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('Only an active plan can be abandoned');
|
||||
});
|
||||
|
||||
it('rejects completing a draft plan with 400', async () => {
|
||||
const app = buildApp();
|
||||
const created = await app
|
||||
.post('/api/v1/plans')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send(planBody('Draft complete'));
|
||||
const res = await app
|
||||
.post(`/api/v1/plans/${created.body.plan.id}/complete`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('Only an active plan can be completed');
|
||||
});
|
||||
|
||||
it('completes an active plan and refuses to close it twice', async () => {
|
||||
const app = buildApp();
|
||||
const created = await app
|
||||
.post('/api/v1/plans')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send(planBody('To complete'));
|
||||
const id = created.body.plan.id;
|
||||
const activated = await app
|
||||
.post(`/api/v1/plans/${id}/activate`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ startDate: MONDAY });
|
||||
expect(activated.status).toBe(200);
|
||||
const res = await app
|
||||
.post(`/api/v1/plans/${id}/complete`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
import { describe, it, expect, beforeAll } from 'vitest';
|
||||
import { buildApp, createUser, createExercise, loginViaApi, registerViaApi, seedPlan, seedSession, PASSWORD } from '../helpers.js';
|
||||
import {
|
||||
buildApp,
|
||||
createUser,
|
||||
createExercise,
|
||||
loginViaApi,
|
||||
registerViaApi,
|
||||
seedPlan,
|
||||
seedSession,
|
||||
PASSWORD,
|
||||
} from '../helpers.js';
|
||||
|
||||
describe('sessions routes', () => {
|
||||
let token: string;
|
||||
@@ -53,12 +62,16 @@ describe('sessions routes', () => {
|
||||
|
||||
it('starts a session for a planned day and initializes exercises + sets from the plan', async () => {
|
||||
const app = buildApp();
|
||||
const res = await app.post(`/api/v1/days/${dayId}/sessions`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/days/${dayId}/sessions`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(201);
|
||||
const sessionId = res.body.sessionId;
|
||||
expect(sessionId).toBeTruthy();
|
||||
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(detail.status).toBe(200);
|
||||
const session = detail.body.session;
|
||||
expect(session.status).toBe('IN_PROGRESS');
|
||||
@@ -66,7 +79,9 @@ describe('sessions routes', () => {
|
||||
const ex = session.exercises[0];
|
||||
expect(ex.planExerciseId).toBe(planExerciseId);
|
||||
expect(ex.sets).toHaveLength(2);
|
||||
expect(ex.sets.map((s: { planSetId: string }) => s.planSetId).sort()).toEqual([...planSetIds].sort());
|
||||
expect(ex.sets.map((s: { planSetId: string }) => s.planSetId).sort()).toEqual(
|
||||
[...planSetIds].sort(),
|
||||
);
|
||||
expect(ex.sets.every((s: { reps: number | null }) => s.reps === null)).toBe(true);
|
||||
});
|
||||
|
||||
@@ -89,14 +104,18 @@ describe('sessions routes', () => {
|
||||
|
||||
it('rejects a second active session for the same day with 409', async () => {
|
||||
const app = buildApp();
|
||||
const res = await app.post(`/api/v1/days/${dayId}/sessions`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/days/${dayId}/sessions`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error.message).toBe('You already have an active session');
|
||||
});
|
||||
|
||||
it('rejects starting another day while a session is active with 409', async () => {
|
||||
const app = buildApp();
|
||||
const res = await app.post(`/api/v1/days/${day2Id}/sessions`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/days/${day2Id}/sessions`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error.message).toBe('You already have an active session');
|
||||
});
|
||||
@@ -118,7 +137,9 @@ describe('sessions routes', () => {
|
||||
|
||||
it('returns a null active session for a user without sessions', async () => {
|
||||
const app = buildApp();
|
||||
const res = await app.get('/api/v1/me/active-session').set('Authorization', `Bearer ${otherToken}`);
|
||||
const res = await app
|
||||
.get('/api/v1/me/active-session')
|
||||
.set('Authorization', `Bearer ${otherToken}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.session).toBeNull();
|
||||
});
|
||||
@@ -177,8 +198,12 @@ describe('sessions routes', () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const addedExercise = detail.body.session.exercises.find((e: { planExerciseId: string | null }) => !e.planExerciseId);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const addedExercise = detail.body.session.exercises.find(
|
||||
(e: { planExerciseId: string | null }) => !e.planExerciseId,
|
||||
);
|
||||
|
||||
const hide = await app
|
||||
.patch(`/api/v1/sessions/exercises/${addedExercise.id}`)
|
||||
@@ -193,34 +218,69 @@ describe('sessions routes', () => {
|
||||
expect(remove.status).toBe(200);
|
||||
});
|
||||
|
||||
it('rejects removing a planned (from-plan) exercise with 400 — only hidden is allowed', async () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const planned = detail.body.session.exercises.find(
|
||||
(e: { planExerciseId: string | null }) => e.planExerciseId,
|
||||
);
|
||||
|
||||
const remove = await app
|
||||
.patch(`/api/v1/sessions/exercises/${planned.id}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ removed: true });
|
||||
expect(remove.status).toBe(400);
|
||||
expect(remove.body.error.message).toBe('Only exercises added to the session can be removed');
|
||||
|
||||
const hide = await app
|
||||
.patch(`/api/v1/sessions/exercises/${planned.id}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ hidden: true });
|
||||
expect(hide.status).toBe(200);
|
||||
});
|
||||
|
||||
it('rejects adding an extra set before the norm is done with 400', async () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const ex = detail.body.session.exercises[0];
|
||||
|
||||
const res = await app
|
||||
.post(`/api/v1/sessions/exercises/${ex.id}/sets`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('All planned sets must be recorded before adding an extra set');
|
||||
expect(res.body.error.message).toBe(
|
||||
'All planned sets must be recorded before adding an extra set',
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects completing the session while norm sets are pending with 400', async () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const res = await app.post(`/api/v1/sessions/${sessionId}/complete`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/sessions/${sessionId}/complete`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('All planned sets must be recorded before completing the session (2 remaining)');
|
||||
expect(res.body.error.message).toBe(
|
||||
'All planned sets must be recorded before completing the session (2 remaining)',
|
||||
);
|
||||
});
|
||||
|
||||
it('records the norm sets and adds an extra set once the norm is done', async () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const ex = detail.body.session.exercises[0];
|
||||
|
||||
for (const set of ex.sets) {
|
||||
@@ -244,7 +304,9 @@ describe('sessions routes', () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const ex = detail.body.session.exercises[0];
|
||||
|
||||
const res = await app
|
||||
@@ -258,7 +320,9 @@ describe('sessions routes', () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const ex = detail.body.session.exercises[0];
|
||||
const extra = ex.sets.find((s: { planSetId: string | null }) => s.planSetId == null);
|
||||
expect(extra).toBeTruthy();
|
||||
@@ -270,7 +334,9 @@ describe('sessions routes', () => {
|
||||
expect(res.body).toEqual({ ok: true });
|
||||
|
||||
// the removed set no longer appears in the session detail
|
||||
const after = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const after = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const afterEx = after.body.session.exercises[0];
|
||||
expect(afterEx.sets.some((s: { id: string }) => s.id === extra.id)).toBe(false);
|
||||
});
|
||||
@@ -279,7 +345,9 @@ describe('sessions routes', () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const ex = detail.body.session.exercises[0];
|
||||
const norm = ex.sets.find((s: { planSetId: string | null }) => s.planSetId != null);
|
||||
|
||||
@@ -294,7 +362,9 @@ describe('sessions routes', () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const set = detail.body.session.exercises[0].sets[0];
|
||||
|
||||
const res = await app
|
||||
@@ -309,7 +379,9 @@ describe('sessions routes', () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const set = detail.body.session.exercises[0].sets[0];
|
||||
const res = await app
|
||||
.patch(`/api/v1/sessions/sets/${set.id}`)
|
||||
@@ -322,7 +394,9 @@ describe('sessions routes', () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const detail = await app.get(`/api/v1/sessions/${sessionId}`).set('Authorization', `Bearer ${token}`);
|
||||
const detail = await app
|
||||
.get(`/api/v1/sessions/${sessionId}`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
const set = detail.body.session.exercises[0].sets[0];
|
||||
const res = await app
|
||||
.patch(`/api/v1/sessions/sets/${set.id}`)
|
||||
@@ -336,7 +410,9 @@ describe('sessions routes', () => {
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
|
||||
const res = await app.post(`/api/v1/sessions/${sessionId}/complete`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/sessions/${sessionId}/complete`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const day = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
@@ -347,24 +423,32 @@ describe('sessions routes', () => {
|
||||
const app = buildApp();
|
||||
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
|
||||
const sessionId = session.body.day.sessions[0].id;
|
||||
const res = await app.post(`/api/v1/sessions/${sessionId}/complete`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/sessions/${sessionId}/complete`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it('rejects starting a session on a non-planned day with 400', async () => {
|
||||
const app = buildApp();
|
||||
const res = await app.post(`/api/v1/days/${dayId}/sessions`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/days/${dayId}/sessions`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('Day is not in planned state');
|
||||
});
|
||||
|
||||
it('abandons a session and marks the day skipped', async () => {
|
||||
const app = buildApp();
|
||||
const start = await app.post(`/api/v1/days/${day2Id}/sessions`).set('Authorization', `Bearer ${token}`);
|
||||
const start = await app
|
||||
.post(`/api/v1/days/${day2Id}/sessions`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(start.status).toBe(201);
|
||||
const sessionId = start.body.sessionId;
|
||||
|
||||
const res = await app.post(`/api/v1/sessions/${sessionId}/abandon`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/sessions/${sessionId}/abandon`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const day = await app.get(`/api/v1/days/${day2Id}`).set('Authorization', `Bearer ${token}`);
|
||||
@@ -374,7 +458,9 @@ describe('sessions routes', () => {
|
||||
|
||||
it('rejects starting a new session on a day with an abandoned session with 400', async () => {
|
||||
const app = buildApp();
|
||||
const res = await app.post(`/api/v1/days/${day2Id}/sessions`).set('Authorization', `Bearer ${token}`);
|
||||
const res = await app
|
||||
.post(`/api/v1/days/${day2Id}/sessions`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('Day is not in planned state');
|
||||
});
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
import { describe, it, expect, beforeAll } from 'vitest';
|
||||
import { prisma } from '../../src/lib/prisma.js';
|
||||
import { buildApp, createExercise, createUser, loginViaApi, rand, seedPlan, seedSession, PASSWORD } from '../helpers.js';
|
||||
import {
|
||||
buildApp,
|
||||
createExercise,
|
||||
createUser,
|
||||
loginViaApi,
|
||||
rand,
|
||||
seedPlan,
|
||||
seedSession,
|
||||
PASSWORD,
|
||||
} from '../helpers.js';
|
||||
|
||||
async function createMembership(userId: string, gymId: string, roles: string[]) {
|
||||
return prisma.gymMembership.create({
|
||||
@@ -44,7 +53,9 @@ describe('trainer — invitations and trainees', () => {
|
||||
|
||||
describe('authorization', () => {
|
||||
it('rejects /trainer/trainees for a user without the TRAINER role with 403', async () => {
|
||||
const res = await app.get('/api/v1/trainer/trainees').set('Authorization', `Bearer ${plain.token}`);
|
||||
const res = await app
|
||||
.get('/api/v1/trainer/trainees')
|
||||
.set('Authorization', `Bearer ${plain.token}`);
|
||||
expect(res.status).toBe(403);
|
||||
});
|
||||
|
||||
@@ -110,14 +121,20 @@ describe('trainer — invitations and trainees', () => {
|
||||
});
|
||||
|
||||
it('lists pending invitations for both parties with the right direction', async () => {
|
||||
const sent = await app.get('/api/v1/trainer/invitations').set('Authorization', `Bearer ${trainer.token}`);
|
||||
const sent = await app
|
||||
.get('/api/v1/trainer/invitations')
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(sent.status).toBe(200);
|
||||
const sentItem = sent.body.invitations.find((i: { other: { id: string } }) => i.other.id === trainee.id);
|
||||
const sentItem = sent.body.invitations.find(
|
||||
(i: { other: { id: string } }) => i.other.id === trainee.id,
|
||||
);
|
||||
expect(sentItem).toMatchObject({ direction: 'sent' });
|
||||
expect(sentItem.other).toMatchObject({ id: trainee.id, email: trainee.email });
|
||||
expect(sentItem.other).toHaveProperty('nickname');
|
||||
|
||||
const received = await app.get('/api/v1/trainer/invitations').set('Authorization', `Bearer ${trainee.token}`);
|
||||
const received = await app
|
||||
.get('/api/v1/trainer/invitations')
|
||||
.set('Authorization', `Bearer ${trainee.token}`);
|
||||
expect(received.status).toBe(200);
|
||||
const receivedItem = received.body.invitations.find(
|
||||
(i: { other: { id: string } }) => i.other.id === trainer.id,
|
||||
@@ -273,9 +290,13 @@ describe('trainer — invitations and trainees', () => {
|
||||
|
||||
describe('trainee list and detail', () => {
|
||||
it('lists active trainees with metrics (pending relations excluded)', async () => {
|
||||
const res = await app.get('/api/v1/trainer/trainees').set('Authorization', `Bearer ${trainer.token}`);
|
||||
const res = await app
|
||||
.get('/api/v1/trainer/trainees')
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(res.status).toBe(200);
|
||||
const found = res.body.trainees.find((t: { user: { id: string } }) => t.user.id === trainee.id);
|
||||
const found = res.body.trainees.find(
|
||||
(t: { user: { id: string } }) => t.user.id === trainee.id,
|
||||
);
|
||||
expect(found).toBeTruthy();
|
||||
expect(found.user).toMatchObject({ id: trainee.id, firstName: 'Anna' });
|
||||
expect(found.gyms.some((g: { id: string }) => g.id === gymId)).toBe(true);
|
||||
@@ -289,14 +310,18 @@ describe('trainer — invitations and trainees', () => {
|
||||
.get(`/api/v1/trainer/trainees?gymId=${gymId}`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(res.status).toBe(200);
|
||||
const found = res.body.trainees.find((t: { user: { id: string } }) => t.user.id === trainee.id);
|
||||
const found = res.body.trainees.find(
|
||||
(t: { user: { id: string } }) => t.user.id === trainee.id,
|
||||
);
|
||||
expect(found).toBeTruthy();
|
||||
|
||||
const otherGym = await prisma.gym.create({ data: { name: `Other Gym ${rand()}` } });
|
||||
const filtered = await app
|
||||
.get(`/api/v1/trainer/trainees?gymId=${otherGym.id}`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(filtered.body.trainees.find((t: { user: { id: string } }) => t.user.id === trainee.id)).toBeUndefined();
|
||||
expect(
|
||||
filtered.body.trainees.find((t: { user: { id: string } }) => t.user.id === trainee.id),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('returns 404 for a detail of a non-assigned user', async () => {
|
||||
@@ -333,7 +358,9 @@ describe('trainer — invitations and trainees', () => {
|
||||
});
|
||||
|
||||
it('includes gym equipment in the detail gyms (limits exercises in the plan editor)', async () => {
|
||||
const equipment = await prisma.equipment.create({ data: { name: `Sztanga ${rand()}`, kind: 'BARBELL' } });
|
||||
const equipment = await prisma.equipment.create({
|
||||
data: { name: `Sztanga ${rand()}`, kind: 'BARBELL' },
|
||||
});
|
||||
await prisma.gymEquipment.create({ data: { gymId, equipmentId: equipment.id, quantity: 2 } });
|
||||
|
||||
const res = await app
|
||||
@@ -368,15 +395,21 @@ describe('trainer — invitations and trainees', () => {
|
||||
},
|
||||
});
|
||||
|
||||
const list = await app.get('/api/v1/trainer/trainees').set('Authorization', `Bearer ${trainer.token}`);
|
||||
const found = list.body.trainees.find((t: { user: { id: string } }) => t.user.id === trainee.id);
|
||||
const list = await app
|
||||
.get('/api/v1/trainer/trainees')
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
const found = list.body.trainees.find(
|
||||
(t: { user: { id: string } }) => t.user.id === trainee.id,
|
||||
);
|
||||
expect(found.user).toMatchObject({ id: trainee.id, nickname: 'annafit' });
|
||||
|
||||
const detail = await app
|
||||
.get(`/api/v1/trainer/trainees/${trainee.id}`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(detail.body.user).toMatchObject({ id: trainee.id, nickname: 'annafit' });
|
||||
expect(detail.body.socialMedia).toEqual([{ platform: 'TIKTOK', url: 'https://tiktok.com/@annafit' }]);
|
||||
expect(detail.body.socialMedia).toEqual([
|
||||
{ platform: 'TIKTOK', url: 'https://tiktok.com/@annafit' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('archives the relation and removes the trainee from the list', async () => {
|
||||
@@ -384,8 +417,12 @@ describe('trainer — invitations and trainees', () => {
|
||||
.delete(`/api/v1/trainer/trainees/${trainee.id}`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(res.status).toBe(204);
|
||||
const list = await app.get('/api/v1/trainer/trainees').set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(list.body.trainees.find((t: { user: { id: string } }) => t.user.id === trainee.id)).toBeUndefined();
|
||||
const list = await app
|
||||
.get('/api/v1/trainer/trainees')
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(
|
||||
list.body.trainees.find((t: { user: { id: string } }) => t.user.id === trainee.id),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects archiving a non-assigned user with 404', async () => {
|
||||
@@ -618,7 +655,9 @@ describe('trainer — managing a trainee plan', () => {
|
||||
days: [
|
||||
{
|
||||
dayOfWeek: 1,
|
||||
exercises: [{ exerciseId, order: 0, sets: [{ index: 0, reps: 10, loadKg: 60, restSec: 90 }] }],
|
||||
exercises: [
|
||||
{ exerciseId, order: 0, sets: [{ index: 0, reps: 10, loadKg: 60, restSec: 90 }] },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -690,7 +729,9 @@ describe('trainer — managing a trainee plan', () => {
|
||||
});
|
||||
|
||||
it('the owner (trainee) can read and edit the trainer-created draft', async () => {
|
||||
const read = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${trainee.token}`);
|
||||
const read = await app
|
||||
.get(`/api/v1/plans/${planId}`)
|
||||
.set('Authorization', `Bearer ${trainee.token}`);
|
||||
expect(read.status).toBe(200);
|
||||
const patch = await app
|
||||
.patch(`/api/v1/plans/${planId}`)
|
||||
@@ -763,11 +804,29 @@ describe('trainer — managing a trainee plan', () => {
|
||||
expect(stored!.status).toBe('ABANDONED');
|
||||
});
|
||||
|
||||
it('completes a draft plan', async () => {
|
||||
it('rejects completing a draft plan with 400', async () => {
|
||||
const created = await app
|
||||
.post(`/api/v1/trainer/trainees/${trainee.id}/plans`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`)
|
||||
.send(planBody('Draft complete'));
|
||||
const res = await app
|
||||
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${created.body.plan.id}/complete`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.message).toBe('Only an active plan can be completed');
|
||||
});
|
||||
|
||||
it('completes an active plan', async () => {
|
||||
// porzucony plan odzwolnił miejsce — nowy szkic da się aktywować
|
||||
const created = await app
|
||||
.post(`/api/v1/trainer/trainees/${trainee.id}/plans`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`)
|
||||
.send(planBody('To complete'));
|
||||
const activated = await app
|
||||
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${created.body.plan.id}/activate`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`)
|
||||
.send({ startDate: mondayIso(1) });
|
||||
expect(activated.status).toBe(200);
|
||||
const res = await app
|
||||
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${created.body.plan.id}/complete`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
@@ -801,7 +860,11 @@ describe('trainer — managing a trainee plan', () => {
|
||||
date: new Date('2026-08-17T00:00:00Z'),
|
||||
status: 'COMPLETED',
|
||||
exercises: [
|
||||
{ exerciseId: ex.id, order: 0, sets: [{ index: 0, reps: 10, loadKg: 60, restSec: 90 }] },
|
||||
{
|
||||
exerciseId: ex.id,
|
||||
order: 0,
|
||||
sets: [{ index: 0, reps: 10, loadKg: 60, restSec: 90 }],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
@@ -813,7 +876,9 @@ describe('trainer — managing a trainee plan', () => {
|
||||
await seedSession(dayId, {
|
||||
status: 'COMPLETED',
|
||||
startedAt: new Date('2026-08-17T07:00:00Z'),
|
||||
exercises: [{ exerciseId: ex.id, order: 0, sets: [{ planSetId: planSet1.id, reps: 10, index: 0 }] }],
|
||||
exercises: [
|
||||
{ exerciseId: ex.id, order: 0, sets: [{ planSetId: planSet1.id, reps: 10, index: 0 }] },
|
||||
],
|
||||
});
|
||||
const res = await app
|
||||
.get(`/api/v1/trainer/trainees/${trainee.id}/exercise-latest`)
|
||||
@@ -922,7 +987,9 @@ describe('trainer — trainee calendar', () => {
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(res.status).toBe(200);
|
||||
const now = new Date();
|
||||
expect(res.body.month).toBe(`${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`);
|
||||
expect(res.body.month).toBe(
|
||||
`${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`,
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects an invalid month format with 400', async () => {
|
||||
|
||||
Reference in New Issue
Block a user