summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-04-11 12:56:32 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-04-11 12:56:32 +1000
commitdebfaea8667257b6179f83f460b4b76d9dec1b01 (patch)
treecdb01af0b3b0017d806c94a6fa28f378d37e2ed2 /src/api
parentRemove Member.isInGuildOrFail, as it will always pass ( route permission check ) (diff)
downloadserver-debfaea8667257b6179f83f460b4b76d9dec1b01.tar.xz
Use partition func instead + use Promise.all
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/guilds/#guild_id/roles/#role_id/members.ts35
1 files changed, 15 insertions, 20 deletions
diff --git a/src/api/routes/guilds/#guild_id/roles/#role_id/members.ts b/src/api/routes/guilds/#guild_id/roles/#role_id/members.ts
index 886b0d1a..6b9fad9c 100644
--- a/src/api/routes/guilds/#guild_id/roles/#role_id/members.ts
+++ b/src/api/routes/guilds/#guild_id/roles/#role_id/members.ts
@@ -17,7 +17,7 @@
 */
 
 import { Router, Request, Response } from "express";
-import { Member } from "@spacebar/util";
+import { Member, partition } from "@spacebar/util";
 import { route } from "@spacebar/api";
 
 const router = Router();
@@ -35,26 +35,21 @@ router.patch(
 			relations: ["roles"],
 		});
 
-		const members_to_add = members.filter((member) => {
-			return (
+		const [add, remove] = partition(
+			members,
+			(member) =>
 				member_ids.includes(member.id) &&
-				!member.roles.map((role) => role.id).includes(role_id)
-			);
-		});
-		const members_to_remove = members.filter((member) => {
-			return (
-				!member_ids.includes(member.id) &&
-				member.roles.map((role) => role.id).includes(role_id)
-			);
-		});
-
-		for (const member of members_to_add) {
-			await Member.addRole(member.id, guild_id, role_id);
-		}
-
-		for (const member of members_to_remove) {
-			await Member.removeRole(member.id, guild_id, role_id);
-		}
+				!member.roles.map((role) => role.id).includes(role_id),
+		);
+
+		await Promise.all([
+			...add.map((member) =>
+				Member.addRole(member.id, guild_id, role_id),
+			),
+			...remove.map((member) =>
+				Member.removeRole(member.id, guild_id, role_id),
+			),
+		]);
 
 		res.sendStatus(204);
 	},