Add lib/api/cap.js: requireWrite(entity_type) maps HTTP method to action, runs canAct, and tags req.capTier as allow|suggest|deny→403. Mutating routes (pages, projects, tasks, refs, resources, source_docs) now check req.capTier and either run the repo (allow) or divert to pending_changes returning 202 (suggest). Owner and worker actors stay on the allow path. requireOwner helper added for Task 11. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
111 lines
3.6 KiB
JavaScript
111 lines
3.6 KiB
JavaScript
import { Router } from 'express';
|
|
import { z } from 'zod';
|
|
import * as repo from '../../db/repos/tasks.js';
|
|
import { validate } from '../validate.js';
|
|
import { NotFoundError, ValidationError, asyncWrap } from '../errors.js';
|
|
import { requireWrite, divertToPending } from '../cap.js';
|
|
|
|
const STATUSES = ['todo', 'doing', 'blocked', 'done'];
|
|
|
|
const createSchema = z.object({
|
|
project_id: z.string().uuid().nullable().optional(),
|
|
title: z.string().min(1).max(500),
|
|
body: z.string().optional(),
|
|
priority: z.number().int().optional(),
|
|
due_at: z.string().datetime().nullable().optional(),
|
|
position: z.number().int().optional()
|
|
});
|
|
|
|
const patchSchema = z.object({
|
|
project_id: z.string().uuid().nullable().optional(),
|
|
title: z.string().min(1).max(500).optional(),
|
|
body: z.string().nullable().optional(),
|
|
status: z.enum(STATUSES).optional(),
|
|
priority: z.number().int().nullable().optional(),
|
|
due_at: z.string().datetime().nullable().optional(),
|
|
position: z.number().int().nullable().optional()
|
|
});
|
|
|
|
const idParams = z.object({ id: z.string().uuid() });
|
|
const spaceParams = z.object({ space_id: z.string().uuid() });
|
|
const projectParams = z.object({ project_id: z.string().uuid() });
|
|
|
|
export const router = Router();
|
|
export const spacesScopedRouter = Router({ mergeParams: true });
|
|
export const projectsScopedRouter = Router({ mergeParams: true });
|
|
|
|
spacesScopedRouter.get('/',
|
|
validate({ params: spaceParams, query: z.object({ status: z.enum(STATUSES).optional() }) }),
|
|
asyncWrap(async (req, res) => {
|
|
res.json(await repo.listBySpace(req.params.space_id, { status: req.validatedQuery.status }));
|
|
})
|
|
);
|
|
|
|
spacesScopedRouter.post('/',
|
|
requireWrite('task'),
|
|
validate({ params: spaceParams, body: createSchema }),
|
|
asyncWrap(async (req, res) => {
|
|
const payload = { ...req.body, space_id: req.params.space_id };
|
|
if (req.capTier === 'suggest') {
|
|
return divertToPending(req, res, { entity_type: 'task', action: 'create', payload });
|
|
}
|
|
try {
|
|
const row = await repo.create(payload, req.actor);
|
|
res.status(201).json(row);
|
|
} catch (e) {
|
|
if (e.code === '23503') throw new ValidationError('invalid space or project', {
|
|
space_id: req.params.space_id, project_id: req.body.project_id
|
|
});
|
|
throw e;
|
|
}
|
|
})
|
|
);
|
|
|
|
projectsScopedRouter.get('/',
|
|
validate({ params: projectParams }),
|
|
asyncWrap(async (req, res) => {
|
|
res.json(await repo.listByProject(req.params.project_id));
|
|
})
|
|
);
|
|
|
|
router.get('/:id',
|
|
validate({ params: idParams }),
|
|
asyncWrap(async (req, res) => {
|
|
const row = await repo.getById(req.params.id);
|
|
if (!row) throw new NotFoundError('task not found');
|
|
res.json(row);
|
|
})
|
|
);
|
|
|
|
router.patch('/:id',
|
|
requireWrite('task'),
|
|
validate({ params: idParams, body: patchSchema }),
|
|
asyncWrap(async (req, res) => {
|
|
const existing = await repo.getById(req.params.id);
|
|
if (!existing) throw new NotFoundError('task not found');
|
|
if (req.capTier === 'suggest') {
|
|
return divertToPending(req, res, {
|
|
entity_type: 'task', entity_id: req.params.id, action: 'update', payload: req.body
|
|
});
|
|
}
|
|
const row = await repo.update(req.params.id, req.body, req.actor);
|
|
res.json(row);
|
|
})
|
|
);
|
|
|
|
router.delete('/:id',
|
|
requireWrite('task'),
|
|
validate({ params: idParams }),
|
|
asyncWrap(async (req, res) => {
|
|
const existing = await repo.getById(req.params.id);
|
|
if (!existing) throw new NotFoundError('task not found');
|
|
if (req.capTier === 'suggest') {
|
|
return divertToPending(req, res, {
|
|
entity_type: 'task', entity_id: req.params.id, action: 'delete', payload: {}
|
|
});
|
|
}
|
|
await repo.del(req.params.id, req.actor);
|
|
res.status(204).end();
|
|
})
|
|
);
|