This commit is contained in:
@@ -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' });
|
||||
|
||||
Reference in New Issue
Block a user