Plany i sesje: porzucenie/konczenie tylko z ACTIVE, przyszle dni bez skip, usuwanie tylko cwiczen dodanych do sesji
This commit is contained in:
@@ -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