Fix issue #44: Sync item folders
Some checks failed
Test / test (push) Failing after 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-14 23:39:26 +00:00
parent c7d20dd1f7
commit 1f9ab330b9
14 changed files with 981 additions and 23 deletions

View File

@@ -5,12 +5,22 @@ const upload = require('../services/storage');
const Campaign = require('../models/Campaign');
const Item = require('../models/Item');
const Folder = require('../models/Folder');
async function userOwnsCampaign(campaignId, userId) {
const campaign = await Campaign.findOne({ _id: campaignId, createdBy: userId }).lean();
return Boolean(campaign);
}
// Folders the campaign's items are grouped into, in the shape the sidebar groups by
// (the world's own item folders arrive through the sync, see routes/foundry-link.js).
async function itemFolders(campaignId) {
return Folder.find({ campaign: campaignId, target: 'item' })
.select('_id name parentFolder date')
.sort({ date: -1 })
.lean();
}
router.get('/list', async (req, res) => {
try {
const { campaign } = req.query;
@@ -18,7 +28,7 @@ router.get('/list', async (req, res) => {
if (!(await userOwnsCampaign(campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
const items = await Item.find({ campaign }).sort({ name: 1 }).lean();
res.json({ status: 'ok', items });
res.json({ status: 'ok', items, folders: await itemFolders(campaign) });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });