UNPKG

@fontoxml/fontoxml-development-tools

Version:

Development tools for Fonto.

130 lines (114 loc) 3.31 kB
import asyncRouteWithLockCleanupHandler from '../asyncRouteWithLockCleanupHandler.js'; import httpStatusCodeHelpers from './review-annotations/httpStatusCodeHelpers.js'; import { STATUS_OK } from './review-annotations/Result.js'; /** @typedef {import('../../src/getAppConfig.js').DevCmsConfig} DevCmsConfig */ /** * @param {DevCmsConfig} config */ export default function configureReviewAnnotationReplyPutRouteHandler(config) { return asyncRouteWithLockCleanupHandler(async (_acquireLock, req, res) => { const { context: { editSessionToken }, annotationIdentifier, reply, } = req.body; if (!reply) { res.status(400).send('Missing a "reply" field in the request.'); return; } if (!annotationIdentifier) { res.status(400).send( 'Missing an "annotationIdentifier" field in the request.' ); return; } const timeoutSet = await config.debugConfiguration.getTimeoutConfigForRoute( req.cms, editSessionToken, '/review/annotation/reply', 'PUT' ); if (timeoutSet) { // Do not handle the request, but let it time out. return new Promise(() => {}); } const currentSession = req.getFontoSession(editSessionToken); const isDebuggingEnabled = await config.debugConfiguration.isDebuggingEnabled( req.cms, editSessionToken ); if (isDebuggingEnabled) { const stringToVerify = reply.metadata.reply; let foundHttpStatusCode = httpStatusCodeHelpers.getDebugHttpStatusCode( stringToVerify, [400, 500] ); if (foundHttpStatusCode && !foundHttpStatusCode.onDelete) { httpStatusCodeHelpers.sendDebugHttpResponse( res, foundHttpStatusCode ); return; } foundHttpStatusCode = httpStatusCodeHelpers.getDebugHttpStatusCode( stringToVerify, [403, 404, 412] ); if (foundHttpStatusCode && !foundHttpStatusCode.onDelete) { if (foundHttpStatusCode.statusCode === 403) { httpStatusCodeHelpers.sendDebugHttpResponse( res, foundHttpStatusCode ); return; } if (foundHttpStatusCode.statusCode === 404) { // To be consistent with other debug statusCode overrides, we should delete the // reply in case of 404. However deleting a reply is not supported. So this case // is not implemented. throw new Error( 'Debug with statusCode 404 for reply is not implemented.' ); } if (foundHttpStatusCode.statusCode === 412) { reply.metadata.reply += '-simulated-change'; await req.repositories.annotation.editReply( req.cms, currentSession, reply, annotationIdentifier ); httpStatusCodeHelpers.sendDebugHttpResponse( res, foundHttpStatusCode ); return; } } } const editReplyResult = await req.repositories.annotation.editReply( req.cms, currentSession, reply, annotationIdentifier ); const httpStatusCode = httpStatusCodeHelpers.mapAnnotationResultStatusToHttpStatusCode( editReplyResult.status, true ); if (editReplyResult.status !== STATUS_OK) { res.status(httpStatusCode).end(); return; } res.status(httpStatusCode) .set('content-type', 'application/json; charset=utf-8') .json({ revisionId: editReplyResult.revisionId, reply: editReplyResult.annotation, }); }); }