From 04ddfadbb6287097c26037a4a151bdb18c2fc49e Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Wed, 30 Apr 2025 22:49:40 -0500 Subject: [PATCH] sort array to make algorithm happy --- src/api/routes/guilds/#guild_id/channels.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/api/routes/guilds/#guild_id/channels.ts b/src/api/routes/guilds/#guild_id/channels.ts index a2add70f..2eccb3e1 100644 --- a/src/api/routes/guilds/#guild_id/channels.ts +++ b/src/api/routes/guilds/#guild_id/channels.ts @@ -108,13 +108,32 @@ router.patch( async (req: Request, res: Response) => { // changes guild channel position const { guild_id } = req.params; - const body = req.body as ChannelReorderSchema; + let body = req.body as ChannelReorderSchema; const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: { channel_ordering: true }, }); + body = body.sort((a, b) => { + const apos = + a.position || + (a.parent_id + ? guild.channel_ordering.findIndex( + (_) => _ === a.parent_id, + ) + 1 + : 0); + const bpos = + b.position || + (b.parent_id + ? guild.channel_ordering.findIndex( + (_) => _ === b.parent_id, + ) + 1 + : 0); + console.log(apos, bpos); + return apos - bpos; + }); + // The channels not listed for this query const notMentioned = guild.channel_ordering.filter( (x) => !body.find((c) => c.id == x),