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