From 4b2bf12fd1d78b066cf81a96b4b40703a880be5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 11 Feb 2021 12:35:10 -0500 Subject: [PATCH] feat: #9294, put new categories at top --- src/categories/create.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/categories/create.js b/src/categories/create.js index d5246dd497..a27f0954aa 100644 --- a/src/categories/create.js +++ b/src/categories/create.js @@ -13,11 +13,15 @@ const cache = require('../cache'); module.exports = function (Categories) { Categories.create = async function (data) { const parentCid = data.parentCid ? data.parentCid : 0; - const cid = await db.incrObjectField('global', 'nextCid'); + const [cid, firstChild] = await Promise.all([ + db.incrObjectField('global', 'nextCid'), + db.getSortedSetRangeWithScores(`cid:${parentCid}:children`, 0, 0), + ]); data.name = String(data.name || `Category ${cid}`); const slug = `${cid}/${slugify(data.name)}`; - const order = data.order || cid; // If no order provided, place it at the end + const smallestOrder = firstChild.length ? firstChild[0].score - 1 : 1; + const order = data.order || smallestOrder; // If no order provided, place it at the top const colours = Categories.assignColours(); let category = {