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

@@ -4,6 +4,7 @@ const router = express.Router();
const Campaign = require('../models/Campaign');
const Folder = require('../models/Folder');
const Note = require('../models/Note');
const Item = require('../models/Item');
async function userOwnsCampaign(campaignId, userId) {
const campaign = await Campaign.findOne({ _id: campaignId, createdBy: userId }).lean();
@@ -18,7 +19,7 @@ router.get('/list', async (req, res) => {
const hasAccess = await userOwnsCampaign(campaign, req.user.id);
if (!hasAccess) return res.json({ status: 'error', msg: 'unauthorized' });
const folders = await Folder.find({ campaign })
const folders = await Folder.find({ campaign, target: { $ne: 'item' } })
.select('_id name date color')
.sort({ date: -1 })
.lean();
@@ -89,6 +90,12 @@ router.post('/delete', async (req, res) => {
{ folder: folderId },
{ $set: { folder: null, date: Date.now() } }
);
// Item folders share this model: the items inside move to the root of
// the item list rather than point at a folder that is gone.
await Item.updateMany(
{ folder: folderId },
{ $set: { folder: null } }
);
await Folder.deleteOne({ _id: folderId });
for (const sub of subfolders) {
await moveRecursive(sub._id);