sort array to make algorithm happy

This commit is contained in:
MathMan05 2025-04-30 22:49:40 -05:00 committed by Rory&
parent 91361ea9c6
commit 04ddfadbb6

View File

@ -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),