Fix issue #20: Add notes to a map
This commit is contained in:
@@ -4,6 +4,7 @@ const router = express.Router();
|
||||
const Campaign = require('../models/Campaign');
|
||||
const Note = require('../models/Note');
|
||||
const Folder = require('../models/Folder');
|
||||
const Map = require('../models/Map');
|
||||
|
||||
async function userOwnsCampaign(campaignId, userId) {
|
||||
const campaign = await Campaign.findOne({ _id: campaignId, createdBy: userId }).lean();
|
||||
@@ -64,6 +65,49 @@ router.get('/subfolder/list', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Every note of the campaign, folders included, without their content: enough to offer a note
|
||||
// picker and to label the pins other screens put on a map.
|
||||
router.get('/all', async (req, res) => {
|
||||
try {
|
||||
const { campaign } = req.query;
|
||||
if (!campaign) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const hasAccess = await userOwnsCampaign(campaign, req.user.id);
|
||||
if (!hasAccess) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
const notes = await Note.find({ campaign })
|
||||
.select('_id title folder date')
|
||||
.sort({ title: 1 })
|
||||
.lean();
|
||||
|
||||
const folders = await Folder.find({ campaign })
|
||||
.select('_id name parentFolder')
|
||||
.lean();
|
||||
|
||||
res.json({ status: 'ok', notes, folders });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.json({ status: 'error', msg: 'errors.internal' });
|
||||
}
|
||||
});
|
||||
|
||||
// One note with its content, for the screens that show a note on their own (a map pin, for
|
||||
// instance) instead of from the sidebar's note list.
|
||||
router.get('/retrieve/:id', async (req, res) => {
|
||||
try {
|
||||
const note = await Note.findById(req.params.id).lean();
|
||||
if (!note) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
const hasAccess = await userOwnsCampaign(note.campaign, req.user.id);
|
||||
if (!hasAccess) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
res.json({ status: 'ok', note });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.json({ status: 'error', msg: 'errors.internal' });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/create', async (req, res) => {
|
||||
try {
|
||||
const { title, content, campaign, folder } = req.body;
|
||||
@@ -119,6 +163,9 @@ router.post('/delete', async (req, res) => {
|
||||
const result = await Note.deleteOne({ _id: id });
|
||||
if (result.deletedCount === 0) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
// A pin pointing at a note that is gone would open nothing: drop those along with the note.
|
||||
await Map.updateMany({ 'pins.note': id }, { $pull: { pins: { note: id } } });
|
||||
|
||||
res.json({ status: 'ok' });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
Reference in New Issue
Block a user