Files
the-sunken-reach/backend/src/routes/notes.routes.js
T
joshuaandClaude Opus 5.5 94067f1268 V1.0.1: Aldenfell campaign seed, content library, party overview
- Seed now resets the database and seeds the Aldenfell campaign from the
  campaign book: admin DM user, default weapons, locations, bestiary,
  pre-built encounters, and DM/shared notes
- Default weapon/spell/monster library, character profile fields,
  locations and dice mode (new migration)
- Party overview page, pagination, info tips, build preview calc
- Remove stale demo-login hint; notes preserve line breaks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 10:24:48 +02:00

70 lines
2.5 KiB
JavaScript

const express = require('express');
const { z } = require('zod');
const prisma = require('../db');
const { requireAuth } = require('../middleware/auth');
const { loadCampaignMembership } = require('../middleware/campaignAccess');
const router = express.Router();
router.use(requireAuth);
const createNoteSchema = z
.object({
campaignId: z.string().optional(),
characterId: z.string().optional(),
body: z.string().min(1).max(10000),
visibility: z.enum(['DM_ONLY', 'SHARED']).default('SHARED'),
})
.refine((data) => Boolean(data.campaignId) || Boolean(data.characterId), {
message: 'Provide campaignId and/or characterId',
});
router.post('/', async (req, res, next) => {
try {
const parsed = createNoteSchema.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
const { campaignId, characterId, body, visibility } = parsed.data;
let resolvedCampaignId = campaignId || null;
if (characterId) {
const character = await prisma.character.findUnique({ where: { id: characterId } });
if (!character) return res.status(404).json({ error: 'Character not found' });
resolvedCampaignId = resolvedCampaignId || character.campaignId;
}
if (resolvedCampaignId) {
const membership = await loadCampaignMembership(req.user.id, resolvedCampaignId);
if (!membership) return res.status(403).json({ error: 'Not a member of this campaign' });
// A campaign-level (not character-level) DM_ONLY note is the DM's
// private scratchpad — only the DM may create one of those.
if (visibility === 'DM_ONLY' && !characterId && membership.role !== 'DM') {
return res.status(403).json({ error: 'Only the DM can post a DM-only campaign note.' });
}
}
const note = await prisma.note.create({
data: { campaignId, characterId, authorId: req.user.id, body, visibility },
include: { author: { select: { displayName: true } } },
});
res.status(201).json({ note });
} catch (err) {
next(err);
}
});
router.delete('/:id', async (req, res, next) => {
try {
const note = await prisma.note.findUnique({ where: { id: req.params.id } });
if (!note) return res.status(404).json({ error: 'Note not found' });
if (note.authorId !== req.user.id) {
return res.status(403).json({ error: 'You can only delete your own notes' });
}
await prisma.note.delete({ where: { id: req.params.id } });
res.status(204).end();
} catch (err) {
next(err);
}
});
module.exports = router;