UNPKG

@fontoxml/fontoxml-development-tools

Version:

Development tools for Fonto.

71 lines (61 loc) 2.02 kB
import asyncRouteWithLockCleanupHandler from '../asyncRouteWithLockCleanupHandler.js'; import httpStatusCodeHelpers from './review-annotations/httpStatusCodeHelpers.js'; import unmergeableRevisionList from './stores/unmergeableRevisionList.js'; /** @typedef {import('../../src/getAppConfig.js').DevCmsConfig} DevCmsConfig */ /** * @param {DevCmsConfig} config */ export default function configureReviewAnnotationPostRouteHandler(config) { return asyncRouteWithLockCleanupHandler(async (_acquireLock, req, res) => { const { context: { editSessionToken }, annotation, } = req.body; if (!annotation) { res.status(400).send('Missing an "annotation" field in the request.'); return; } const timeoutSet = await config.debugConfiguration.getTimeoutConfigForRoute( req.cms, editSessionToken, '/review/annotation', 'POST', ); if (timeoutSet) { // Do not handle the request, but let it time out. return new Promise(() => {}); } const isDebuggingEnabled = await config.debugConfiguration.isDebuggingEnabled( req.cms, editSessionToken, ); if (isDebuggingEnabled) { const stringToVerify = annotation.metadata.comment; const foundHttpStatusCode = httpStatusCodeHelpers.getDebugHttpStatusCode( stringToVerify, [400, 500], ); if (foundHttpStatusCode && !foundHttpStatusCode.onDelete) { httpStatusCodeHelpers.sendDebugHttpResponse(res, foundHttpStatusCode); return; } } const currentSession = req.getFontoSession(editSessionToken); const annotations = await req.repositories.annotation.addAnnotations( req.cms, currentSession, [annotation], ); const addedAnnotation = annotations[0]; delete addedAnnotation.replies; // Revisions that are the created context of an annotation cannot be merged. unmergeableRevisionList.add(addedAnnotation.createdDocumentRevisionId); res .status(201) .set('content-type', 'application/json; charset=utf-8') .json({ annotation: addedAnnotation, }); }); }