Fixup join/leave sessions

main
MiguelMLorente 2025-12-01 22:53:50 +01:00
parent 69bc958991
commit 337b4f43e0
2 changed files with 15 additions and 6 deletions

View File

@ -25,7 +25,7 @@ export class SessionController {
id: session.id, id: session.id,
size: session.size, size: session.size,
userCount: session.users.length, userCount: session.users.length,
includesRequester: session.users.includes(user), includesRequester: session.users.find((u) => u.id === userId),
date: session.date, date: session.date,
})); }));
} }
@ -36,7 +36,7 @@ export class SessionController {
@Body('sessionId') sessionId: string, @Body('sessionId') sessionId: string,
) { ) {
const user = await this.userService.getUserById(userId); const user = await this.userService.getUserById(userId);
this.sessionService.joinSession(user, sessionId); await this.sessionService.joinSession(user, sessionId);
} }
@Post('/leave') @Post('/leave')
@ -45,6 +45,6 @@ export class SessionController {
@Body('sessionId') sessionId: string, @Body('sessionId') sessionId: string,
) { ) {
const user = await this.userService.getUserById(userId); const user = await this.userService.getUserById(userId);
this.sessionService.leaveSession(user, sessionId); await this.sessionService.leaveSession(user, sessionId);
} }
} }

View File

@ -22,7 +22,10 @@ export class SessionService {
} }
public getAllSessions() { public getAllSessions() {
return this.sessionRepo.find({ relations: { users: true } }); return this.sessionRepo.find({
relations: { users: true },
order: { date: 'ASC' },
});
} }
public async joinSession(user: User, sessionId: string) { public async joinSession(user: User, sessionId: string) {
@ -30,12 +33,15 @@ export class SessionService {
where: { where: {
id: sessionId, id: sessionId,
}, },
relations: {
users: true,
},
}); });
if (!session) { if (!session) {
throw new NotFoundException(); throw new NotFoundException();
} }
if ( if (
session.users.find((u) => u === user) || session.users.find((u) => u.id === user.id) ||
session.users.length >= session.size session.users.length >= session.size
) { ) {
return; return;
@ -49,11 +55,14 @@ export class SessionService {
where: { where: {
id: sessionId, id: sessionId,
}, },
relations: {
users: true,
},
}); });
if (!session) { if (!session) {
throw new NotFoundException(); throw new NotFoundException();
} }
session.users = session.users.filter((u) => u !== user); session.users = session.users.filter((u) => u.id !== user.id);
await this.sessionRepo.save(session); await this.sessionRepo.save(session);
} }
} }