Procházet zdrojové kódy

Fix bug caused by adding users to societies who don't exist

Matt Coles před 9 roky
rodič
revize
e45a6c8425
1 změnil soubory, kde provedl 24 přidání a 25 odebrání
  1. 24 25
      presenters/society-controller.js

+ 24 - 25
presenters/society-controller.js

@@ -46,37 +46,36 @@ module.exports = {
46 46
             redis.hset(soc_query, "description", description);
47 47
             redis.hset(soc_query, "users", admins_str);
48 48
             redis.hset(soc_query, "events", JSON.stringify([]));
49
-            admins.map(function (admin_name) {
49
+            admins.map(function (admin_name, idx, adm) {
50 50
               user_controller.user_exists(admin_name, function (exists) {
51 51
                 if (!exists) {
52 52
                   users_exist = false;
53 53
                 }
54
+                if (idx === adm.length-1 && users_exist) {
55
+                  admins.map(function (admin_name) {
56
+                    redis.hget("user:" + admin_name, "societies", function (err, result) {
57
+                      if (result) {
58
+                        result = JSON.parse(result);
59
+                        redis.hset("user:" + admin_name, "societies", JSON.stringify(result.concat(society_name)));
60
+                      } else {
61
+                        redis.hset("user:" + admin_name, "societies", JSON.stringify([society_name]));
62
+                      }
63
+                    });
64
+                  });
65
+                  complete({"success": 1,
66
+                           "society" : {
67
+                             "name": society_name,
68
+                             "admins": admins,
69
+                             "description": description,
70
+                             "users": admins
71
+                           },
72
+                           "error": 0});
73
+                } else if (idx === adm.length-1) {
74
+                  complete({"success": 0,
75
+                           "error": 4});
76
+                }
54 77
               });
55 78
             });
56
-            if (users_exist) {
57
-              admins.map(function (admin_name) {
58
-                redis.hget("user:" + admin_name, "societies", function (err, result) {
59
-                  if (result) {
60
-                    result = JSON.parse(result);
61
-                    redis.hset("user:" + admin_name, "societies", JSON.stringify(result.concat(society_name)));
62
-                  } else {
63
-                    redis.hset("user:" + admin_name, "societies", JSON.stringify([society_name]));
64
-                  }
65
-                });
66
-              });
67
-              complete({"success": 1,
68
-                       "society" : {
69
-                         "name": society_name,
70
-                         "admins": admins,
71
-                         "description": description,
72
-                         "users": admins
73
-                       },
74
-                       "error": 0});
75
-
76
-            } else {
77
-              complete({"success": 0,
78
-                       "error": 4});
79
-            }
80 79
           } else {
81 80
             complete({"success": 0,
82 81
                      "error": 3});