sort array to make algorithm happy
This commit is contained in:
parent
91361ea9c6
commit
04ddfadbb6
@ -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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user