diff --git a/src/notifications/notifications.gateway.ts b/src/notifications/notifications.gateway.ts index 5e952d6c78c6f35977f362a7459c0ebf65d1ab5e..cc3ad3a802c877439b877efdc551f5b4f8f43d01 100644 --- a/src/notifications/notifications.gateway.ts +++ b/src/notifications/notifications.gateway.ts @@ -50,12 +50,25 @@ export class NotificationGateway async handleMessage(client: Socket, data: NotificationdDTO) { // check if the game exists and is either started or paused const game = await this.gameRepository.findOne({ id: data.game }); - if (game && ['STARTED', 'PAUSED'].includes(game.state)) { + if (!game) { + // inform user about error + this.server.to(client.id).emit(data.game, { + type: 'error', + message: 'Game was not found', + }); + } + if (['STARTED', 'PAUSED'].includes(game.state)) { // send the message to all clients listening to gameId branch this.server.emit(data.game, data); // create entry for notification in db const message = await this.notificationRepository.create(data); await this.notificationRepository.insert(message); + } else { + // inform user about error + this.server.to(client.id).emit(data.game, { + type: 'error', + message: 'Notifications can be sent only in STARTED and PAUSED state', + }); } } }