Api+App: komentarze usera - statusy przeczytania i typ USER_COMMENT (feed, profil, panel trenera)
This commit is contained in:
@@ -19,6 +19,7 @@ describe('comments — under executed exercises', () => {
|
||||
let traineeAbandonedSessionId: string;
|
||||
let traineeAbandonedExerciseId: string;
|
||||
let traineeCommentId: string;
|
||||
let traineeExerciseName: string;
|
||||
let mySessionId: string;
|
||||
let myExerciseId: string;
|
||||
|
||||
@@ -46,6 +47,7 @@ describe('comments — under executed exercises', () => {
|
||||
|
||||
// Trainee: COMPLETED session + IN_PROGRESS session.
|
||||
const exercise = await createExercise();
|
||||
traineeExerciseName = exercise.name;
|
||||
const plan = await seedPlan({
|
||||
userId: trainee.id,
|
||||
weeks: [
|
||||
@@ -254,8 +256,9 @@ describe('comments — under executed exercises', () => {
|
||||
const session = res.body.sessions.find((s: { id: string }) => s.id === traineeSessionId);
|
||||
expect(session).toBeTruthy();
|
||||
const exercise = session.exercises.find((e: { id: string }) => e.id === traineeExerciseId);
|
||||
// Autor (trener) zawsze widzi własny komentarz jako przeczytany.
|
||||
expect(exercise.comments).toEqual([
|
||||
expect.objectContaining({ id: traineeCommentId, authorName: 'Tomasz', body: 'Świetna forma!' }),
|
||||
expect.objectContaining({ id: traineeCommentId, authorName: 'Tomasz', read: true }),
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -264,8 +267,9 @@ describe('comments — under executed exercises', () => {
|
||||
expect(res.status).toBe(200);
|
||||
const session = res.body.sessions.find((s: { id: string }) => s.id === traineeSessionId);
|
||||
const exercise = session.exercises.find((e: { id: string }) => e.id === traineeExerciseId);
|
||||
// Dla adresata komentarz trenera jest nieprzeczytany (brak wiersza CommentRead).
|
||||
expect(exercise.comments).toEqual([
|
||||
expect.objectContaining({ id: traineeCommentId, authorName: 'Tomasz', body: 'Świetna forma!' }),
|
||||
expect.objectContaining({ id: traineeCommentId, authorName: 'Tomasz', read: false }),
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -293,8 +297,15 @@ describe('comments — under executed exercises', () => {
|
||||
});
|
||||
expect(note).toBeTruthy();
|
||||
notificationId = note!.id;
|
||||
expect(note!.title).toBe('Trener Tomasz dodał komentarz do treningu');
|
||||
expect(note!.payload).toMatchObject({ sessionId: traineeSessionId, trainerName: 'Tomasz' });
|
||||
expect(note!.title).toBe(`Trener Tomasz: komentarz przy ćwiczeniu ${traineeExerciseName}`);
|
||||
expect(note!.payload).toMatchObject({
|
||||
sessionId: traineeSessionId,
|
||||
sessionExerciseId: traineeExerciseId,
|
||||
exerciseName: traineeExerciseName,
|
||||
commentId: noteCommentId,
|
||||
authorName: 'Tomasz',
|
||||
preview: 'Zwróć uwagę na kolana',
|
||||
});
|
||||
|
||||
const feed = await app
|
||||
.get('/api/v1/me/feed')
|
||||
@@ -340,6 +351,183 @@ describe('comments — under executed exercises', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('USER_COMMENT notifications (feed symmetry) + read marking', () => {
|
||||
let traineeNoteCommentId: string;
|
||||
|
||||
it('notifies the active coach when the trainee comments on their own session', async () => {
|
||||
const before = await app
|
||||
.get('/api/v1/me/feed/unread-count')
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(before.status).toBe(200);
|
||||
|
||||
const res = await app
|
||||
.post('/api/v1/me/comments')
|
||||
.set('Authorization', `Bearer ${trainee.token}`)
|
||||
.send({
|
||||
body: 'Barki mnie bolą po wyciskaniu',
|
||||
sessionId: traineeSessionId,
|
||||
sessionExerciseId: traineeExerciseId,
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
traineeNoteCommentId = res.body.comment.id;
|
||||
|
||||
const note = await prisma.notification.findFirst({
|
||||
where: { userId: trainer.id, type: 'USER_COMMENT' },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
expect(note).toBeTruthy();
|
||||
expect(note!.title).toBe(`Anna: komentarz przy ćwiczeniu ${traineeExerciseName}`);
|
||||
expect(note!.payload).toMatchObject({
|
||||
sessionId: traineeSessionId,
|
||||
sessionExerciseId: traineeExerciseId,
|
||||
exerciseName: traineeExerciseName,
|
||||
commentId: traineeNoteCommentId,
|
||||
traineeId: trainee.id,
|
||||
authorName: 'Anna',
|
||||
preview: 'Barki mnie bolą po wyciskaniu',
|
||||
});
|
||||
|
||||
const feed = await app
|
||||
.get('/api/v1/me/feed')
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
const item = feed.body.items.find((i: { id: string }) => i.id === note!.id);
|
||||
expect(item).toMatchObject({ source: 'notification', type: 'USER_COMMENT', read: false });
|
||||
|
||||
const after = await app
|
||||
.get('/api/v1/me/feed/unread-count')
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
expect(after.body.unreadCount).toBe(before.body.unreadCount + 1);
|
||||
});
|
||||
|
||||
it('coach marks the trainee comment read; flag flips in trainee history', async () => {
|
||||
const before = await app
|
||||
.get(`/api/v1/trainer/trainees/${trainee.id}/history`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
const exBefore = before.body.sessions
|
||||
.find((s: { id: string }) => s.id === traineeSessionId)
|
||||
.exercises.find((e: { id: string }) => e.id === traineeExerciseId);
|
||||
expect(exBefore.comments.find((c: { id: string }) => c.id === traineeNoteCommentId).read).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
const mark = await app
|
||||
.post('/api/v1/me/comments/read')
|
||||
.set('Authorization', `Bearer ${trainer.token}`)
|
||||
.send({ commentIds: [traineeNoteCommentId] });
|
||||
expect(mark.status).toBe(200);
|
||||
|
||||
const after = await app
|
||||
.get(`/api/v1/trainer/trainees/${trainee.id}/history`)
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
const exAfter = after.body.sessions
|
||||
.find((s: { id: string }) => s.id === traineeSessionId)
|
||||
.exercises.find((e: { id: string }) => e.id === traineeExerciseId);
|
||||
expect(exAfter.comments.find((c: { id: string }) => c.id === traineeNoteCommentId).read).toBe(
|
||||
true,
|
||||
);
|
||||
// Wiersz CommentRead należy do widza (trenera), nie do autora.
|
||||
const rows = await prisma.commentRead.findMany({
|
||||
where: { commentId: traineeNoteCommentId },
|
||||
});
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]).toMatchObject({ commentId: traineeNoteCommentId, userId: trainer.id });
|
||||
});
|
||||
|
||||
it('trainee marks the trainer comment read; unknown ids are ignored (idempotent)', async () => {
|
||||
const mark = await app
|
||||
.post('/api/v1/me/comments/read')
|
||||
.set('Authorization', `Bearer ${trainee.token}`)
|
||||
.send({ commentIds: [traineeCommentId, traineeCommentId, 'nieistniejacy-id'] });
|
||||
expect(mark.status).toBe(200);
|
||||
|
||||
const hist = await app.get('/api/v1/me/history').set('Authorization', `Bearer ${trainee.token}`);
|
||||
const exercise = hist.body.sessions
|
||||
.find((s: { id: string }) => s.id === traineeSessionId)
|
||||
.exercises.find((e: { id: string }) => e.id === traineeExerciseId);
|
||||
expect(exercise.comments.find((c: { id: string }) => c.id === traineeCommentId).read).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('never writes read rows for the author or for non-participants', async () => {
|
||||
// Własny komentarz: autor widzi go jako przeczytany bez wiersza CommentRead.
|
||||
const own = await app
|
||||
.post('/api/v1/me/comments')
|
||||
.set('Authorization', `Bearer ${trainer.token}`)
|
||||
.send({ body: 'własna notatka trenera', sessionId: mySessionId, sessionExerciseId: myExerciseId });
|
||||
expect(own.status).toBe(201);
|
||||
const ownMark = await app
|
||||
.post('/api/v1/me/comments/read')
|
||||
.set('Authorization', `Bearer ${trainer.token}`)
|
||||
.send({ commentIds: [own.body.comment.id] });
|
||||
expect(ownMark.status).toBe(200);
|
||||
expect(
|
||||
await prisma.commentRead.count({
|
||||
where: { commentId: own.body.comment.id, userId: trainer.id },
|
||||
}),
|
||||
).toBe(0);
|
||||
const ownHist = await app
|
||||
.get('/api/v1/me/history')
|
||||
.set('Authorization', `Bearer ${trainer.token}`);
|
||||
const ownExercise = ownHist.body.sessions
|
||||
.find((s: { id: string }) => s.id === mySessionId)
|
||||
.exercises.find((e: { id: string }) => e.id === myExerciseId);
|
||||
expect(ownExercise.comments.find((c: { id: string }) => c.id === own.body.comment.id).read).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// Postronny: podopieczny nie oznaczy komentarza trenera w sesji trenera.
|
||||
const outsider = await app
|
||||
.post('/api/v1/me/comments/read')
|
||||
.set('Authorization', `Bearer ${trainee.token}`)
|
||||
.send({ commentIds: [own.body.comment.id] });
|
||||
expect(outsider.status).toBe(200);
|
||||
expect(await prisma.commentRead.count({ where: { commentId: own.body.comment.id } })).toBe(
|
||||
0,
|
||||
);
|
||||
});
|
||||
|
||||
it('no coach (no ACTIVE coaching) = no USER_COMMENT notification', async () => {
|
||||
const solo = await createUserWithToken({ firstName: 'Solo' });
|
||||
const soloExercise = await createExercise();
|
||||
const soloPlan = await seedPlan({
|
||||
userId: solo.user.id,
|
||||
weeks: [
|
||||
{
|
||||
index: 0,
|
||||
days: [
|
||||
{
|
||||
dayOfWeek: 2,
|
||||
exercises: [
|
||||
{ exerciseId: soloExercise.id, order: 0, sets: [{ index: 0, reps: 8, loadKg: 30 }] },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
const soloDay = soloPlan.weeks[0].days[0];
|
||||
const soloSession = await seedSession(soloDay.id, {
|
||||
status: 'COMPLETED',
|
||||
exercises: [
|
||||
{
|
||||
exerciseId: soloExercise.id,
|
||||
planExerciseId: soloDay.exercises[0].id,
|
||||
order: 0,
|
||||
sets: [{ planSetId: soloDay.exercises[0].sets[0].id, reps: 8 }],
|
||||
},
|
||||
],
|
||||
});
|
||||
const before = await prisma.notification.count({ where: { type: 'USER_COMMENT' } });
|
||||
const res = await app
|
||||
.post('/api/v1/me/comments')
|
||||
.set('Authorization', `Bearer ${solo.token}`)
|
||||
.send({ body: 'bez trenera', sessionId: soloSession.id });
|
||||
expect(res.status).toBe(201);
|
||||
expect(await prisma.notification.count({ where: { type: 'USER_COMMENT' } })).toBe(before);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /me/comments', () => {
|
||||
it('creates a comment on my own session + exercise', async () => {
|
||||
const res = await app
|
||||
|
||||
Reference in New Issue
Block a user