From c3597a886477a0037aeea54b9c829e5df8aab9dc Mon Sep 17 00:00:00 2001
From: Matt Kirby <MattJKirby@outlook.com>
Date: Sat, 15 Apr 2023 20:00:24 +0100
Subject: [PATCH] fixed accept and reject

---
 .../src/Routes/RequestsRouter.ts              | 34 +++++++++++--------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/backend-services/friend-service/src/Routes/RequestsRouter.ts b/backend-services/friend-service/src/Routes/RequestsRouter.ts
index 44f447eb..9e0f13e0 100644
--- a/backend-services/friend-service/src/Routes/RequestsRouter.ts
+++ b/backend-services/friend-service/src/Routes/RequestsRouter.ts
@@ -11,13 +11,13 @@ const requestManager = new RequestManager(friendManager);
  * GET '/'
  * Returns a string
  */
-RequestRouter.get('/', async (req:CustomJWTRequest, res:Response): Promise<void> => {
+RequestRouter.get('/', async (req:CustomJWTRequest, res:Response): Promise<Response> => {
   const {token} = req;
 
   return requestManager.GetRequests(token as string).then((result) => {
-    res.status(200).json({requests: result})
+    return res.status(200).json({requests: result})
   }).catch((error: Error) => {
-    res.status(401).json({error: error.message})
+    return res.status(401).json({error: error.message})
   });
 });
 
@@ -47,14 +47,16 @@ RequestRouter.put('/accept', async (req:CustomJWTRequest, res:Response): Promise
   const {request_id} = req.body;
   const {token} = req;
 
-  await requestManager.GetSingleRequest(request_id).then(result => {
+ return await requestManager.GetSingleRequest(request_id).then(result => {
     if(result.TargetUser !== token){
-      return res.status(400).json({error: "unauthorised"})
+      throw new Error("Unauthorised")
+    } else {
+      return requestManager.AcceptRequest(request_id).then((result) => {
+        return res.sendStatus(200)
+      }).catch((error: Error) => {
+        return res.status(400).json({error: error.message})
+      })
     }
-  })
-
-  return requestManager.AcceptRequest(request_id).then((result) => {
-    return res.sendStatus(200)
   }).catch((error: Error) => {
     return res.status(400).json({error: error.message})
   })
@@ -67,14 +69,16 @@ RequestRouter.put('/reject', async (req:CustomJWTRequest, res:Response): Promise
   const {request_id} = req.body;
   const {token} = req;
 
-  await requestManager.GetSingleRequest(request_id).then(result => {
+  return await requestManager.GetSingleRequest(request_id).then(result => {
     if(result.TargetUser !== token){
-      return res.status(400).json({error: "unauthorised"})
+      throw new Error("Unauthorised")
+    } else {
+      return requestManager.RejectRequest(request_id).then((result) => {
+        return res.sendStatus(200)
+      }).catch((error: Error) => {
+        return res.status(400).json({error: error.message})
+      })
     }
-  })
-
-  return requestManager.RejectRequest(request_id).then((result) => {
-    return res.sendStatus(200)
   }).catch((error: Error) => {
     return res.status(400).json({error: error.message})
   })
-- 
GitLab