From cf32a8a127e9f47eac7ee7a23c451de9a8a3b873 Mon Sep 17 00:00:00 2001
From: L4168 <L4168@student.jamk.fi>
Date: Thu, 18 Jul 2019 10:46:02 +0300
Subject: [PATCH] add error for invalid notification socket message

---
 src/notifications/notifications.gateway.ts | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/notifications/notifications.gateway.ts b/src/notifications/notifications.gateway.ts
index 5e952d6..cc3ad3a 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',
+      });
     }
   }
 }
-- 
GitLab