UNPKG

@fontoxml/fontoxml-development-tools

Version:

Development tools for Fonto.

146 lines (134 loc) 3.62 kB
import EARLY_RETURN_SYMBOL from './review-annotations/EarlyReturnSymbol.js'; import httpStatusCodeHelpers from './review-annotations/httpStatusCodeHelpers.js'; import { STATUS_OK } from './review-annotations/Result.js'; export default function configureReviewAnnotationReplyPutRouteHandler( annotationDatabase, debugConfiguration ) { return (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; } debugConfiguration .getTimeoutConfigForRoute( req.cms, editSessionToken, '/review/annotation/reply', 'PUT' ) .then((timeoutSet) => { if (timeoutSet) { return Promise.reject(EARLY_RETURN_SYMBOL); } return debugConfiguration.isDebuggingEnabled( req.cms, editSessionToken ); }) .then((isDebuggingEnabled) => { const currentSession = req.getFontoSession(editSessionToken); if (isDebuggingEnabled) { const stringToVerify = reply.metadata.reply; let foundHttpStatusCode = httpStatusCodeHelpers.getDebugHttpStatusCode( stringToVerify, [400, 500] ); if (foundHttpStatusCode && !foundHttpStatusCode.onDelete) { httpStatusCodeHelpers.sendDebugHttpResponse( res, foundHttpStatusCode ); return Promise.reject(EARLY_RETURN_SYMBOL); } foundHttpStatusCode = httpStatusCodeHelpers.getDebugHttpStatusCode( stringToVerify, [403, 404, 412] ); if (foundHttpStatusCode && !foundHttpStatusCode.onDelete) { if (foundHttpStatusCode.statusCode === 403) { httpStatusCodeHelpers.sendDebugHttpResponse( res, foundHttpStatusCode ); return Promise.reject(EARLY_RETURN_SYMBOL); } if (foundHttpStatusCode.statusCode === 404) { return annotationDatabase .deleteReply( req.cms, currentSession, reply, annotationIdentifier ) .then(() => { httpStatusCodeHelpers.sendDebugHttpResponse( res, foundHttpStatusCode ); return Promise.reject(EARLY_RETURN_SYMBOL); }); } if (foundHttpStatusCode.statusCode === 412) { reply.metadata.reply += '-simulated-change'; return annotationDatabase .editReply( req.cms, currentSession, reply, annotationIdentifier ) .then(() => { httpStatusCodeHelpers.sendDebugHttpResponse( res, foundHttpStatusCode ); return Promise.reject(EARLY_RETURN_SYMBOL); }); } } } return annotationDatabase.editReply( req.cms, currentSession, reply, annotationIdentifier ); }) .then((editReplyResult) => { const httpStatusCode = httpStatusCodeHelpers.mapAnnotationResultStatusToHttpStatusCode( editReplyResult.status, true ); if (editReplyResult.status === STATUS_OK) { res.status(httpStatusCode) .set('content-type', 'application/json; charset=utf-8') .json({ revisionId: editReplyResult.revisionId, reply: editReplyResult.annotation, }); } else { res.status(httpStatusCode).end(); } }) .catch((error) => { if (error !== EARLY_RETURN_SYMBOL) { res.status(500).send(error); } }); }; }