From 6b0c3781d9219f2793229785b3a59b5565d68957 Mon Sep 17 00:00:00 2001
From: Han Xiangyu <admin@pescn.cn>
Date: Thu, 28 Nov 2024 20:40:06 +0800
Subject: [PATCH] fix: update member data request pending

Fixed the issue where the /controller/network/{network_id}/members interface in the UI did not return data, causing the browser to remain in a pending state, ensuring that it can now return 200 or an error normally.
---
 src/controllers/networkController.js | 97 +++++++++++++++-------------
 1 file changed, 52 insertions(+), 45 deletions(-)

diff --git a/src/controllers/networkController.js b/src/controllers/networkController.js
index 50c10ee..b729c70 100644
--- a/src/controllers/networkController.js
+++ b/src/controllers/networkController.js
@@ -627,77 +627,84 @@ exports.easy_post = async function(req, res) {
 }
 
 // Easy members auth POST
-exports.members = async function(req, res) {
+exports.members = async function (req, res) {
   const navigate =
     {
       active: 'networks',
       whence: '/controller/networks'
     }
 
-  let errors = null;
-
-  if (req.method === 'POST') {
-
-    req.checkBody('id', 'Member ID is required').notEmpty();
-    req.sanitize('id').trim();
-    req.sanitize('id').escape();
+  try {
+    if (req.method === 'POST') {
+      // Validate common fields
+      req.checkBody('id', 'Member ID is required').notEmpty();
+      req.sanitize('id').trim();
+      req.sanitize('id').escape();
+
+      // Validate optional fields
+      if (req.body.auth !== undefined) {
+        req.checkBody('auth', 'Authorization state must be boolean').isBoolean();
+        req.sanitize('auth').trim();
+        req.sanitize('auth').escape();
+      }
 
-    if (req.body.auth) {
-      req.checkBody('auth', 'Authorization state must be boolean').isBoolean();
-      req.sanitize('auth').trim();
-      req.sanitize('auth').escape();
+      if (req.body.activeBridge !== undefined) {
+        req.checkBody('activeBridge', 'activeBridge state must be boolean').isBoolean();
+        req.sanitize('activeBridge').trim();
+        req.sanitize('activeBridge').escape();
+      }
 
-      errors = req.validationErrors();
+      if (req.body.name !== undefined) {
+        req.sanitize('name').trim();
+        req.sanitize('name').escape();
+      }
 
-      if (!errors) {
-        const auth =
-          {
-            authorized: req.body.auth
-          };
+      // Collect validation errors
+      const errors = req.validationErrors();
+      if (errors) {
+        return res.status(400).json({ errors: errors.array() });
+      }
 
+      // Simultaneously process all provided fields
+      if (req.body.auth !== undefined) {
+        const auth = { authorized: req.body.auth };
         try {
-          const mem = await zt.member_object(req.params.nwid, req.body.id, auth);
+          await zt.member_object(req.params.nwid, req.body.id, auth);
         } catch (err) {
-          throw err;
+          console.error('Error processing auth:', err);
+          return res.status(500).json({ error: 'Failed to update authorization state' });
         }
       }
-    } else if (req.body.activeBridge) {
-      req.checkBody('activeBridge', 'activeBridge state must be boolean').isBoolean();
-      req.sanitize('activeBridge').trim();
-      req.sanitize('activeBridge').escape();
-
-      errors = req.validationErrors();
-
-      if (!errors) {
-        const activeBridge =
-          {
-            activeBridge: req.body.activeBridge
-          };
 
+      if (req.body.activeBridge !== undefined) {
+        const activeBridge = { activeBridge: req.body.activeBridge };
         try {
-          const mem = await zt.member_object(req.params.nwid, req.body.id, activeBridge);
+          await zt.member_object(req.params.nwid, req.body.id, activeBridge);
         } catch (err) {
-          throw err;
+          console.error('Error processing activeBridge:', err);
+          return res.status(500).json({ error: 'Failed to update activeBridge state' });
         }
       }
-    } else if (req.body.name) {
-      req.sanitize('name').trim();
-      req.sanitize('name').escape();
 
-      errors = req.validationErrors();
-
-      if (!errors) {
+      if (req.body.name !== undefined) {
         try {
-          const ret = await storage.setItem(req.body.id, req.body.name);
+          await storage.setItem(req.body.id, req.body.name);
         } catch (err) {
-          throw err;
+          console.error('Error processing name:', err);
+          return res.status(500).json({ error: 'Failed to update name' });
         }
       }
+
+      // Return success after processing all fields
+      return res.status(200).json({ success: true });
+    } else {
+      return res.redirect(`/controller/network/${nwid}#members`);
     }
-  } else { // GET
-    res.redirect("/controller/network/" + req.params.nwid + "#members");
+  } catch (err) {
+    console.error('Unexpected error:', err);
+    return res.status(500).json({ error: 'Internal server error', detail: err });
   }
-}
+};
 
 // Member delete GET or POST
 exports.member_delete = async function(req, res) {